Objective-C: Cannot Change Value of Instance Variable Inside a Function

Posted by user262325 on Stack Overflow See other posts from Stack Overflow or by user262325
Published on 2010-06-17T11:47:10Z Indexed on 2010/06/17 13:33 UTC
Read the original article Hit count: 168

Hello everyone,

I cannot change the value of an instance variable inside a function.

I have defined a class:

//  info.h
#import <Foundation/Foundation.h>

@interface NSMyObject : NSObject 
{
    NSInteger i;
}

-(void) setI:(NSInteger)v;

 @end

#import "info.h"

@implementation NSMyObject

-(void) setI:(NSInteger)v ;
{
    i=v;    
}

- (void)dealloc {
    [super dealloc];
}

@end

I call a function 'myFunction' with parameter temObj which is a NSMyObject instance.

myFunction(temObj);//temObj is NSMyObject

In the function, I can change the value of the instance variable of parameter obj.

-(void)myFunction:(NSMyObject*) obj;
{
    [obj setI:0];
}

... expecting this to change the content of temObj.

But when I check the results of operation on obj in function myFunction the value of temObj.i has not changed.

Welcome any comment

Thanks

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c