0

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;
}
8
  • from where you want to update the userdata, please include that part also. Commented Feb 21, 2019 at 7:12
  • you should create mutable copy of your array you want to append, Now append element in newArray and then reAssign old array with you new array :) Commented Feb 21, 2019 at 7:12
  • @Akhilrajtr as same method only i need to update with new class id and cureent date Commented Feb 21, 2019 at 7:15
  • @AbuUlHassan where I need to create a mutable copy Commented Feb 21, 2019 at 7:16
  • on what place you want to append? Commented Feb 21, 2019 at 7:18

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

7 Comments

Hi the app crashing after I did like this [__NSCFDictionary addObject:]: unrecognized selector sent to instance. In the line addObject
Check if your addObject variable is nil, it could be the reason.
Did you try my approach? This error your getting because your dictionary is not mutable.
@Lije edit your question, i will help you, add the current code what you are trying?
hi I updated my question currently doing in last flow
|
0

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.

6 Comments

so before api call I need to get all userData right?
When you are ready to store data in user defaults then you need to do it.
convert it in mutable array of dictionaries which mean MutableDictionary right?
userData is an array which contains multiple dictionaries. First userData will be converted in mutable array and append a dictionary in userData array.
hi crashed [__NSCFDictionary addObject:]: unrecognized selector sent to instance I
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.