A message send
class methods vs. instance methods
A message with an argument
a message send with three arguments
alloc and init
sending message to nil
id
memory management
how to declare instance variable and instance methods
#import <> vs. #import ""
using custom classes
setter and getter
setter and getter naming conventions
using properties replace instance variables
#import <Foundation/Foundation.h>
#import "BNRPerson.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// create an intance of BNRPerson
BNRPerson *mikey = [[BNRPerson alloc]init];
// give the instance variables interesting values using setters
[mikey setHeightInMeters:1.8];
[mikey setWeightInKilos:96];
// log the instance variables using the getters
float mikeyHeight = [mikey heightInMeters];
int mikeyWeight = [mikey weightInKilos];
NSLog(@"mikey is %.2f meters tall and weighs %d kilograms", mikeyHeight, mikeyWeight);
// log some values using custom methods;
float mikeyBMI = [mikey bodyMassIndex];
NSLog(@"mikey has a BMI of %f", mikeyBMI);
}
return 0;
}
dot notation
Another attribute that you will see shortly is copy
. Practically speaking, whenever you declare a property that points to an NSString
or an NSArray
, you should include the copy attribute
override and super
inheritance hierarchy
hierarchy of BMI
description and %@
@class vs. import
class extensions
isEqual vs. identical
immutable objects
sort
define
网友评论