简体   繁体   English

在NSUserDefaults中存储我的自定义对象的NSArray

[英]Storing a NSArray of my Custom Objects in NSUserDefaults

The requirement is that I want to store NSArray of my custom objects in NSUserDefaults . 该要求是,我要存储NSArray在我的自定义对象的NSUserDefaults Following is the code from my example 以下是我的示例中的代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    sampleArray=[[NSMutableArray alloc]init];

    MyClass *obj1=[[MyClass alloc]init];
    obj1.name=@"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];
    obj2.name=@"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];
    obj3.name=@"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);
    }

    }

This is my custom class confirming to the NSCoding protocol 这是我的自定义类确认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;
}

here is the output:- 这是输出: -

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
 )

The problem is <MyClass: 0X632910> . 问题是<MyClass: 0X632910> I was expecting it to be contents of obj1 itself. 我期待它是obj1本身的内容。

The problem isn't with NSUserDefaults. 问题不在于NSUserDefaults。 It's with how you are printing the information out: 这是你如何打印信息:

You should override -(NSString*) description , probably to read something like this: 你应该覆盖-(NSString*) description ,可能是这样的:

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

You are not logging the way like you are adding the elements to the array. 您没有像将数组添加到数组一样记录方式。 You are adding a MyClass object to the second array but you are expecting its hobbies NSArray to be printed. 您正在向第二个数组添加MyClass对象,但您希望打印其业余爱好NSArray

Change your logging function like this. 像这样更改日志记录功能。 It will print corretly until the second object. 它将正确打印,直到第二个对象。 For the third object you have to write another for loop in the function. 对于第三个对象,您必须在函数中编写另一个for循环。 Because your object hierarchy is like that. 因为您的对象层次结构就是这样。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM