我需要获取用户提交的信息,将该信息存储在NSMutableDictionary中,然后将该NSMutableDictionary存储在另一个NSMutableDictionary中,然后在另一个类中进行编码。无论出于何种原因,我似乎无法将第一个NSMutableDictionary存储在另一个中。
由于工作规则,我不得不缩减这里的代码,如果它似乎缺少任何东西,那就很抱歉。我只发布了我遇到问题的部分。
UserInfo.h:
#import <Foundation/Foundation.h>
@interface MyPlanInfo : NSObject <NSCoding>
@property (nonatomic, strong) NSMutableDictionary *emergencyDictionary;
@end
UserInfo.m:
#import <Foundation/Foundation.h>
#import "MyPlanInfo.h"
static NSString *emergencyDictionaryKey = @"emergencyDictionaryKey";
@implementation MyPlanInfo
@synthesize emergencyDictionary;
- (id) initWithCoder:(NSCoder *)coder
{
self = [super init];
self.emergencyDictionary = [coder decodeObjectForKey:emergencyDictionaryKey];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.emergencyDictionary forKey:emergencyDictionaryKey];
}
@end
infoView.h
#import <UIKit/UIKit.h>
#import "MyPlanInfo.h"
@interface infoView : UIViewController <NSCoding>
{
NSMutableDictionary *emergencyContactInfo;
NSArray *userInfo;
NSArray *userKeys;
NSMutableArray *tempArray;
}
@property (nonatomic, strong) MyPlanInfo *myPlanInfoObject;
-(void)saveUserInfo;
-(void)loadUserInfo;
@end
infoView.m:
#import "infoView.h"
@interface infoView ()
@end
@implementation infoView
static NSString *userInfoKey = @"userInfoKey";
static NSString *userName;
-(void)viewDidLoad
{
[super viewDidLoad];
if(!self.myPlanInfoObject)
{
self.myPlanInfoObject = [[MyPlanInfo alloc] init];
}
[self loadUserInfo];
}
-(void)addToDictionary
{
emergencyContactInfo = [NSMutableDictionary dictionaryWithObjects:userInfo forKeys:userKeys];
if([userInfo count] != 0 || userInfo == nil)
{
self.myPlanInfoObject.emergencyDictionary = [NSMutableDictionary dictionaryWithObject:emergencyContactInfo forKey:userName];
}
[self saveUserInfo];
}
- (void)saveUserInfo
{
NSData *userInfoData = [NSKeyedArchiver archivedDataWithRootObject:self.myPlanInfoObject];
[[NSUserDefaults standardUserDefaults] setObject:userInfoData forKey:userInfoKey];
}
- (void)loadUserInfo
{
NSData *userInfoData = [[NSUserDefaults standardUserDefaults] objectForKey:userInfoKey];
if(userInfoData)
{
self.myPlanInfoObject = [NSKeyedUnarchiver unarchiveObjectWithData:userInfoData];
}
}
@end
在infoView.m中,在addToDictionary方法中,userInfo是一个用户输入信息的数组,而userKey是一个key的数组。 emergencyContactInfo NSMutableDictionary工作正常,一切都在其中,但是当我尝试将其设置为新的NSMutableDictionary中的对象时,对于一个键,它不起作用。一切都是零。
任何人对我做错了怎么想?
编辑:如果你投票,请留下原因,以便我可以避免做任何我以后做错的事。
答案 0 :(得分:1)
在以下行中,您使用普通MyPlanInfo
/ alloc
创建init
的实例:
self.myPlanInfoObject = [[MyPlanInfo alloc] init];
但是,至少在提供的代码中,您没有在init
中覆盖MyPlanInfo
,而是覆盖initWithCoder:
:
- (id) initWithCoder:(NSCoder *)coder
{
self = [super init];
self.emergencyDictionary = [coder decodeObjectForKey:emergencyDictionaryKey];
return self;
}
当您只使用普通init
时,MyPlanInfo
的{{1}}实例变量将为emergencyDictionary
。您可能会向nil
添加以下内容以覆盖MyPlanInfo
:
init
这将确保新创建的- (id) init
{
if ((self = [super init])) {
emergencyDictionary = [[NSMutableDictionary alloc] init];
}
return self;
}
实例具有可以从其他类操作的正确MyPlanInfo
。