-1

Is there a way I can cast this Swift data set in someway to a form that is acceptable to NSUserDefauts? i.e. NSObject NSSet? (p.s. I realize NSUserDefaults isn't for this type of data, but I'm just testing)

struct Users {
        var name: String = ""
        var stores: [Store]
    }

    struct Store {
        var name: String = ""

        var clothingSizes = [String : String]()

    }

    init() {
        let userDefaults = NSUserDefaults.standardUserDefaults()

        if let usersPeople = userDefaults.valueForKey("Users") as?
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
GarySabo
  • 5,806
  • 5
  • 49
  • 124
  • 1
    Pretty sure your are limited in what types you can store in Swift: http://stackoverflow.com/a/2315972/620197 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html#//apple_ref/occ/instm/NSUserDefaults/setObject:forKey: – Mike D Jun 04 '15 at 15:31
  • But the questions is about casting, right? – Dima Deplov Jun 04 '15 at 15:47
  • Yes, the examples I've seen always store arrays or arrays of arrays in NSUserDefaults Core Data etc....my data structure, the Store object particularly, is a string associated with an array of a dictionary, which is what I can't figure out how to store in a non-Swift API like NSUserDefults. – GarySabo Jun 04 '15 at 17:16
  • @PSUSabes but, NSUserDefaults has Swift API. You need to save and then read your data, right? Then you just need to use, `setObject:forKey:` for setting and `objectForKey` for reading. Look at my answer and try it. – Dima Deplov Jun 04 '15 at 21:36
  • Thanks @flinth but I get NSUSerDefaults does not have a member named setDictionary – GarySabo Jun 04 '15 at 21:41
  • @PSUSabes yes, that's why you can use `setObject:forKey`. I edit my question – Dima Deplov Jun 04 '15 at 21:43

2 Answers2

2

I think you can use Dictionary. You'll need to make method to wrap data to struct and vice versa.

For example:

var users : [String: AnyObject]()
users["name"] = "SomeName"
users["stores"] = yourStoreArray
NSUserDefaults.standardUserDefaults().setObject(users, forKey: "Users")

something like that.

And when you need to get struct

if let myDictionaryFromUD = userDefaults.objectForKey("Users") as? [String:AnyObject]{
   self.users = Users(myDictionaryFromUD["name"], stores: myDictionaryFromUD["stores"] as! [Store]) 

}

I aslo assume that you will save to userDefaults array of Users. In this case, you will save [[String: AnyObject]] but mechanics the same.

Dima Deplov
  • 3,688
  • 7
  • 45
  • 77
  • stores isn't just an array though, it has a nested dictionary in it? – GarySabo Jun 04 '15 at 17:12
  • @PSUSabes in my example stores is an array. My example is based on your question and stores is array of Store struct in your case. It can be Dictionary too. – Dima Deplov Jun 04 '15 at 17:44
0

I don't know that this is the best way to do this but you can try this.

struct Users {
        var name: String = ""
        var stores: [Store]
    }

    struct Store {
        var name: String = ""

        var clothingSizes = [String : String]()

    }

var Advert = [Users]()

init() {
        NSUserDefaults.standardUserDefaults().setObject(Advert[0].name, forKey: "NameYouWant")
}
Bishan
  • 401
  • 1
  • 9
  • 16