iOS Development-OC Chapter-nsset,nsnumber

Source: Internet
Author: User

Recently reviewed some of OC 's basic knowledge, found that really forgot a lot of important things, and today organized some small points of knowledge, and share with you.

The iOS Collection object cannot store the C-language base type, all of which can be boxed and unboxing for OC object manipulation.

1,nsnumber Packaging class

1> Normal Initialization

NSNumber * NUM1 = [[NSNumber alloc] initwithint:20];

NSNumber * num2 = [NSNumber numberwithchar: ' A '];

2> literal initialization

NSNumber * num3 = @300;

NSNumber * num4 = @123.2;

NSNumber * num5 = @ ' X ';

3> variable Initialization

int a = 29;

NSNumber * NUM5 = @ (a);

4>nsnumber to basic data types

Charvalue,intvalue,longvalue,doublevalue

2,nsvalue Packaging class

Example:

typedef struct {

int age;

char * name;

char sex;

} Sperson;

Sperson person;

Person.name = "Dahuan";

Person.age = 10;

Person.sex = ' m ';

Converts a struct to a Nsvalue object,

@encode (Atype) can return the representation of a C string (char *) of that type

Nsvalue * value = [Nsvalue valuewithbytes:&person objctype: @encode (Sperson)];

Sperson Person2;

Convert a Nsvalue object to struct

[Value getvalue:&person2];

NSLog (@ "%s", person2.name);

NSLog (@ "%d", person2.age);

NSLog (@ "%c", person2.sex);

Nsrange range = {12,2};

Commonly used structural body packing

Nsvalue * rvalue =[nsvalue Valuewithrange:range];

Rangevalue will nsrange unpacking

NSLog (@ "%@", Nsstringfromrange ([rvalue rangevalue]));

3. Distinguish various null values

Nil is an empty object pointer

Nil is a pointer to a class that is empty

Null is the basic data type is empty.

Nsnull an empty object can be placed in an array.

4.Nsset Usage

1> Nsset Initialization

Nsset * set = [[Nsset alloc] initwithobjects:@ "a", @ "B", @ "C", nil];

Nsset * Set1 = [Nsset setwithobjects:@ "z", @ "x", @ "C", nil];

Nsset * Set2 = [Nsset Setwitharray:array];

2> getting arbitrary objects in Nsset

[Set2 Anyobject]

Whether the 3> contains an object

[Set2 containsobject:@ "W"]

Whether the 4> has the same element

[Set Intersectsset:set2]

5.Nsmutableset Usage

1> Nsmutableset Initialization

Nsmutableset * set = [[Nsmutableset alloc] initwithcapacity:0];

2> Append delete and Nsmutablearray same

3> Take two nsset of the set

[Set Unionset:set2]

4> Nsset minus the same element

[Set Minusset:set2]

5> the intersection of two Nsset

[Set Intersectset:set2]

6.nsdictionary Usage

1> Nsdictionary Initialization

Nsdictionary * Dict = [[Nsdictionary alloc] initwithobjectsandkeys:@ "value1", @ "Key1", @ "value2", @ "Key2", nil];

Nsdictionary * Dict2 = [nsdictionary dictionarywithobjects:@[@ "value1", @ "value2"] forkeys:@[@ "Key1", @ "Key2"];

Nsdictionary * dict3 = @{@ "Key1": @ "value1", @ "Key2": @ "value2"};//literal form

2> to get value values by key value

[Dict3 objectforkey:@ (2)]//key value can be any object

[Dict3 valueforkey:@ "Key2"]//key value can only be nsstring

dict3[@ "Key2")//literal form, recommended use

3> get all the key values in the dictionary

[Dict3 AllKeys]

4> gets all the value values in the dictionary

[Dict3 Allvalues]

iOS Development-OC Chapter-nsset,nsnumber

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.