2013-04-11 142 views
0

我將UIImage存儲在NSMutableArray中,然後將NSMutableArray存儲到NSDictionary中。 NSDictionary的密鑰是一個文件夾名稱,NSDictionary的值是一個NSMutableArray。 現在我如何存儲和檢索NSUserDefaults中的NSDictionary。用NSMutableArray將NSDictionary保存爲NSUserDefaults的值

我做了如下:可是

NSMutableArray *imageArray=[[NSMutableArray alloc] init]; 
[imageArray addObject:selectedImage]; 


[allFolderWithImageDict setValue:imageArray forKey:@"UniqueFolderName"]; 

NSUserDefaults *defauktCenter = [NSUserDefaults standardUserDefaults]; 
[defauktCenter setValue:allFolderWithImageDict forKey:@"FolderImageDict"]; 
[defauktCenter synchronize]; 

的NSDictionary不是在節省NSUserDefaults的。

請一些例子表明

感謝

+0

http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults – 2013-04-11 13:55:44

回答

1

存儲和檢索的值自定義對象,您可以使用NSKeyedArchiverNSKeyedUnarchiver類:

// To Save. . . 
NSData *resData = [NSKeyedArchiver archivedDataWithRootObject:allFolderWithImageDict]; 
[[NSUserDefaults standardUserDefaults] setObject:resData forKey:@"FolderImageDict"]; 

// To Load. . . 
NSData *respData = [[NSUserDefaults standardUserDefaults] objectForKey:@"FolderImageDict"]; 
resultDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:respData]; 
NSLog(@"dict :: %@",resultDictionary); 

好運!

+0

我得到了從這個鏈接回答http://rajuandroid.blogspot.in/2013/05/how-to-store-custom-objects-in.html – meera 2013-05-01 13:32:30

0

而不是使用的NSDictionary的,使用NSMutableDictionary .... :)

並檢索存儲在字典中的數據.....

NSUserDefaults *d = [NSUserDefaults standardUserDefaults]; 

    NSString *loadname1 = [d objectForKey:@"FolderImageDict"]; 

    NSLog(@"%@", loadname1); 
相關問題