【问题标题】:NSUserDefaults setobject method crashingNSUserDefaults setobject 方法崩溃
【发布时间】:2013-10-23 06:27:21
【问题描述】:

我试图在 NSUserDefualts 中保存一个继承 MKAnnotation 类的对象。它在 iOS 6 中运行良好,但在 iOS7 中崩溃。这是保存的代码:

    ParkHereAnnotation = [[catAnnotation alloc] initWithCoordinate:workingCoordinate];
    ParkHereAnnotation.title = @"Your Parked Here" ;
    ParkHereAnnotation.subtitle = @"";
    //ParkHereAnnotation.annotationType = location;
    ParkHereAnnotation.parkId = [[NSString alloc] initWithString:@"-1"];
    [ParkHereAnnotation setAnnotationType:location];
    [userDefaults setObject:ParkHereAnnotation forKey:@"ParkingAnnotation"];//Crashing here in iOS7 but not in iOS6.

类: 标题

头文件

@interface catAnnotation : NSObject <MKAnnotation>
{
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
    NSString *parkId;
    enum MapAnnotationType annotationType;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, assign) enum MapAnnotationType annotationType;
@property (nonatomic,retain) NSString *parkId;


@end

实施

@implementation catAnnotation
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize annotationType;
@synthesize parkId;

-(id) init
{
    return self;
}

-(id) initWithCoordinate:(CLLocationCoordinate2D)inCoord
{
    coordinate = inCoord;
    return self;
}

@end

设置前的类对象为:

崩溃: * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSUserDefaults setObject:forKey:]: 尝试为关键的 ParkingAnnotation 插入非属性列表对象” *** 首先抛出调用堆栈: (0x31576e8b 0x3b8716c7 0x31576dcd 0x31ea7c21 0x126613 0x12708f 0x126f45 0x33d3155f 0x33d314fb 0x33d314cb 0x33d1d0f3 0x33d398cd 0x33d30b0d 0x33d2bc09 0x33d00f59 0x33cff747 0x31541f27 0x315413ef 0x3153fbdf 0x314aa541 0x314aa323 0x361e12eb 0x33d611e5 0xf811d 0x3bd6aab7) libc++abi.dylib:以 NSException 类型的未捕获异常终止

【问题讨论】:

    标签: iphone annotations maps ios7


    【解决方案1】:

    您的模型类不是属性列表内容。在 userdefault 中,您只能保存 plist 内容,例如 string、bool、dict、array 等。要保存自定义对象,您需要编写 coder/encoder 方法,例如:(example)

     -(void)encodeWithCoder:(NSCoder*)coder
    {
        [coder encodeObject:_title];
        [coder encodeObject:_album];
        [coder encodeObject:_pathURL];
        [coder encodeObject:_daysOfGoal];
        [coder encodeObject:_numberOfTimesPlayed];
        [coder encodeObject:_infoString];
        [coder encodeObject:_duration];
        [coder encodeObject:_numberOfTimesPlayed];
        [coder encodeObject:_dosage];
        [coder encodeObject:_creationDate];
        [coder encodeObject:_type];
        [coder encodeBool:_isAutosuggestion forKey:@"isAutosuggestion"];
    
    }
    
    - (id)initWithCoder:(NSCoder *)coder
    {
        self = [super init];
    
        if(self)
        {
            [self setTitle:[coder decodeObject]];
            [self setAlbum:[coder decodeObject]];
            [self setPathURL:[coder decodeObject]];
            [self setDaysOfGoal:[coder decodeObject]];
            [self setNumberOfTimesPlayed:[coder decodeObject]];
            [self setInfoString:[coder decodeObject]];
            [self setDuration:[coder decodeObject]];
            [self setNumberOfTimesPlayed:[coder decodeObject]];
            [self setDuration:[coder decodeObject]];
            [self setDosage:[coder decodeObject]];
            [self setCreationDate:[coder decodeObject]];
            [self setIsAutosuggestion:[coder decodeBoolForKey:@"isAutosuggestion"]];
    
        }
    
        return self;
    }
    

    您需要添加这些方法以保存在 userdefault 中。

    【讨论】:

      【解决方案2】:

      在 iOS7 中,您不能将类对象存储在 NSUserDefaults 中,您必须先将类转换为字典、数组或数据,然后才能保存在 NSuserDefaults 中。 NSUserDefault 实际上是一个 plist,它将所有数据存储在 Library 文件夹中。因此,如果您想在其中保存任何内容,则必须遵循属性列表类型。

      【讨论】:

      • 明确地说,在 iOS 6 中你也不能。不同之处在于 iOS 6 会默默地保存您的数据,而 iOS 7 会让您知道。
      猜你喜欢
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多