简体   繁体   English

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

[英]Problems Saving NSMutableArray with custom objects

I have a class called CurrentUser , which holds a NSMutableArray called listOfFriends . 我有一个名为CurrentUser的类,其中包含一个名为listOfFriendsNSMutableArray This listOfFriends holds objects from the class Friends . 这个listOfFriends拥有Friends类中的对象。

Both CurrentUser and Friend classes have the NSCoding protocols handled. CurrentUserFriend类都处理了NSCoding协议。 The Friend object has a property called userImg , which is a UIImage . Friend对象具有一个名为userImg的属性,它是一个UIImage There's just one problem. 只有一个问题。 When I try to load the lisOfFriends , the Friends don't come with their UIImages . 当我尝试加载lisOfFriends ,Friends不附带其UIImages

If I save any friend, and try to load it back again, it comes with the UIImage with no problem. 如果我保存了任何朋友,并尝试再次将其加载回去,则UIImage附带了它,没有问题。 But if the object is inside the listOfFriends , it doesn't come with the UIImage , but all other informations are loaded correctly. 但是,如果对象在listOfFriends内部,则它不随UIImage ,但所有其他信息均已正确加载。

My question then is: How can I store and load a NSMutableArray with custom objects? 那么我的问题是:如何存储和加载带有自定义对象的NSMutableArray Declaration of the friendList: 好友声明:

CurrentUser.h CurrentUser.h

@property (nonatomic,strong)NSMutableArray *FriendsList;

CurrentUser.m CurrentUser.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;
}

Friend.m 朋友

- (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;

}

This is the object I have to save and load the CurrentUser, It's called "Configurations". 这是我必须保存和加载CurrentUser的对象,它称为“配置”。 Configurations.h 配置文件

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

@end

Configurations.m 配置文件

#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

This is how I'm loading the CurrentUser. 这就是我加载CurrentUser的方式。

saver = [[configurations alloc]init];
currentUser *tmpUser = [saver loadCustomObject];
thisUser = [currentUser instance];

static dispatch_once_t once;
dispatch_once(&once, ^ {
        if(tmpUser != nil){
            if(![tmpUser.username isEqualToString:@""]){


            for(Friend *savedFriend in tmpUser.FriendsList)
            {
                [thisUser addFriend:savedFriend];
            }
                thisUser.username = tmpUser.username;
                thisUser.email = tmpUser.email;
                thisUser.userID = tmpUser.userID;
                thisUser.userPSW = tmpUser.userPSW;
                thisUser.userImg = tmpUser.userImg;
});

Your initWithCoder: methods are incorrect. 您的initWithCoder:方法不正确。 They need to be in the following pattern: 它们需要采用以下模式:

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

Fixing the initWithCoder: method for both your CurrentUser and Friend classes will help a lot. CurrentUserFriend类修复initWithCoder:方法将大有帮助。

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

Friend: 朋友:

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

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

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