Objective C Tutorial - Objective C Properties






Accessing and Getting Values in Properties

@interface MyClass : NSObject {

@private
    NSString *personName;
    int loopVar;
  
}

@property (retain) NSString *personName;
@property (assign) int loopVar;

-(void)displayMessage: (NSString *) myName count:(int)myLoop;
-(int)calculateValue: (int *)outsideData;

@end

@implementation MyClass

@synthesize personName;
@synthesize loopVar;


- (id)init {
    if ((self = [super init])) {
        // Initialization code here.
        NSLog (@"Hello, world!");
    }
    
    return self;
}

-(void)displayMessage: (NSString *) myName count:(int)myLoop;
{
    NSLog (@"Hello, %@", myName);
    NSLog (@"The loop will repeat %i times", myLoop);
    int i;
    for (i = 0; i < myLoop; i++)
    {
        NSLog (@"The value of i = %i", i);
    }
}

-(int)calculateValue: (int *)outsideData
{
    int hold;
    hold = *outsideData + *outsideData;
    *outsideData = 99;
    return hold;
}

@end



#import <Foundation/Foundation.h>
 
int main ()
{

  MyClass *testObject = [[MyClass alloc] init];
    testObject.personName = @"John Smith";
    testObject.loopVar = 2;
    //[testObject setPersonName: @"John Smith"];
    //[testObject setLoopVar: 2];
    int repeatLoop;
    repeatLoop = testObject.loopVar;
    [testObject displayMessage:testObject.personName count: repeatLoop];
    [testObject release];  
   return 0;
}

The code above generates the following result.