0

This has to be easy and I'm just missing something. I have a function in my application that is posting to a server, getting a response, and turning that response into an NSDictionary, and then turning one of the values sets in that NSDictionary into an NSArray. I need to access the values in this array outside of the scope of the function that created them. How can I make a json response available to my whole application if it is created within a specific function? Thanks!

Dan Beaulieu
  • 17,926
  • 15
  • 92
  • 126
  • 3
    If you want to access data throughout the application, you need to persist the response. You can use `NSUserDefaults` or CoreData or may be a singleton to share data. – GoodSp33d Jan 04 '16 at 13:40

1 Answers1

1

There's a few ways you can do this, one of them is, as @GoodSp33d mentioned, NSUserDefaults another way is by using a completion handler and if you are open to using singletons, that's an option as well.

NSUserDefaults

// settings
var jo : [NSObject : AnyObject] = [
    "a" : "1.0",
    "b" : "2.0"
] 
// store your value
userDefaults.setObject(jo, forKey: akey)
// optionally synchronize
var isOk = userDefaults.synchronize()

// safely retrieve your value
if let data0 = userDefaults.dictionaryForKey(akey) {
    print(data0)
} else {
    print("not set")
}

(source: Martin R)

note: this method actually persists the data to local storage making it available across multiple sessions

@diatrevolo added: It should be stated for consistency purposes that NSUserDefaults are not always persistent across platforms. tvOS user defaults, for example, get purged by the system

Singleton Method

There are a lot of people, myself included, that prefer not to use this approach but other consider this perfectly valid. This is the singleton approach. Setting your value to this property makes it globally available within your app.

struct sharedResult {
    static var sharedDict: NSDictionary = nil
}

(more singleton approaches)

note: this method holds your data for the session only.

Completion Handler

However, I personally like to wrap my HTTP Requests in a closure / completion handler and when I have my object back from the server, I pass it into the completion handler.

// definition
func HTTPConnect(completion: (result: Bool)->()) {  
 // make http request {
    // once you have your result pass it back
    completion(result: myResult)
 // }
}

// call from where you need the data
HTTPConnect() { (result) -> () in
    // do stuff with the result
    print(result)
}

Note: this method doesn't hold your data at all. It does, however, make it easy for you to pass your value from one controller to another.

Community
  • 1
  • 1
Dan Beaulieu
  • 17,926
  • 15
  • 92
  • 126
  • Im using NSUserDefaults for a userId since i want it to persist until the user deletes the app. I'm going to try that and see if they update correctly. Thanks! – Christopher Butterfield Jan 04 '16 at 17:09
  • 2
    It should be stated for consistency purposes that NSUserDefaults are not always persistent across platforms. tvOS user defaults, for example, get purged by the system. – diatrevolo Jan 04 '16 at 17:46
  • So on tvOS when you reboot your system you lose your NSUserDefault storage? I'll update my answer, thank you – Dan Beaulieu Jan 04 '16 at 17:48