开发者

Setter method not being called

I have开发者_C百科 this code:

.h

int cellsPerRow;

@property int cellsPerRow;

.m

@synthesize cellsPerRow;

-(void)init {
...
cellsPerRow = 4;
}

-(void)setCellsPerRow:(int)cellsPerRowLocal {
cellsPerRow = cellsPerRowLocal;
....
}

But the setter method isn't being called. Any ideas why?


You are assigning to the variable instead of the property. Instead of:

cellsPerRow = 4;

You need to do either:

self.cellsPerRow = 4;

or

[self setCellsPerRow:4];

The former is transformed by the compiler into the later, so they are equivalent.


The property and synthesize create the setter and getter methods for you. In essence what you are doing is actually override the methods that were created for you in the first place.

Don't do that. Just use dot notation, like so, self.cellsPerRow = cellsPerRowLocal. This will set your cellsPerRow ivar to whatever you want it to be in cellsPerRowLocal.

In you header file:

int cellsPerRow;

@property (nonatomic, retain) int cellsPerRow;

In your implementation file:

@synthesize cellsPerRow;

- (void) inThisMethodYouSetcellsPerRow {

cellsPerRowLocal = ... ;

self.cellsPerRow = cellsPerRowLocal;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜