2

I have a simple swift app that pulls JSON information from online and uses it in a table view. For example, a document would have a name, description, etc. As well as an url to display the pdf. I know how to open the PDF from both local storage and an online url, but if I had an "available offline" value that could be either true or false, where would I store it?

I cannot put it as a JSON key because then it would change the setting for all users accessing the online JSON files, so where do I put a simple device specific option such as this?

Mike Schmidt
  • 945
  • 3
  • 12
  • 31

2 Answers2

2

NSUserDefaults is great for easy tiny-scale storage, like remembering settings. It works like this:

[[NSUserDefaults standardUserDefaults] setObject:@"object" forKey:@"this is my key"];

Then, later,

[[NSUserDefaults standardUserDefaults] objectForKey:@"this is my key"];
//this gives you your object

You just have to be careful with typing, otherwise your app will crash.

Here's the docs for NSUserDefaults: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/

xǝlɐ
  • 63
  • 1
  • 11
  • This question is tagged "swift". Your solution may have value, but is not written in the language requested by OP, so it's off-topic. Please provide an answer in Swift. Thank you. – Eric Aya Jul 20 '16 at 08:48
  • It's a basic Cocoa API question; "Swift" is coincidental. This fully answers the question. – gnasher729 Jul 20 '16 at 08:57
  • Yes it's about Cocoa, but it's tagged "Swift", not "Objective-C" or "JavaScript" or "Ruby": the answers should use Swift. If you really believe the question is language-agnostic, then *edit the question and remove the language tag*. But keeping this tag and using a different language is just wrong. – Eric Aya Jul 20 '16 at 09:08
2

NsUserDefault is the best choice for you. You have to store single variable so.

Coredata is used to small tiny database like if you want to store your data like name , description than you can use coreData.

Here you simple store with NSUserDefaults

Edited:- To store:-

let value = NSUserDefaults.standardUserDefaults()
value.setInteger (10,forKey: "value")

// here you can use setBool setDouble etc.

To retrieve:-

let num = value.objectForKey ("value") as? Integer
Dhruv Khatri
  • 803
  • 6
  • 15