IOS Data Persistence Nsuserdefaults

Source: Internet
Author: User

Original blog, reproduced please specify
Blog.csdn.net/hello_hwc

The data persistence storage of iOS is divided into many kinds

    • Normal file, plist
    • Nsuserdefaults
    • CoreData
    • KeyChain

This article explains the use of nsuserdefaults

Standarduserdefaults

Most of the time, Standarduserdefaults is used to save the data, which is a shared userdefaults, which means that the same userdefaults is accessed anywhere in the program.
Write Data
Swift Code

        var userdefaults = NSUserDefaults.standardUserDefaults()        userdefaults.setBool(truefor"KEYBOOL")        userdefaults.setObject("String"for"KSTRING")

Note that the data written by the above code is not immediately synced to the file, but is saved in the cache, which increases the rate (the file IO is inefficient). If you want to sync now

        userdefaults.synchronize()

If it is not very necessary to synchronize to the database, it is recommended not to call the synchronous method every write, because it is relatively slow.

Read the data

            var str = userdefaults.objectForKey("KSTRING"as? String            varbool =  userdefaults.boolForKey("KEYBOOL")
Userdefaults for App Group services

Such userdefaults are shared and used in the following two scenarios

    • App Suite, which means you need to share data between two apps
    • App extension, sharing data between extension and his contain app. Typical is the Input method program, in the contain app settings, the keyboard style will change.

How to use-take extension as an example
Writing data in the contain app

OC Code

NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:@"group.yourcompanyname.yourappname"];    setforKey:@"KDICFORWIDGET"];    setObject:@"1234"forKey:@"KSTRINGFORWIDGET"];    [shared synchronize];

Read data in extension

NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:@"group.yourcompanyname.yourappname"];NSMutableDictionary *dic = [shared objectForKey:@"KDICFORWIDGET"];NSString *str = [shared objectForKey:@"KSTRINGFORWIDGET"]

IOS Data Persistence Nsuserdefaults

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.