cprg-190 day two

Posted on May 09, 2013 ios sait

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

Everything inherits from NSObject.


Define the implementation of the Person class.

#import "Person.h"

@implementation Person



Define the interface for Person.

#import "Person.h"

@interface Person : NSObject


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.

  • (id)new Return Value A new instance of the receiver.

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];


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;

@implementation Person

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


Person * person = [Person new];
[person name:@"blah" age:2]