Frage

How to Change or Update NSMuttableDictionary i apply code below

in User.h file the code

@interface User : NSObject

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

then in User.m file the code

#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

in my .pch file

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

then i got the all key and value like this

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

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

Now my question is How to update

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

If anybody know this plz give me some answer to solve my query

Thanks in Advanced!!!

War es hilfreich?

Lösung

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

Andere Tipps

Why don't you write a method to update your properties like

-(User *)updateWithDictionary:(NSDictionary *)dictionary
{
    if (self)
    {        
        self.userID = [dictionary objectForKey:@"IdUser"];
        self.avatar = [dictionary objectForKey:@"Avatar"];
        self.firstname = [dictionary objectForKey:@"FirstName"];
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top