Should I use NSUserDefaults or a plist to store data?

后端 未结 9 1443
北海茫月
北海茫月 2020-11-28 19:17

I will be storing a few strings (maybe 10-20). I am not sure if I should use NSUserDefaults to save them, or write them out to a plist. What is considered best practice? NSU

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 20:03

    NSUSerDefaults is indeed quick to implement, but mostly as your application grows, you want to store more and more, I went directly for plist files.

    Mostly, people want to store a list of something, so here is my share on how to do this with NSDictionary. This does not require you to create a plist file first, it will be created at the first time saving something

    xcode 7 beta, Swift 2.0

    saving

    func SaveItemFavorites(items : Array) -> Bool
    {
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
        let docuDir = paths.firstObject as! String
        let path = docuDir.stringByAppendingPathComponent(ItemFavoritesFilePath)
        let filemanager = NSFileManager.defaultManager()
    
        let array = NSMutableArray()        
        
        for var i = 0 ; i < items.count ; i++
        {
            let dict = NSMutableDictionary()
            let ItemCode = items[i].ItemCode as NSString
            
            dict.setObject(ItemCode, forKey: "ItemCode")
            //add any aditional..
            array[i] = dict
        }
        
        let favoritesDictionary = NSDictionary(object: array, forKey: "favorites")
        //check if file exists
        if(!filemanager.fileExistsAtPath(path))
        {
            let created = filemanager.createFileAtPath(path, contents: nil, attributes: nil)
             if(created)
             {
                 let succeeded = favoritesDictionary.writeToFile(path, atomically: true)
                 return succeeded
             }
             return false
        }
        else
        {
            let succeeded = notificationDictionary.writeToFile(path, atomically: true)
            return succeeded
        }
    }
    

    Little note from the docs:

    NSDictionary.writeToFile(path:atomically:)

    This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

    So whatever you set at dict.SetObject() should be one of the above mentioned types.

    loading

    private let ItemFavoritesFilePath = "ItemFavorites.plist"
    
    func LoadItemFavorites() -> Array
    {
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
        let docuDir = paths.firstObject as! String
        let path = docuDir.stringByAppendingPathComponent(ItemFavoritesFilePath)
        let dict = NSDictionary(contentsOfFile: path)
        let dictitems : AnyObject? = dict?.objectForKey("favorites")
        
        var favoriteItemsList = Array()
        
        if let arrayitems = dictitems as? NSArray
        {
            for var i = 0;i

提交回复
热议问题