0

I am trying to save an array of a user-created class named "TopTenData". The code I used follows. Running the code indicates the file is created, but the file is NOT created. When I navigate to the path I do not find the desired file. Any help would be appreciated.

func writeArrayToPlist(array: [TopTenData]) {
    if let arrayPath: String = createArrayPath() {
        (array as NSArray).writeToFile(arrayPath, atomically: false)
        print("Array written successfully")   
        print(arrayPath)   // navigating this path shows no files
    }
}

func createArrayPath () -> String? {
    if let docsPath: String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last {
        return ((docsPath as NSString).stringByAppendingPathComponent("myTopTenData") as NSString).stringByAppendingPathExtension("plist")
    }
    return nil
} 
Gil
  • 101
  • 6

1 Answers1

0

writeToFile method of NSArray has some limitations. For example it only support these data types:

  • NSString
  • NSData
  • NSDate
  • NSNumber
  • NSArray
  • NSDictionary

For custom data types you can use NSKeyedArchiver and NSKeyedUnarchiver

See this post

Community
  • 1
  • 1
Muhammad Zeeshan
  • 2,441
  • 22
  • 33
  • Thanks, you steered me in the correct direction. I found this demo from Apple to be very helpful. https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson10.html – Gil Feb 17 '16 at 01:51
  • If this solves your problem then please mark it as correct. Thank you. – Muhammad Zeeshan Mar 16 '16 at 05:21