initWithCoder:在NSObject中不可见?

时间:2012-04-28 11:16:18

标签: objective-c ios cocoa-touch nscoding

我有一个界面:

#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有什么不同吗? 感谢

1 个答案:

答案 0 :(得分:14)

ARC和手动引用计数之间没有任何区别。 NSObject不符合NSCoding,因此不会提供-initWithCoder:-encodeWithCoder:。只是不要调用那些方法的超类实现。

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