Starting with arithmetic expressions but I already read that last class.

Everything inherits from NSObject.

Person.m

Define the implementation of the Person class.

#import "Person.h"

@implementation Person

@end

Person.h

Define the interface for Person.

#import "Person.h"

@interface Person : NSObject

@end

strong vs weak references

Strong reference is like a dog on a leash. The dog wants to run a way. strong is like the leash. As long as the pointer is strong then the dog can’t run away. With a weak reference the dog can run away.

method invocation

Instead of calling it invoking a method or calling a function it is described as “sending a message”.

To invoke a class method.

[Person new]

To invoke an instance method.

Person * person = [Person new];
  [person sayHi]

Documentation for NSObject#new

new Allocates a new instance of the receiving class, sends it an init message, and returns the initialized object.

Discussion This method is a combination of alloc and init. Like alloc, it initializes the isa instance variable of the new object so it points to the class data structure. It then invokes the init method to complete the initialization process.

You will never see people use “new”. Instead they use alloc and init.

Person * person = [[Person alloc] init];

Properties

To get and set you can send messages to the object. To set a property you need to prefix the message with “set”.

Person * person = [Person new];
  [person setFirstName:@"Mo"];
  NSLog(@"%@", [person firstName]);

dot syntax

Person * person = [Person new];
  person.firstName = "Mo";
  NSLog(@"%@", person.firstName);

Named Parameters

@interface Person : NSObject
-(void) name:(NSString *)theName age:(int)theAge;
@end

@implementation Person

-(void) name:(NSString *)theName age:(int)theAge
{
}

@end

Person * person = [Person new];
[person name:@"blah" age:2]
comments powered by Disqus