9. IOS programming using Nsuserdefaults Read, save application parameters

Source: Internet
Author: User

1. Meet Nsuserdefaults

Usually we may not leave the application, directly in the application to set configuration parameters, etc., this need to use nsuserdefaults, and Nsuserdefaults is a singleton class, each application has only one Nsuserdefaults object.

1) method of obtaining the Nsuserdefaults instance (Getting the Shared nsuserdefaults Instance)

+ (nsuserdefaults *) standarduserdefaults: Returns the default object.

+ (void) Resetstandarduserdefaults: Resets the user default object.


2) Get defaults (Getting default values)

-(nsarray *) Arrayforkey: (nsstring *) DefaultName

-(BOOL) Boolforkey: (nsstring *) DefaultName

-(nsdata *) Dataforkey: (nsstring *) DefaultName

-(nsdictionary *) Dictionaryforkey: (nsstring *) DefaultName

-(float) Floatforkey: (nsstring *) DefaultName

-(nsinteger)integerforkey:(nsstring *) DefaultName

-(ID) Objectforkey: (nsstring *) DefaultName

-(nsarray *) Stringarrayforkey: (nsstring *) DefaultName

-(nsstring *) Stringforkey: (nsstring *) DefaultName

-(double) Doubleforkey: (nsstring *) DefaultName

-(nsurl *) Urlforkey: (nsstring *) DefaultName


3) Set the default value (Setting Default Values)

-(void) Setbool: (BOOL) Value
Forkey: (nsstring *) DefaultName

-(void) setfloat: (float) Value
Forkey: (nsstring *) DefaultName

-(void) Setinteger: (nsinteger) Value
Forkey: (nsstring *) DefaultName

-( void SetObject: ( id value
           forkey: nsstring * Span class= "S5" > defaultname

-( void SetDouble: ( double value
            forkey: (nsstring * ) defaultname

-(void) SetUrl: (nsurl *) URL
Forkey: (nsstring *) DefaultName

There are a lot of ways, properties, please refer to the official documentation.


2. code example

@implementation Viewcontroller

-(void) viewdidload {

[superviewdidload];

}


-(void) didreceivememorywarning {

[superdidreceivememorywarning];

}


-(ibaction) Save: ( ID ) Sender {

// Get default configuration file

nsuserdefaults *userdefaults = [nsuserdefaults Standarduserdefaults];

[userdefaults setValue:self. UserName . text forkey:@ "UserName"];

[userdefaults setValue:self. UserPassword . text forkey:@ "UserPassword"];

[userdefaults setobject:self. Phone . text forkey:@ "Phone"];

[userdefaults setobject:self. Address . text forkey:@ "Address"];

nsarray *addlist =@[self. add1 . text , Self . ADD2 . text , Self . add3 . text ];

[userdefaults setValue: addlistforkey:@ "Addlist" ];

// submit Save now

[userdefaults synchronize];

self. Show . text = @ "Set Value Success" ;

// print sandbox location for easy viewing (can be omitted)

NSLog(@ "[%@]",nshomedirectory());

}


-(ibaction) Get: ( ID ) Sender {

nsuserdefaults *userdefaults = [nsuserdefaults Standarduserdefaults];

nsstring *username = [userdefaultsobjectforkey:@ "username" ];

nsstring *userpassword = [userdefaultsobjectforkey:@ " UserPassword "];

nsstring *phone = [userdefaultsobjectforkey:@ "Phone" /c19>];

nsstring *address = [userdefaultsobjectforkey:@ "Address" ];

nsarray *addlist = [userdefaultsobjectforkey:@ "Addlist" ];

[self. UserName setText: username];

[self. UserPassword setText: UserPassword];

[self. Phone setText:p hone];

[self. Address setText: address];

[self. add1 setText: addlist[0];

[self. ADD2 setText: addlist[1];

[self. add3 setText: addlist[2];

self. Show . text = @ "Get value Success" ;

}


-(void) Touchesbegan: ( Nsset *) touches withevent: (uievent *) Event

{

[self. UserName resignfirstresponder];

[self. UserPassword resignfirstresponder];

[self. Phone resignfirstresponder];

[self. Address resignfirstresponder];

[self. add1 resignfirstresponder];

[self. ADD2 resignfirstresponder];

[self. add3 resignfirstresponder];

}

@end


3. Operation Result



9. IOS programming using Nsuserdefaults Read, save application parameters

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.