问题描述
- iOS归档自定义对面时,其中image属性怎么反序列化的时候为nil,下面是代码
-
//编码- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInteger:_age forKey:@"age"];
[aCoder encodeObject:_phoneNo forKey:@"phoneNo"];
[aCoder encodeObject:_image forKey:@"image"];
NSLog(@"111%@",_image);
}
//解码
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
_name = [aDecoder decodeObjectForKey:@"name"];
_age = [aDecoder decodeIntegerForKey:@"age"];
_phoneNo = [aDecoder decodeObjectForKey:@"phoneNo"];
_image = [aDecoder decodeObjectForKey:@"image"];
NSLog(@"222%@",[aDecoder decodeObjectForKey:@"image"]);
return self;
}
其他三个属性都是有值的Person对象定义属性的时候,UIImage直接敲好像出不来 ,手动加入了UIKit.h
#import
#import
@interface Person : NSObject
@property (strong, nonatomic) NSString *name;
@property (assign, nonatomic) NSInteger age;
@property (strong, nonatomic) NSString *phoneNo;
@property (strong, nonatomic) UIImage *image;
@end不知道是不是这个原因导致的?求大神解答疑惑,谢谢了
- (void)encodeWithCoder:(NSCoder *)aCoder{
解决方案
http://www.2cto.com/kf/201505/399869.html
解决方案二:
UIImage要转NSData