Daniel
Daniel

Reputation: 87

How can I simply change a class variable from another class in ObjectiveC?

I simply want to change a variable of an object from another class. I can compile without a problem, but my variable always is set to 'null'. I used the following code:

Object.h:

@interface Object : NSObject {
    //...
    NSString *color;
    //...
}

@property(nonatomic, retain) NSString* color;

+ (id)Object;
- (void)setColor:(NSString*)col;
- (NSString*)getColor;
@end

Object.m:

+(id)Object{
    return [[[Object alloc] init] autorelease];
}

- (void)setColor:(NSString*)col {
    self.color = col;
}

- (NSString*)getColor {
    return self.color;
}

MyViewController.h

#import "Object.h"

@interface ClassesTestViewController : UIViewController {
    Object *myObject;
    UILabel *label1;
}

@property UILabel *label1;
@property (assign) Object *myObject;
@end

MyViewController.m:

#import "Object.h"
@implementation MyViewController
@synthesize myObject;

- (void)viewDidLoad {
    [myObject setColor:@"red"];
    NSLog(@"Color = %@", [myObject getColor]);
    [super viewDidLoad];
}

The NSLog message is always Color = (null)

I tried many different ways to solve this problem, but no success. Any help would be appreciated.


Thanks for the help so far.

I modified the code as follow, but it still doesn't work as it should.

MyViewController.h:
    #import <UIKit/UIKit.h>
    #import "Object.h"

    @interface MyViewController : UIViewController {
        Object *myObject;
    }
    @property (nonatomic, retain) Object *myObject;
    @end

MyViewController.m:

#import "MyViewController.h"
#import "Object.h"

@implementation MyViewController
@synthesize myObject;

- (void)viewDidLoad {
Object *myObject = [Object new];
myObject = 0;
[myObject setColor:@"red"];
NSLog(@"color = %@", myObject.color);
[super viewDidLoad];
}

If I do it like this, NSLog returns color = null (and I think myObject is only visible in viewDidLoad). How can declare myObject and make it visible in MyViewController? I stripped down my Object class to

Object.h:

@interface Object : NSObject {
    NSString *color;
}    
@property(nonatomic, retain) NSString *color;
@end

Object.m:

#import "Object.h"
@implementation Object
@synthesize color;
@end

I wasn't able to define an object myObject in ViewDidLoad so that I can access its properties from the whole ViewController class? What did I miss? Side question: Why do I have to set myObject to 0?

Upvotes: 1

Views: 1031

Answers (1)

philsquared
philsquared

Reputation: 22493

  1. You're declaring a property, then explicitly declaring the accessors in Object.h. You only need to do one or the other - they mean the same thing (well, almost - you'll have color instead of getColor)
  2. To implement the property in Object.m you should use @synthesize color. The explicit implementations, again, are then redundant (unless they do anything extra).
  3. The explicit setColor implementation in Object.m is calling the property - which you are implementing explicitly, so I would have expected you to get an infinite recursion here.
  4. MyViewController.m should probably synthesize label1, since you declare the property in the header (although it's not being used in your snippet).
  5. [myObject getColor] is calling the color property, which you declared but did not synthesize. If you had explicitly implemented it as color it would have picked that up - but it won't match getColor (which is fortunately as that would have led to an infinite recursion again.
  6. I don't see anywhere where you create your myObject instance. If you don't it will be nil and methods called on it (including property accesses) will return 0 or nil.

I suspect (6) is the cause of your issue, but the others need to be addressed too. Make sure you read up on property syntax.

Upvotes: 3

Related Questions