objective c - week 3 - tuesday
TRANSCRIPT
-
8/14/2019 Objective C - Week 3 - Tuesday
1/14
September 03, 20
Data Types and Fun
with Classes
-
8/14/2019 Objective C - Week 3 - Tuesday
2/14
September 03, 20
Type Constant Examples NSLog charschar 'a', '\n' %c
short int %hi, %hx, %ho
unsigned short int %hu, %hx, %ho
int 12, -97, 0xFFE0, 0177 %i, %x, %o
unsigned int 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 longint
12ull, 0xffeeULL %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
Basic Data Types
-
8/14/2019 Objective C - Week 3 - Tuesday
3/14
September 03, 20
Operator Description Associativity
() Function call
[] Array element reference or message expression
-> Pointer to structure member reference Left to right
. Structure member reference or method call
- Unary minus
+ Unary plus
++ Increment
-- Decrement
! Logical negation
~ Ones complement Right to left
* Pointer reference (indirection)
& Address
sizeof Size of an object
(type) Type cast (conversion)
* Multiplication
/ Division Left to right
% Modulus
+ Addition Left to right
- Subtraction
> Right shift
< Less than
Greater than
>= Greater than or equal to
== Equality Left to right
!= 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
Order of Precedence
-
8/14/2019 Objective C - Week 3 - Tuesday
4/14
September 03, 20
// 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;
}
-
8/14/2019 Objective C - Week 3 - Tuesday
5/14
September 03, 20
@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
-
8/14/2019 Objective C - Week 3 - Tuesday
6/14
September 03, 20
@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
-
8/14/2019 Objective C - Week 3 - Tuesday
7/14
September 03, 20
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;
}
-
8/14/2019 Objective C - Week 3 - Tuesday
8/14
September 03, 20
-
8/14/2019 Objective C - Week 3 - Tuesday
9/14
September 03, 20
-
8/14/2019 Objective C - Week 3 - Tuesday
10/14
September 03, 20
-
8/14/2019 Objective C - Week 3 - Tuesday
11/14
September 03, 20
We're not working with Cocoa here, so change the line in the file Fraction.h that reads
#import
to read
#import
-
8/14/2019 Objective C - Week 3 - Tuesday
12/14
September 03, 20
@interface Fraction : NSObject{ int numerator; int denominator;}
@property int numerator, denominator;
-(void) print;-(double) convertToNum;@end
Synthesized Accessor Methods
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-
8/14/2019 Objective C - Week 3 - Tuesday
13/14
September 03, 20
Multiple Arguments to Methods
A method to set both the numerator and the denominator could be named setNumerator:andDenominator: , andyou might use it like this:
[myFraction setNumerator: 1 andDenominator: 3];
[myFraction setTo: 1 over: 3];
-
8/14/2019 Objective C - Week 3 - Tuesday
14/14
September 03, 20