2010-02-25 10 views
0

내 AppDelegate에서 스킨 클래스를 변수로 선언합니다. 나는 포인터를 다른 코드에 의해 재사용 된 것처럼 응용 프로그램에서 나중에 거기에 할당합니다.클래스 '속성이 할당없이 타입 변경

여러 가지 선언이 있지만 어떤 이유로 toolBarTint 항목이 내 응용 프로그램의 나중에 사용 시점에 디버거를 사용할 때 (임의로) 다른 유형으로 재 할당 된 것처럼 보입니다 (현재의 경우 UISectionRowData에서 변경 될 때마다) . 내가 할당하지 않는 곳은 내 앱에 있습니다. 대리인에 그런

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

그냥 관찰자 메소드를 추가 : -
@interface Skin : NSObject { 
    UIColor *navigationTint; 
    UIColor *searchBarTint; 
    UIColor *toolBarTint; 
    UITableViewStyle tableViewStyle;  
    CGFloat tableViewCellHeight; 
    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 

앱의 대표는

  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; 

답변

4

[UIColor colorWithRed:...] 개체가 자동으로 다시 열리므로 개체를 반환하므로 유효성이 보장되지 않습니다. 은 나중에 사용하기 위해 오브젝트를 보유해야합니다 (할당 대신 속성 보유 특성을 정의하고 dealloc 메소드에서 ivars를 해제하는 것을 잊지 마십시오).
귀하의 사례에서 어떤 일이 일어 났습니까? 귀하의 toolBarTint 개체가 결국 출시되어 메모리가 다른 개체에 의해 점유되었습니다.

0

당신이 toolBartTint 필드에 관찰자를 추가 해봤 피부를 정의 :

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

2

예 위에서 말한 내용을 반향시키고 다른 속성에도 적용됩니다. 나는이 할당 표현식 각각에 [... retain]을 추가하거나 속성 선언의 속성을 할당 대신 유지하도록 전환하는 것이 좋습니다. 또한 각 메소드를 해제하여 dealloc 메소드에서이를 정리하는 것을 잊지 마십시오. 일반적으로 Cocoa-Touch API는 메소드 본문 범위를 넘어서 유지하기 위해 명시 적으로 보유해야하는 자동 릴리스 된 객체를 반환하는 (+ classNameWith ...) 메소드의 규칙을 따릅니다. 즉, colorWith ... stringWith ... urlWith ...와 같은 팩토리 메서드 호출은 자동 해제 된 객체를 반환합니다. 그러나 객체를 할당/초기화하면 암시 적으로 유지됩니다.

+0

'할당'대신 '보유'에 +1 –

관련 문제