IOS stage learning 15th-day notes (NSDictionary and NSMutableDictionary dictionary), iosnsdictionary

Source: Internet
Author: User
Tags allkeys

IOS stage learning 15th-day notes (NSDictionary and NSMutableDictionary dictionary), iosnsdictionary

Knowledge points of IOS Learning (oc language)

1. OC dictionary

1) dictionary: A container object. elements are stored in the form of key-value pairs. Keys and values are of any type and keys are unique, the value can be repeated.

2) There are two types of dictionaries in OC:

1. unchangeable dictionary: NSDictionary, which cannot be modified after Initialization

2. Variable dictionary: NSMutableDictionary, which can be modified after initialization.

Ii. NSDictionary dictionary operations

1) create a dictionary object using the instantiation method, for example:

1 NSDictionary * dict1 = [[NSDictionary alloc] initWithObjectsAndKeys: @ "one", @ "1", @ "two", @ "2", @ "three ", @ "3", @ "one11", @ "1", @ "two", @ "22", nil]; 2 // note: the previous one is a value) the last one is the key)


2) count can calculate the number of key-values in the dictionary, for example, NSLog (@ "% ld", [dict1 count]);

3) initWithDictionary creates another dictionary object with one dictionary, for example:
1 NSDictionary * dict2 = [[NSDictionary alloc] initWithDictionary: dict1];

4) dictionaryWithObjectsAndKeys:
1 NSDictionary * dict3 = [NSDictionary dictionaryWithObjectsAndKeys: <# (id),... #>, nil];

5) quickly create an object @ {key1: value1, key2: value2,...}. For example:
1 NSDictionary * dict3 =@ {@ "apple": @ "apple", @ "red": @ "red"}; 2 // note: the front is the key) the value is a bit similar to the Json data type.

6) objectForKey is used to obtain the value based on the key, for example:
1 NSString * value = [dict1 objectForKey: @ "2"]; // result: two

7) allKeysForObject extracts all the corresponding keys based on the value (the values can be repeated, and the keys cannot be repeated) for example:
1 NSArray * keys = [dict1 allKeysForObject: @ "two"];

8) allKeys is used to retrieve all keys, for example:
1 NSArray * allKeys = [dict1 allKeys];
9) allValues is used to retrieve all values, for example:
1 NSArray * allValues = [dict1 allValues];

10) NSDictionary Traversal method

1. iterator method:

1 NSEnumerator * enumerator = [dict1 keyEnumerator]; 2 id obj; 3 // [enumerator nextObject]: If a key exists, it is returned. Otherwise, it is nil, automatically points to the next key4 while (obj = [enumerator nextObject]) {5 NSLog (@ "% @ --> % @", obj, [dict1 objectForKey: obj]); 6}


2. Fast traversal:

1  for(id key in dict1){2   NSLog(@"%@*******%@",key,[dict1 objectForKey:key]);3  }


3. NSMutableDictionary dictionary operations

1) NSMutableDictionary inherits from NSDictionary.

2) create an empty dictionary object, for example, 1 NSMutableDictionary * dict1 = [[NSMutableDictionary alloc] init];

3) setObject... ForKey... If the key does not exist, add it. If the key exists, modify it, for example:
1 [dict1 setObject: @ "one" forKey: @ "1"];

4) addEntriesFromDictionary adds all the content of the other dictionary. For example:

1 NSDictionary *subDict=@{@"2":@"two",@"3":@"three",@"1":@"one1",@"4":@"four"};2 [dict1 addEntriesFromDictionary:subDict];


5) removeObjectForKey: delete an element based on the key (both the key and the corresponding value will be deleted). For example:
1 dict1 removeObjectForKey: @ "1"];

6) removeObjectsForKeys is used to delete multiple keys and corresponding values, for example:
1 NSArray * keys = @ [@ "2", @ "3"]; 2 [dict1 removeObjectsForKeys: keys];

7) removeAllObjects:
1 [dict1 removeAllObjects];

8) setDictionary use another dictionary to reset the dictionary content, for example:
1 [dict1 setDictionary: subDict];

Iv. Package and unpackage in OC

1) package the process of converting a value type data to an object type data, for example:

1 // encapsulate int data into an object (packed) 2 NSNumber * intNumber = [[NSNumber alloc] initWithInt: 100]; 3 NSNumber * longNumber = [NSNumber numberWithInteger: 100]; 4 NSNumber * charNumber = [[NSNumber alloc] initWithChar: 'a'];


2) NSNumber: Used to encapsulate data into objects.

3) Unpack a process that converts an object type to a value type, for example:

1 // retrieve the basic data values in the object (unpack) 2 NSLog (@ "% d", [intNumber intValue]); 3 NSLog (@ "% ld ", [longNumber integerValue]); 4 NSLog (@ "% c", [charNumber charValue]);


4) compare is used to compare the data values of two objects.

Instance code:

1 NSComparisonResult cmp = [intNumber compare: longNumber]; 2 // NSNumber can directly display the data value. The description method has been rewritten. 3 if (cmp = NSOrderedAscending) {4 NSLog (@ "% @ <% @", intNumber, longNumber); 5} else if (cmp = NSOrderedDescending) {6 NSLog (@ "% @> % @", intNumber, longNumber); 7} else if (cmp = NSOrderedSame) {8 NSLog (@ "% @ = % @", intNumber, longNumber); 9}


5) NSValue: encapsulate data of the struct and pointer types into objects.

6) encapsulate struct data into objects. Note: you cannot store struct variables in arrays. You need to encapsulate them as NSValue objects.

Instance code:

1 struct mysct 2 {3 int a; 4 int B; 5}; 6 struct mysct s1 = {1, 2}, s2; 7 NSValue * value1 = [[NSValue alloc] initWithBytes: & s1 objCType: @ encode (struct mysct)]; 8 NSLog (@ "% s, % s", @ encode (struct mysct), @ encode (int )); // result: {mysct = ii}, I 9 NSArray * array1 = [[NSArray alloc] initWithObjects: value1, nil]; 10 // Save the object encapsulated by the struct variable to the array 11 NSArray * array1 = [[NSArray alloc] initWithObjects: value1, nil]; 12 NSValue * value2 = [array1 firstObject]; 13 // store the data in value2 to s214 [value2 getValue: & s2]; 15 NSLog (@ "s2: {% d, % d}", s2.a, s2. B ); // result: 1, 2

7) @ encode (aType) returns the representation of this type of C string (char.

5. Random Number Generation in OC

1. rand is used to generate random numbers of the int type. rand () is not a real pseudo-random number generator, and random () is relatively better.

2. random is used to generate a random number of the long type. The seed needs to be set during initialization, for example:
1 srandom (unsigned int) time (time_t *) NULL); // set the seed during initialization.

3. arc4random is used to generate random data of the unsigned int type, for example:

1 int value = arc4random () % x; // returns an integer random number between 0 and X-1 2 int value = (arc4random () % x) + 1; // returns an integer random number between 1 and x.


6. Structures commonly used in OC

 

1) structures commonly used in OC: NSPoint, NSRect, NSSize, and nsange

1. NSPoint is used to get/set the coordinate point instance code of the object:

1 NSPoint pt = NSMakePoint (10, 20); 2 NSValue * vl1 = [NSValue valueWithPoint: pt]; 3 NSPoint pt2 = [vl1 pointValue]; 4 NSLog (@ "Point = (%. 0f, %. 0f) ", pt2.x, pt2.y); // result: Point = (10, 20)

2. NSRect is used to obtain/set the coordinate point and length and width of an object. The example code is as follows:

1 NSRect rt = NSMakeRect (2, 6, 20, 10); 2 NSValue * vl3 = [NSValue valueWithRect: rt]; 3 NSRect pt4 = [vl3 rectValue]; 4 NSLog (@ "Rect = (%. 0f, %. 0f, %. 0f, %. 0f) ", pt4.origin. x, pt4.origin. y, pt4.size. width, pt4.size. height); // result: Rect = (2, 6, 20, 10)

3. NSSize is used to get/set the object length and width. Example code:

1 NSSize rs=NSMakeSize(30, 60);2 NSValue *vl4=[NSValue valueWithSize:rs];3 NSSize pt5=[vl4 sizeValue];4 NSLog(@"Size=(%.0f,%.0f)",pt5.width,pt5.height);


4. The nsange instance code is used to obtain/set the start coordinate and length of an object:

1 NSRange rg=NSMakeRange(3, 6);2 NSValue *vl2=[NSValue valueWithRange:rg];3 NSRange pt3=[vl2 rangeValue];4 NSLog(@"Range=(%lu,%lu)",pt3.location,pt3.length);


2) The elements in basic data can be operated only when the data is converted to an object.

Related Article

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.