Lije
Lije

Reputation: 45

Add new array object in existing key value without change - Objective C

In our application, when user register first time I need to save user key and its value. So first time I am created successfully like this format;

key: "ayi927GHt548"
 data: {
 userdata: [
    {classId: "001", updateDate:"2018/02/12"}
 ]}

for this I get the class id and update date like this; here is the code

 -(void)someMethod {
   // ... do the process to get user class ID and user key
   userClassID = @"001" // string declared global
   userKey = @"ayi927GHt548" // string declared global

   [apiManager saveUserInformationwithKey:userKey andValue:[self saveCurrentUserIformationIntoUserInfoAPI]];

 }

   -(NSDictionary *)saveCurrentUserInformationIntoUserInfoAPI {

      NSDictionary *dictionaryUserInfo = @{
      @"userData": @[
          @{
              @"classId": userClassID, // I get user class id 
              @"updateDate": [self getCurrentDateTime]
           }
        ]
     };
  return dictionaryUserInfo;
}

// in global apimanager class i have a method to added on key value for get final solution

-(void)saveUserInformationwithKey:(NSString *)key andValue:(NSDictionary *)value {
     NSDictionary *authInform = @{
        @"key": key,
        @"value": value
     };

   // call api for post method
 }

this much I did correctly as what I want.

but the problem is for another case I need to append the new value to same existing 'userdata', without change the previous one, that is look like;

   key: "ayi927GHt548"
   data: {
       userdata: [
            {classId: "001", updateDate:"2019/02/18"},
            {classId: "003", updateDate:"2019/02/21"} //? here this is the new object is which i want to add
      ]
   }

how can I get like this? please help

currently Iam doing like this

    NSMutableArray *dataFinal =  [NSMutableArray arrayWithObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"data"]];

    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:5] ;
    [dict setObject:@"54455" forKey:@"classId"]; 
    [dict setObject:[self getCurrentDateTime] forKey:@"updateDate"]; 
    [dataFinal addObject:dict];
    NSLog(@"Final: %@", dataFinal);

and the response look like this, but I need to append inside userdata

{
    key = 12345;
    data =         {
        userdata =             (
                            {
                updateDate = "2019/02/21";
                classId = 0002;
            }
        );
    };
},
    {
    updateDate = "2019/02/21";
    classId = 54455;
}

Upvotes: 0

Views: 747

Answers (2)

Harjot Singh
Harjot Singh

Reputation: 575

This is how you can get the value and append it to the array later.

NSMutableArray *userData = [dictionaryInfo objectForKey:@"userData"]; 
NSDate *yourdate = date; 
NSNumber *classID = classID; 
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:5];
[dict setObject:classID forKey:@"classId"]; 
[dict setObject:yourdate forKey:@"updateDate"]; 
[userData addObject:dict];

I am away from my mac, please kindly correct semantic errors yourself. :)

EDIT: I have added the code to be in your userData, try to add this line and tell me?

dataFinal = [[[[NSUserDefaults standardUserDefaults] objectForKey:@"data"] valueForKeyPath:@"data. userdata"]mutableCopy];

Print the values and let me know. Hope it helps!

Upvotes: 1

Mahendra
Mahendra

Reputation: 8914

Firstly you need to get userData array and then convert it in mutable array of dictionaries.

Then you need to append the required data in userData and then save back to user defaults.

Upvotes: 0

Related Questions