0xS*_*ina 6 cocoa-touch objective-c nscoding ios
我有一个界面:
#import <Foundation/Foundation.h>
@interface Picture : NSObject;
@property (readonly) NSString *filepath;
- (UIImage *)image;
@end
Run Code Online (Sandbox Code Playgroud)
和实施:
#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
Run Code Online (Sandbox Code Playgroud)
我收到错误:ARC问题 - 'NSObject'没有可见的@interface声明选择器'initWithCoder:'
使用ARC时,NSCoding有什么不同吗?谢谢
小智 14
ARC和手动引用计数之间没有什么不同.NSObject不符合NSCoding,因此不供应-initWithCoder:或-encodeWithCoder:.只是不要调用那些方法的超类实现.
- (id)initWithCoder: (NSCoder *)aCoder {
self = [super init];
if (self) {
[aCoder decodeObject: filepath forKey: kFilepath];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4267 次 |
| 最近记录: |