September 03, 2009
Data Types and Fun with Classes
September 03, 2009
Basic Data Types Type
Constant Examples
NSLog chars
char
'a','\n'
%c
shortint
—
%hi, %hx, %ho
unsigned short int
—
%hu, %hx, %ho
int
12, -97, 0xFFE0, 0177
%i, %x, %o
unsignedint
12u, 100U, 0XFFu
%u, %x, %o
long int
12L, -2001, 0xffffL
%li, %lx, %lo
unsigned long int
12UL, 100ul, 0xffeeUL
%lu, %lx, %lo
long long int
0xe5e5e5e5LL, 500ll
%lli, %llx, &llo
unsigned long long 12ull, 0xffeeULL int
%llu, %llx, %llo
float
12.34f, 3.1e-5f, 0x1.5p10, 0x1P-1
%f, %e, %g, %a
double
12.34, 3.1e-5, 0x.1p3
%f, %e, %g, %a
long double
12.341, 3.1e-5l
%Lf, $Le, %Lg
id
nil
%p
September 03, 2009
Order of Precedence Operator
Description
()
Function call
Associativity
[]
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
sizeof
Size of an object
(type)
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
Left to right
^
Bitwise XOR
Left to right
|
Bitwise OR
Left to right
&&
Logical AND
Left to right
||
Logical OR
Left to right
?:
Conditional
Right to left
= *= /= %= += -= &= ^= |= <<= >>=
Assignment operators
Right to left
,
Comma operator
Right to left
Left to right
Right to left
Left to right
Left to right
Left to right
Left to right
Left to right
September 03, 2009
// Basic conversions in Objective-C
#import 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 to read #import
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