开发者

Class' property changes type without assignment

I am declaring a Skin class as a variable on my AppDelegate. I only ever assign to it there but later in the app its as if the pointer has been reused by some other code.

I have various declarations but for some reason the item toolBarTint appears to be reassigned (randomly) a different Type when using the debugger at the point of usage later in my app, in the current case UISectionRowData (but changes each time). I do not assign to is anywhere else in my app.

@interface Skin : NSObject {
    UIColor *navigationTint;
    UIColor *searchBarTint;
    UIColor *toolBarTint;
    UITableViewStyle tableViewStyle;    
    CGFloat tab开发者_JAVA技巧leViewCellHeight;
    UIColor *tableViewBackgroundColour;
    MKPinAnnotationColor *pinColour;
    NSString * locationViewFontName;
    CGFloat locationViewFontSize;
}

@property (nonatomic,assign) UIColor *navigationTint;   
@property (nonatomic,assign) UIColor *searchBarTint;
@property (nonatomic,assign) UIColor *toolBarTint;
@property (nonatomic,assign) UITableViewStyle tableViewStyle;   
@property (nonatomic,assign) CGFloat tableViewCellHeight;   
@property (nonatomic,assign) UIColor *tableViewBackgroundColour;
@property (nonatomic,assign) MKPinAnnotationColor *pinColour;
@property (nonatomic,retain) NSString * locationViewFontName;
@property (nonatomic,assign) CGFloat locationViewFontSize;

@end

-- App delegate define skin

            skin = [[Skin alloc] init];

    skin.navigationTint = [UIColor colorWithRed:((float) 154 / 255.0f) green:((float) 98 / 255.0f) blue:((float) 176  / 255.0f) alpha:1.0f];
    skin.searchBarTint = [UIColor colorWithRed:((float) 154 / 255.0f) green:((float) 98 / 255.0f) blue:((float) 176 / 255.0f) alpha:1.0f];
    skin.toolBarTint = [UIColor colorWithRed:((float) 154 / 255.0f) green:((float) 98 / 255.0f) blue:((float) 176 / 255.0f) alpha:1.0f];
    skin.tableViewStyle = UITableViewStyleGrouped;
    skin.tableViewCellHeight = 60.0;
    skin.tableViewBackgroundColour = [UIColor colorWithRed:((float) 154 / 255.0f) green:((float) 98 / 255.0f) blue:((float) 176 / 255.0f) alpha:1.0f];
    skin.pinColour = MKPinAnnotationColorRed;
    skin.locationViewFontName = @"Helvetica";
    skin.locationViewFontSize = 15.0f;


[UIColor colorWithRed:...] returns autoreleased object so its validity is not guaranteed outside scope where you create it. You must retain object for later use (define property with retain attribute instead of assign and not forget to release your ivars in dealloc method).
What happened in your case - your toolBarTint object was released eventually and then memory was occupied by some other object.


Yes I echo what was said above and it applies to the other properties as well. I would suggest adding [... retain] around each of these assignment expressions or switching the attribute in the property declaration to be retain instead of assign. Also don't forget to clean these up in your dealloc method by releasing each one. Generally speaking the Cocoa-Touch APIs follow convention of (+ classNameWith...) methods returning auto-released objects that you have to explicitly retain to keep beyond the method body scope. That means any factory method call like colorWith... stringWith... urlWith... will return an auto released object. However, if you alloc/init the object it will be implicitly retained.


Have you tried adding an observer for the toolBartTint field:

[skin addObserver:self forKeyPath:@"toolBarTint" options: NSKeyValueObservingOptionNew context:NULL]

Then in your delegate just add the observer method:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { }

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜