更改或更新NSMutableDictionary

时间:2014-01-27 06:01:23

标签: ios iphone objective-c nsobject

如何更改或更新NSMuttableDictionary我将应用以下代码

在User.h文件中

代码

@interface User : NSObject

@property (nonatomic, strong) NSNumber *userID;
@property (nonatomic, strong) NSString *avatar;
@property (nonatomic, strong) NSString *firstname;
-(User *)initWithDictionary:(NSDictionary *)dictionary;
@end

然后在User.m文件中输入代码

#import "User.h"

@implementation User

@synthesize userID;
@synthesize avatar;
@synthesize firstname;
-(User *)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if (self)
    {        
        self.userID = [dictionary objectForKey:@"IdUser"];
        self.avatar = [dictionary objectForKey:@"Avatar"];
        self.firstname = [dictionary objectForKey:@"FirstName"];
}
    return self;
}

@end

在我的.pch文件中

#define AppDelegateInstance ((AppDelegate *)[UIApplication sharedApplication].delegate)

然后我得到了所有的关键和价值

AppDelegateInstance.loggedUser = [[User alloc] initWithDictionary:[tempArray objectAtIndex:0]];

Response == (
        {
            IdUser = 1;
            Avatar = "Nishant_1.jpg
            FirstName = Nishant;
        }
     )

现在我的问题是如何更新

 {
     Avatar = "Nishant_1.jpg(Not update **Nishant_1.jpg** to **xyz.jpg**)
     FirstName = Nishant(Not Update **Nishant** to **xyz**);
 }

如果有人知道这个PLZ给我一些答案来解决我的问题

感谢高级!!!

2 个答案:

答案 0 :(得分:0)

为什么不写一个方法来更新你的属性,比如

-(User *)updateWithDictionary:(NSDictionary *)dictionary
{
    if (self)
    {        
        self.userID = [dictionary objectForKey:@"IdUser"];
        self.avatar = [dictionary objectForKey:@"Avatar"];
        self.firstname = [dictionary objectForKey:@"FirstName"];
}

答案 1 :(得分:0)

- (void)updateWithDictionary:(NSDictionary *)dictionary
{
    id userId = [dictionary objectForKey:@"IdUser"] ;
    if (userId) {
        self.userID = userId ;
    }
    id avatar = [dictionary objectForKey:@"Avatar"] ;
    if (avatar) {
        self.avatar = avatar ;
    }
    id firstname = [dictionary objectForKey:@"FirstName"];
    if (firstname) {
        self.firstname = firstname ;
    }
}