2012-04-28 63 views
6

我有一個接口:initWithCoder:在NSObject中不可見?

#import <Foundation/Foundation.h> 

@interface Picture : NSObject; 

@property (readonly) NSString *filepath; 
- (UIImage *)image; 

@end 

和實施:

#import "Picture.h" 

#define kFilepath @"filepath" 

@interface Picture() <NSCoding> { 
    NSString *filepath; 
} 

@end 


@implementation Picture 
@synthesize filepath; 

- (id)initWithCoder:(NSCoder *)aDecoder { 
    self = [super initWithCoder:aDecoder]; 
    return self; 

} 

- (void)encodeWithCoder:(NSCoder *)aCoder { 
    [aCoder encodeObject:filepath forKey:kFilepath]; 
} 

- (UIImage *)image { 
    return [UIImage imageWithContentsOfFile:filepath]; 
} 

@end 

我得到錯誤:ARC問題 - 對 'NSObject的' 不可見@interface聲明選擇 '的initWithCoder:'

使用ARC時,NSCoding有什麼不同嗎? 謝謝

回答

13

ARC和手動引用計數沒有什麼不同。 NSObject不符合NSCoding,因此不提供-initWithCoder:-encodeWithCoder:。只是不要調用這些方法的超類實現。

- (id)initWithCoder: (NSCoder *)aCoder { 
    self = [super init]; 
    if (self) { 
     [aCoder decodeObject: filepath forKey: kFilepath]; 
    } 
    return self; 
}