0

I'm trying to save CNContact details into my UserDefaults but it fails. Any other way I could save CNContact ?

struct User {
    var firstName: String = ""
    var lastName: String = ""
    var email: String = ""
    var phoneNumber: String = ""
    var company: String = ""
    var assistantContact: CNContact?

    init(firstName: String, lastName: String, email: String, phoneNumber: String, company: String, assistantContact: CNContact? = nil) {
        self.firstName = firstName
        self.lastName = lastName
        self.email = email
        self.phoneNumber = phoneNumber
        self.company = company
        self.assistantContact = assistantContact
    }

    func encode() -> Dictionary<String, AnyObject> {

        var dictionary : Dictionary = Dictionary<String, AnyObject>()
        dictionary["firstName"] = firstName as AnyObject?
        dictionary["lastName"] = lastName as AnyObject?
        dictionary["email"] = email as AnyObject?
        dictionary["phoneNumber"] = phoneNumber as AnyObject?
        dictionary["company"] = company as AnyObject?
        dictionary["assistantContact"] = assistantContact as AnyObject?
        return dictionary

    }

    init(dictionary: Dictionary<String, AnyObject>) {
        if let firstName = dictionary["firstName"] as? String {
            self.firstName = firstName
        }
        if let lastName = dictionary["lastName"] as? String {
            self.lastName = lastName
        }
        if let email = dictionary["email"] as? String {
            self.email = email
        }
        if let phoneNumber = dictionary["phoneNumber"] as? String {
            self.phoneNumber = phoneNumber
        }
        if let company = dictionary["company"] as? String {
            self.company = company
        }
        if let assistantContact = dictionary["assistantContact"] as? CNContact {
            self.assistantContact = assistantContact
        }
    }

}
Nitesh
  • 1,564
  • 2
  • 26
  • 53
  • Code you wrote to save that contact in defaults that crashes please show once – iOS Geek Oct 31 '17 at 05:06
  • 1
    From the documentation for `UserDefaults`: *The UserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list—that is, an instance of (or for collections, a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData. For more details, see Preferences and Settings Programming Guide.* `CNContact` is not listed. – rmaddy Oct 31 '17 at 05:08
  • @iOSGeek UserDefaults.standard.set(currentUser.encode(), forKey: AppConstants.kLoggedInUser). it crashes on currentUser.encode() – Nitesh Oct 31 '17 at 05:09
  • Did you tried saving User.Encode() Reference instead of Directly saving it to Defaults ? As let dict : Dictionary = "Value" and then save – iOS Geek Oct 31 '17 at 05:23
  • @iOSGeek No. How exactly ? – Nitesh Oct 31 '17 at 05:26
  • you are directly saving A value being returned by a function As currentUser.encode() Instead First take its value to a new dictionary as I wrote let dict : Dictionary = currentUser.encode() and now save this as " UserDefaults.standard.set(dict, forKey: "iosgeek") " – iOS Geek Oct 31 '17 at 05:37
  • @iOSGeek saving it as new dic didn't worked. – Nitesh Oct 31 '17 at 06:01
  • I assume from the code sample, you want to save parts of `CNContact` into an `NSDictionary`. Have you gotten the simplest case version working, just one name field? The reason I ask, is I see a the lines about various variables, but I don't see them tied into your assistantContact. – benc Nov 21 '18 at 04:45
  • I would do this only if you know the contacts won't have any images. Pls see https://cocoacasts.com/ud-9-how-to-save-an-image-in-user-defaults-in-swift on why images in UserDefaults is not a good idea. – benc Mar 07 '23 at 04:22

1 Answers1

0

First Encode Contacts Array using.

let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: contactsArray)

Now save encodedData in UserDefaults.

For decode contacts array,

if let contacts: [CNContact] = decodedData as? [CNContact] {
            //Decoded Contacts Array
 }
Satish Babariya
  • 3,062
  • 1
  • 15
  • 29