not able to understand that how to store value in userdefaults

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


not able to understand that how to store value in userdefaults



i am new to swift and i want to know that from my response i want to store user id in userdefaults please any one help


data = {
"access_token" = b332236bb2c9a2a5e35452bff62f76ac;
"contact_no" = 847334868;
email = "jhdsjhs@gmail.com";
"fk_user_id" = 125;
"is_active" = 1;
photo = "usr_jbdcm1533033074.png";
"user_name" = schsjcsacksjah;
"user_role" = 2;
"user_type" = 0;
};
message = "Customer registered successfully!";
success = 1;
}



i want to store fk_user_id in user defaults please can anyone help





Are you able to fetch fk_user_id from your response ? Saving it is just one search and one click on google.
– Sharad Chauhan
9 mins ago





So post the code you've written that tries to extract this value and save it to UserDefaults. We'll help you debug it.
– Duncan C
8 mins ago


UserDefaults





yes i am getting in response but not able to understand how to store
– Vishal
8 mins ago







Your question is too broad : you're asking how to do something very common in iOS instead of just asking help for a specific problem you are having. First try to do some research and write some code, and if you encounter a problem while doing it, then you can ask for help here. A good starting point : developer.apple.com/documentation/foundation/userdefaults
– Guillaume Algis
7 mins ago





From a Swift collection type output it cannot be determined if a value is integer or string.
– vadian
4 mins ago


integer


string




2 Answers
2


let user_id = (dict["data"] as! Dictionary)["fk_user_id"]
UserDefaults.standard.set(user_id, forKey: "fk_user_id")
UserDefaults.synchronize()



try this code and I hope it will help you.





fk_user_id is not string.
– Kuldeep
5 mins ago


fk_user_id





@Kuldeep How do you know? Of course it can be string
– vadian
3 mins ago





As I know that Int value not comes with " ".
– Kuldeep
28 secs ago


" "



You have to convert int(125) value to NSNumber first, then you can store it in userdefaults


let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(valueInNsnumber, forKey:"fk_user_id")





No, in Swift 3+ you have not. Swift 2 is dead.
– vadian
54 secs ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived