2013-10-22 85 views
0

需求是我想將我的自定義對象的NSArray存儲在NSUserDefaults中。 以下是從我的例子在NSUserDefaults中存儲我的自定義對象的NSArray

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    sampleArray=[[NSMutableArray alloc]init]; 

    MyClass *obj1=[[MyClass alloc]init]; 
    [email protected]"Reetu"; 
    obj1.countOpen=1; 
    NSArray *subArray = [[NSArray alloc]initWithObjects:@"likes131",@"likes132",   @"likes133", nil]; 
    obj1.hobbies = [[NSArray alloc]initWithObjects:@"like11", @"like12", subArray, nil]; 
    [sampleArray addObject:obj1]; 

    MyClass *obj2=[[MyClass alloc]init]; 
    [email protected]"Pinku"; 
    obj2.countOpen=2; 
    NSArray *subArray2 = [[NSArray alloc]initWithObjects:obj1 ,@"likes231",@"likes232", @"likes233", obj1 ,nil]; 
obj2.hobbies = [[NSArray alloc]initWithObjects:@"like21", @"like22", subArray2 ,nil]; 
[sampleArray addObject:obj2]; 

    MyClass *obj3=[[MyClass alloc]init]; 
    [email protected]"Mike"; 
    obj3.countOpen=6; 

    obj3.hobbies = [[NSArray alloc]initWithObjects:obj1 , obj2 ,@"likes000", nil]; 
    [sampleArray addObject:obj3]; 

    //First lets encode it 
    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults]; 
    NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:sampleArray]; 
    [userDefault setObject:myEncodedObject forKey:[NSString stringWithFormat:@"sample"]]; 
    [userDefault synchronize]; 

    //Lets decode it now 
    NSData *myDecodedObject = [userDefault objectForKey: [NSString stringWithFormat:@"sample"]]; 
    NSArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject]; 

    //Print the array received from User's Default 
    for (MyClass *item in decodedArray) { 

     NSLog(@"name=%@",item.name); 
     NSLog(@"LIKES TO %@",item.hobbies); 
    } 

    } 

這是我的自定義類確認到NSCoding協議

- (void)encodeWithCoder:(NSCoder *)encoder 
{ 

    //Encode properties, other class variables, etc 
    [encoder encodeObject:self.name forKey:@"name"]; 
    [encoder encodeObject:[NSNumber numberWithInt:self.countOpen] forKey:@"destinationCode"]; 
    [encoder encodeObject:self.hobbies forKey:@"likesTo"]; 

} 

- (id)initWithCoder:(NSCoder *)decoder 
{ 
    self = [super init]; 
    if(self != nil) 
    { 
     self.name = [decoder decodeObjectForKey:@"name"]; 
     self.countOpen = [[decoder decodeObjectForKey:@"countOpen"] intValue]; 
     self.hobbies = [decoder decodeObjectForKey:@"likesTo"]; 
    } 
    return self; 
} 

這裏的代碼輸出: -

2013-10-22 17:01:47.118 Sample[1056:c07] name=Reetu 
2013-10-22 17:01:47.120 Sample[1056:c07] LIKES TO (
    like11, 
    like12, 
     (
     likes131, 
     likes132, 
     likes133 
    ) 
) 
2013-10-22 17:01:47.121 Sample[1056:c07] name=Pinku 
2013-10-22 17:01:47.123 Sample[1056:c07] LIKES TO (
    like21, 
    like22, 
     (
     "<MyClass: 0x6e32910>", 
     likes231, 
     likes232, 
     likes233, 
     "<MyClass: 0x6e32910>" 
    ) 
) 
2013-10-22 17:01:47.125 Sample[1056:c07] name=Mike 
2013-10-22 17:01:47.127 Sample[1056:c07] LIKES TO (
    "<MyClass: 0x6e32910>", 
    "<MyClass: 0x6e1f610>", 
    likes000 
) 

問題是<MyClass: 0X632910>。我期待它是obj1本身的內容。

+3

實際問題是什麼? –

+0

檢查輸出。問題是「」。我期待它是obj1的內容... – TryinHard

+1

請不要使用大寫字母,它對應於喊話。你爲什麼認爲我們知道你在期待什麼? –

回答

2

問題不在於NSUserDefaults。這是一個與你是如何打印出的信息:

您應該重寫-(NSString*) description,大概要讀的東西是這樣的:

-(NSString*) description 
{ 
    return [NSString stringWithFormat:@"<%@ - name:%@ open:%d>", self.class self.name, self.countOpen]; 
} 
0

您還沒有登錄的方式像你所添加的元素的數組。您正在向第二個數組添加MyClass對象,但您期望打印其業餘愛好NSArray

像這樣改變你的日誌功能。它會正確打印,直到第二個對象。對於第三個對象,你必須在函數中編寫另一個for循環。因爲你的對象層次就是這樣。

for (MyClass *item in decodedArray) { 

    NSLog(@"name=%@",item.name); 

    if ([item.technologies isKindOfClass:[MyClass class]]) { 
     for (MyClass *item in arr) { 
      NSLog(@"name=%@",item.name); 
      NSLog(@"LIKES TO %@",item.hobbies); 
     } 
    } 
    else{ 
     NSLog(@"LIKES TO %@",item.hobbies); 
    } 
} 
相關問題