2011-10-03 101 views
0

我想在NSUserDefaults中保存一個對象,但是我沒有成功。情景是,我有SyncObjct類的對象syncObject。我在SyncObject類中實現了以下方法在NSUserDefaults中存儲自定義對象

- (NSMutableDictionary*)toDictionary; 
+ (SyncObject*)getFromDictionary :(NSMutableDictionary*)dictionary; 
- (id) initWithCoder: (NSCoder *)coder; 
- (void) encodeWithCoder: (NSCoder *)coder; 

而且SyncObject類是NSCoding類。現在,SyncObject類包含一個名爲jobsArray的NSMutableArray。該數組包含類JobsObject的對象。我爲這個類實現了上述相同的方法,以使它符合編碼。在SyncObject類的toDictionary方法中,我通過編寫以下行將此數組存儲在字典中。

[dictionary setValue:jobsArray forKey:@"jobsArray"]; 

我正在通過編寫以下行在SyncOject類的getFromDictionary方法中檢索它。

+ (SyncObject*)getFromDictionary :(NSMutableDictionary*)dictionary 
{  
    NSLog(@"SyncObject:getFromDictionary"); 
    SyncObject *syncObject = [[SyncObject alloc] init]; 

    @try 
    { 

     syncObject.noOfJobs = [[dictionary valueForKey:@"noOfJobs"] intValue]; 
     syncObject.totalJobsPerformed = (TotalJobsPerformed*)[dictionary valueForKey:@"totalobsPerformed"]; 
     syncObject.jobsArray = [dictionary valueForKey:@"jobsArray"]; 

    } 
    @catch (NSException * e) 
    { 
     NSLog(@"EXCEPTION %@: %@", [e name], [e reason]); 
    } 
    @finally 
    { 

    } 

    return syncObject; 
} 

而且我還通過編寫以下行在NSUserDefault中存儲syncObject。

NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:syncObject]; 
[userStorage setObject:myEncodedObject forKey:SYNC_OBJECT]; 

我NSUserDefaults的從通過編寫以下行

NSData *myEncodedObject = [userStorage objectForKey: SYNC_OBJECT]; 
    syncObject = (SyncObject*)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject]; 

問題檢索對象是我不能夠正確地檢索jobsArray。一些無效的對象返回並且在嘗試訪問應用程序時崩潰。請誰能告訴我這個問題的原因?

問候

+0

您使用的線程?你叫NSUserDefault的同步嗎? – daveoncode

回答

0

我相信問題是NSUserDefaults的不支持存儲自定義對象類型。

從Apple的文檔使用-setObject時:value參數可以只是屬性列表對象:NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary。對於NSArray和NSDictionary對象,其內容必須是屬性列表對象。

您可以直接從Apple's NSUserDefault Class Reference manual查看其餘的文檔。

0

自定義類的.h

#import <Foundation/Foundation.h> 

@interface Person : NSObject 

@property(nonatomic, retain) NSArray *names; 

@property(strong,nonatomic)NSString *name; 


@end 

自定義類的.m

#import "Person.h" 

@implementation Person 
@synthesize names; 


- (void)encodeWithCoder:(NSCoder *)encoder { 
    [encoder encodeObject:self.name forKey:@"name"]; 
} 

- (id)initWithCoder:(NSCoder *)decoder { 
    if((self = [super init])) { 
     //decode properties, other class vars 
     self.name = [decoder decodeObjectForKey:@"name"]; 

    } 
    return self; 
} 


@end 

要創建Person類對象和存儲和檢索您的NSUserDefaults的數據存儲

存儲你的對象:

#import "ViewController.h" 
#import "Person.h" 

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UITextField *textBox; 

@end 

NSMutableArray *arr; 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (IBAction)saveButton:(id)sender { 
    Person *person =[Person new]; 
    person.name = _textBox.text; 

    arr = [[NSMutableArray alloc]init]; 
    [arr addObject:person]; 

    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:arr]; 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:encodedObject forKey:@"name"]; 
    [defaults synchronize]; 
} 
- (IBAction)showButton:(id)sender { 
    _label.text = @""; 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    NSData *storedEncodedObject = [defaults objectForKey:@"name"]; 
    NSArray *arrStoreObject = [NSKeyedUnarchiver unarchiveObjectWithData:storedEncodedObject]; 

    for (int i=0; i<arrStoreObject.count; i++) 
    { 
     Person *storedObject = [arrStoreObject objectAtIndex:i]; 

     _label.text = [NSString stringWithFormat:@"%@ %@", _label.text , storedObject.name]; 

    } 


} 

@end 
相關問題