-2
PFQuery *Location = [PFQuery queryWithClassName:@"Location"];
[Location findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {      
  NSLog(@"%@", [objects objectAtIndex:0]);             
}];

このオブジェクトを NSUserDefaults に保存するにはどうすればよいですか?

4

2 に答える 2

0

カスタム オブジェクトを格納するには、カスタム オブジェクト クラスの m ファイルにこれら 2 つのメソッドを追加する必要があります。

-(void)encodeWithCoder:(NSCoder *)encoder
{
    //Encode the properties of the object
    [encoder encodeObject:self.contact_fname forKey:@"contact_fname"];
    [encoder encodeObject:self.contact_lname forKey:@"contact_lname"];
    [encoder encodeObject:self.contact_image forKey:@"contact_image"];
    [encoder encodeObject:self.contact_phone_number forKey:@"contact_phone_number"];

}

-(id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if ( self != nil )
    {
        //decode the properties
        self.contact_fname = [decoder decodeObjectForKey:@"contact_fname"];
        self.contact_lname = [decoder decodeObjectForKey:@"contact_lname"];
        self.contact_image = [decoder decodeObjectForKey:@"contact_image"];
        self.contact_phone_number = [decoder decodeObjectForKey:@"contact_phone_number"];
    }
    return self;
}

それから

-(void)writeArrayWithCustomObjToUserDefaults:(NSString *)keyName withArray:(NSMutableArray *)myArray
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];
    [defaults setObject:data forKey:keyName];
    [defaults synchronize];
}

-(NSArray *)readArrayWithCustomObjFromUserDefaults:(NSString*)keyName
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:keyName];
    NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    [defaults synchronize];
    return myArray;
}

これらの関数を使用してカスタム オブジェクト配列を保存および読み取るか、単にこのライブラリを使用できますhttps://github.com/roomorama/RMMapper

于 2015-05-15T11:27:33.170 に答える