How can I store my data locally using iOS + Swift?

I am new to Swift iOS development . I want to save my data (name, pap_image) locally, while my application cannot download it (without an internet connection, etc.). I started my work before two days, but so far I have not received any good result for my problem. I also worked in the Realm and SQlite database, but still have not achieved proper success. Please help me, provide me with the correct stream for storing data (name, path_ image) in the local database. Thank.

+3
source share
2 answers

In iOS, there are basically five options:

  • Core Data
  • Third party database ( Realmetc.)
  • NSKeyedArchiver/NSCoding
  • NSUserDefaults
  • Keychain Services

1 (Basic data) is best when you want to save large amounts of data in a structured format, for example: your contacts / address book, music library.

2 (Realm / 3rd Party DB database) is applicable in similar scenarios to 1. The main data may be a bit dense / turgid to use, so there are third-party abstractions / alternatives.

3 (NSKeyedArchiver) is a rather interesting option; it effectively allows you to “freeze” objects (which correspond to the protocol NSCoding) and store them on disk. This is great if your data is presented as a custom object model.

4 (NSUserDefaults) microdata/. , : , , .

5 ( Keychain) ( /)

, , , Core Data NSKeyArchives UIView.

. : NSCoding, , Keychain.

+15

NSUserDefaults ::

func getPrefNameInt(key: NSString,value: Int) ->Int{
    let prefDefault = NSUserDefaults.standardUserDefaults()
    if let keyVal: AnyObject = prefDefault.objectForKey(key as String){
        return keyVal as! Int
    }else{
        return  value
    }
}
func updatePrefInt(key: NSString,value: Int){
    let prefDefault = NSUserDefaults.standardUserDefaults()
    prefDefault.setInteger(value, forKey: key as String)
}
-2

Source: https://habr.com/ru/post/1652335/


All Articles