Reputation: 87
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
Reputation: 22493
color
instead of getColor
)@synthesize color
. The explicit implementations, again, are then redundant (unless they do anything extra).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.label1
, since you declare the property in the header (although it's not being used in your snippet).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.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