views:

127

answers:

2

Ok I think my understanding of properties in objective c may not be what I thought it was.

In my program I have a singleton that contains my class.

In my class during the init I assign a value from the singleton to my property.

I then assign a value to a property of that property.

However it does not keep the value and when I do a compare of the value in the singleton nothing has changed. What is going on here? Any ideas?

@interface MainGameLoop : NSObject {
    MapData *mapData;
}

@property (retain) MapData *mapData;

-(id) init
{
    self = [super init];
    GlobalVariables *sharedManager = [GlobalVariables sharedManager];
    self.mapData = sharedManager.mapData;   
    return self;
}

In a function of my class:

works:

sharedManager.mapData.currentPlayer = newCurrentPlayer;

does nothing:

self.mapData.currentPlayer == newCurrentPlayer;
+12  A: 
self.mapData.currentPlayer == newCurrentPlayer;

Are you sure that you want two equal signs there? That statement is syntactically correct and will evaluate to either true or false.

Dave DeLong
This is one of several reasons to turn on the “Unused Value” warning in the build settings.
Peter Hosey
wow. It is funny when you are new at something you just have no confidence. All sorts of crazy ideas were running through my head as to why this was happening and it turns out to be something crazy simple. Thanks!
Mel
+1  A: 

== is a Boolean operator, while = is an assignment operator. Like what Dave said, if you are using an if statement such as if (self.mapData.currentPlayer == newCurrentPlayer) {…}, you would want to use == because it would evaluate to true or false, while = would be used to set the value of a variable, which is what I think you are trying to do.

If it's any consolation, I've made that mistake too many times to count…

Something that I do is to use NSLog() or printf() to make sure that each step is working correctly.

Alexsander Akers