September 03, 2009

Data Types and Fun with Classes

September 03, 2009

Basic Data Types
Type char shortint unsigned short int int unsignedint long int unsigned long int long long int Constant Examples 'a', '\n' — — 12, -97, 0xFFE0, 0177 12u, 100U, 0XFFu 12L, -2001, 0xffffL 12UL, 100ul, 0xffeeUL 0xe5e5e5e5LL, 500ll NSLog chars %c %hi, %hx, %ho %hu, %hx, %ho %i, %x, %o %u, %x, %o %li, %lx, %lo %lu, %lx, %lo %lli, %llx, &llo %llu, %llx, %llo %f, %e, %g, %a %f, %e, %g, %a %Lf, $Le, %Lg %p

unsigned long long 12ull, 0xffeeULL int float double long double id 12.34f, 3.1e-5f, 0x1.5p10, 0x1P-1 12.34, 3.1e-5, 0x.1p3 12.341, 3.1e-5l nil

September 03, 2009

Order of Precedence
Operator () [] -> . + ++ -! ~ * & sizeof (type) * / % + << >> < <= > >= == != & ^ | && || ?: = *= /= %= += -= &= ^= |= <<= >>= , Description Function call Array element reference or message expression Pointer to structure member reference Structure member reference or method call Unary minus Unary plus Increment Decrement Logical negation Ones complement Pointer reference (indirection) Address Size of an object Type cast (conversion) Multiplication Division Modulus Addition Subtraction Left shift Right shift Less than Less than or equal to Greater than Greater than or equal to Equality Inequality Bitwise AND Bitwise XOR Bitwise OR Logical AND Logical OR Conditional Assignment operators Left to right Left to right Left to right Left to right Left to right Right to left Right to left Left to right Left to right Left to right Left to right Left to right Right to left Left to right Associativity

Comma operator

Right to left

September 03, 2009

// Basic conversions in Objective-C

#import <Foundation/Foundation.h> int main (int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; float f1 = 123.125, f2; int i1, i2 = -150; i1 = f1; // floating to integer conversion NSLog (@"%f assigned to an int produces %i", f1, i1); f1 = i2; // integer to floating conversion NSLog (@"%i assigned to a float produces %f", i2, f1); f1 = i2 / 100; // integer divided by integer NSLog (@"%i divided by 100 produces %f", i2, f1); f2 = i2 / 100.0; // integer divided by a float NSLog (@"%i divided by 100.0 produces %f", i2, f2); f2 = (float) i2 / 100; // type cast operator NSLog (@"(float) %i divided by 100 produces %f", i2, f2); [pool drain]; return 0; }

September 03, 2009

@interface Calculator: NSObject { double accumulator; } // accumulator methods -(void) setAccumulator: (double) value; -(void) clear; -(double) accumulator; // arithmetic methods -(void) add: (double) value; -(void) subtract: (double) value; -(void) multiply: (double) value; -(void) divide: (double) value; @end

September 03, 2009

@implementation Calculator -(void) setAccumulator: (double) value { accumulator = value; } -(void) clear { accumulator = 0; } -(double) accumulator { return accumulator; } -(void) add: (double) value { accumulator += value; } -(void) subtract: (double) value { accumulator -= value; } -(void) multiply: (double) value { accumulator *= value; } -(void) divide: (double) value { accumulator /= value; } @end

September 03, 2009

int main (int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Calculator *deskCalc; deskCalc = [[Calculator alloc] init]; [deskCalc clear]; [deskCalc setAccumulator: 100.0]; [deskCalc add: 200.]; [deskCalc divide: 15.0]; [deskCalc subtract: 10.0]; [deskCalc multiply: 5]; NSLog (@"The result is %g", [deskCalc accumulator]); [deskCalc release]; [pool drain]; return 0; }

September 03, 2009

September 03, 2009

September 03, 2009

September 03, 2009

We're not working with Cocoa here, so change the line in the file Fraction.h that reads #import <Cocoa/Cocoa.h> to read #import <Foundation/Foundation.h>

September 03, 2009

Synthesized Accessor Methods
@interface Fraction : NSObject {    int numerator;    int denominator; } @property int numerator, denominator; -(void)    print; -(double)  convertToNum; @end

#import "Fraction.h"

@implementation Fraction @synthesize numerator, denominator;

September 03, 2009

Multiple Arguments to Methods

A method to set both the numerator and the denominator could be named setNumerator:andDenominator: , and you might use it like this: [myFraction setNumerator: 1 andDenominator: 3];

[myFraction setTo: 1 over: 3];

September 03, 2009

September 03, 2009

September 03, 2009

Sign up to vote on this title
UsefulNot useful