2013-10-23 70 views
0

我想保存NSUserDefualts中繼承MKAnnotation類的對象。它在iOS 6中工作正常,但在iOS7中崩潰。下面是節約代碼:NSUserDefaults setobject方法崩潰

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 

設置之前類的對象是: enter image description here

崩潰:由於未捕獲的異常 '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

回答

1

在iOS7中,您不能將類對象存儲在NSUserDefaults中,您必須先將類轉換爲字典,數組或數據,然後再保存到NSuserDefaults中。 NSUserDefault實際上是一個plist,它將所有數據存儲在Library文件夾中。所以如果你想保存任何東西,你必須遵循屬性列表類型。

+1

要清楚的是,在iOS 6中,你也不能。不同之處在於iOS 6默默無法保存您的數據,而iOS 7讓您知道。 –

3

你的模型類不是一個屬性列表內容。在userdefault只能保存的plist內容像字符串,布爾,快譯通,陣列等。爲了節省您需要編寫編碼器/編碼器方法的自定義對象,如:(例子)

-(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中。

相關問題