1

I am trying to store tuple in NSUserDefaults. My tuple is below.

var primaryNumberAndName: [(name:String , number: String, numberType: String, imageProfile: UIImage, imageLogo: UIImage)] = []

Adding datas to tuples

for (var i : Int = 0; i < primaryNameArr.count; i++)
        {
          primaryNumberAndName.append(name: primaryNameArr[i] as! String, 
number: primaryNumberArr[i] as! String,  
numberType: primaryNumberTypeArr[i] as! String, 
imageProfile: UIImage(named: "profile_man.png")!, 
imageLogo: UIImage(named: "Our_Logo.png")!)

        }

Coding:

var dictOfObjectsData = NSKeyedArchiver.archivedDataWithRootObject(primaryNumberAndName)
myUserDefaul.setObject(dictOfObjectsData, forKey: "PrimaryContacts")

Error:

Cannot invoke 'archivedDataWithRootObject' with an argument list of type {[(name:String , number: String, numberType: String, imageProfile: UIImage, imageLogo: UIImage)]}

Kindly guide me how to solve this.

McDonal_11
  • 3,935
  • 6
  • 24
  • 55

1 Answers1

1

One option could be to convert tuple into Dictionary before saving it. Something like this:

let primaryNumberKey = "primaryNumber"
let nameKey = "name"

func serializeTuple(tuple: AccessTuple) -> myDictionary {
    return [
        primaryNumberKey : tuple.primaryNumber,
        nameKey : tuple.name
    ]
}

func deserializeDictionary(dictionary: myDictionary) -> myTuple {
    return myTuple (
        dictionary[primaryNumberKey] as String!,
        dictionary[nameKey] as String!
    )
}

This is how you can use it:

// Writing to defaults
let myConvertedDictionary = serializeTuple(myTuple)
NSUserDefaults.standardUserDefaults().setObject(myConvertedDictionary, forKey: "UserDetailslKey")

// Reading from defaults
let myFetchedDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey("UserDetailslKey") as myDictionary
let myFetchedTuple = deserializeDictionary(myFetchedDictionary)

You can now archive Dictionary the regular way!

Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • Now, If I print my tuples,, It will show 100 records. That I need to store in user defaults. Is it possible with the above code?? – McDonal_11 Oct 26 '15 at 14:40
  • How can I convert all datas in to Dictionary?? I dont know logic in loop. Pls guide me @Abhinav – McDonal_11 Oct 26 '15 at 14:45
  • You mean you have a tuple with 100 parameters in it? – Abhinav Oct 26 '15 at 14:55
  • I would advise you to convert array of tuples to array of dictionaries by using the code I shared. Call `serializeTuple` in `for` loop to convert one tuple at a time into dictionary and add it in a new Array. Now, you can save array of dictionaries into `NSUserDefaults`. If you need to know how to do that you can take a look at this [blog](http://www.scriptscoop.net/t/eed05a07a8de/ios-swift-save-array-of-dictionaries-into-nsuserdefaults.html). – Abhinav Oct 26 '15 at 15:10
  • Thr?? Any way to sort the above Tuples? – McDonal_11 Oct 27 '15 at 10:44
  • Did you try with my above comment? – Abhinav Oct 27 '15 at 10:45
  • That only I am doing. In bw, I have to sort datas. So I asked u. – McDonal_11 Oct 27 '15 at 10:47