使用自定义对象保存NSMutableArray时出现问题

时间:2015-06-01 02:36:21

标签: ios objective-c uiimage nscoding

我有一个名为CurrentUser的班级,其中包含名为NSMutableArray的{​​{1}}。此listOfFriends保存来自班级listOfFriends的对象。

FriendsCurrentUser类都处理了Friend个协议。 NSCoding对象有一个名为Friend的属性,它是userImg。 只有一个问题。 当我尝试加载UIImage时,好友不会来lisOfFriends

如果我保存了任何朋友,并尝试重新加载它,则会UIImages没有问题。但是,如果对象位于UIImage内,则listOfFriends不会附带,但所有其他信息都会正确加载。

我的问题是:如何使用自定义对象存储和加载UIImage? friendList的声明:

CurrentUser.h

NSMutableArray

CurrentUser.m

@property (nonatomic,strong)NSMutableArray *FriendsList;

Friend.m

  - (void) encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:username forKey:@"username"];
    [encoder encodeObject:FriendsList forKey:@"FriendsList"];
    //testing direct friend saving
    [encoder encodeObject:[FriendsList objectAtIndex:0] forKey:@"testFriend"];
    [encoder encodeObject:_userPSW forKey:@"userpassword"];
    [encoder encodeObject:UIImagePNGRepresentation(userImg) forKey:@"userImg"];
    [encoder encodeObject:userID forKey:@"userID"];
    [encoder encodeObject:groups forKey:@"groups"];
    [encoder encodeObject:_email forKey:@"email"];

}

- (id)initWithCoder:(NSCoder *)decoder {
    currentUser *returnUser = [[currentUser alloc] init];
    returnUser.username = [decoder decodeObjectForKey:@"username"];
    returnUser.email = [decoder decodeObjectForKey:@"email"];
    //testing direct friend loading
    Friend *testFriend = [decoder decodeObjectForKey:@"testFriend"];
    returnUser.userPSW = [decoder decodeObjectForKey:@"userpassword"];
    returnUser.userImg = [UIImage imageWithData:[decoder decodeObjectForKey:@"userImg"]];
    returnUser.FriendsList = [decoder decodeObjectForKey:@"FriendsList"];
    returnUser.userID = [decoder decodeObjectForKey:@"userID"];
    returnUser.groups = [decoder decodeObjectForKey:@"groups"];

    return returnUser;
}

这是我必须保存并加载CurrentUser的对象,它被称为“配置”。 Configurations.h

- (void)encodeWithCoder:(NSCoder *)encoder{  
    [encoder encodeObject:UIImagePNGRepresentation(userImg) forKey:@"friendIMG"];
    [encoder encodeObject:username forKey:@"friendUN"];
    [encoder encodeObject:userID forKey:@"friendID"];

}

- (id)initWithCoder:(NSCoder *)decoder{
    Friend *obj = [[Friend alloc]init];
    obj.username = [decoder decodeObjectForKey:@"friendUN"];
    obj.userImg = [UIImage imageWithData:[decoder decodeObjectForKey:@"friendIMG"]];
    obj.userID = [decoder decodeObjectForKey:@"friendID"];
    return obj;

}

Configurations.m

#import "currentUser.h"
@interface configurations : NSObject
- (currentUser*)loadCustomObject;
- (void)saveCustomObject:(currentUser *)object;

@end

这就是我加载CurrentUser的方式。

#import "configurations.h"

@implementation configurations
- (void)saveCustomObject:(currentUser *)object {
    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:encodedObject forKey:@"lastUser"];
    [defaults synchronize];

}

- (currentUser*)loadCustomObject {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *encodedObject = [defaults objectForKey:@"lastUser"];
    currentUser *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
    return object;
}
@end

1 个答案:

答案 0 :(得分:2)

您的initWithCoder:方法不正确。它们需要采用以下模式:

- (instancetype)initWithCoder:(NSCoder *)decoder {
    self = [super init]; // or [super initWithCoder:decoder] if appropriate
    if (self) {
        _someIvar = [decoder decodeObjectForKey:@"someKey"];
        _someOtherIvar = [decoder decodeObjectForKey:@"someOtherKey"];
    }

    return self;
}

initWithCoder:CurrentUser类修复Friend方法会有很大帮助。

CurrentUser:

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];
    if (self) {
        _username = [decoder decodeObjectForKey:@"username"];
        _email = [decoder decodeObjectForKey:@"email"];
        //testing direct friend loading
        Friend *testFriend = [decoder decodeObjectForKey:@"testFriend"];
        _userPSW = [decoder decodeObjectForKey:@"userpassword"];
        _userImg = [UIImage imageWithData:[decoder decodeObjectForKey:@"userImg"]];
        _FriendsList = [decoder decodeObjectForKey:@"FriendsList"];
        _userID = [decoder decodeObjectForKey:@"userID"];
        _groups = [decoder decodeObjectForKey:@"groups"];
    }

    return self;
}

朋友:

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];
    if (self) {
        _username = [decoder decodeObjectForKey:@"friendUN"];
        _userImg = [UIImage imageWithData:[decoder decodeObjectForKey:@"friendIMG"]];
        _userID = [decoder decodeObjectForKey:@"friendID"];
    }

    return self;
}