Obejct-C contains three sets: array, dictionary, and set ).
Arrays are similar to arrays in C language, but arrays in OC can only store objects, not basic data types, such as int, float, Enum, struct, or nil. It also provides the compiled index object, which can be used to locate the object to be viewed by making the index. Contains nsmutablearray and nsarray ). The dictionary stores "key-value pairs", that is, key-value. You can use the key-value pairs to find the objects saved in the dictionary. The Set stores objects as well. The objects saved in the set and dictionary are unordered, but the dictionary can be sorted by key. The same object cannot exist in the set and dictionary. if the object is consistent and is written at the beginning of definition, the subsequent object cannot be written. If it is added later, the same object will be replaced. Basic usage:
Initialize an unchangeable array: nsarray * array = [[nsarray alloc] initwithobjects: @ "one", @ "two", @ "three", nil]; this array has only three objects, one, two, three, and the final nil. It can be seen that the Terminator is not stored in the array. Nslog (@ "array COUNT = % d", [array count]); // print the number of objects in the array [array objectatindex: 2] // The variable array for object initialization at index 2: nsmutablearray * mutablearray = [nsmutablearray arraywithcapacity: 0]; // you can set the initial length of the variable array to 0; copy an array to another array mutablearray = [nsmutablearray arraywitharray: array]; // copy the array object to mutablearray and add the object [mutablearray addobject: @ "four"]; fast enumeration: OC provides a fast and centralized method for retrieving arrays, dictionaries, and sets, which is called Quick enumeration, now the array contains a String object, so the quick enumeration is as follows: for (nsstring * STR in array) {nslog (@ "array = % @", STR ); // one-to-one output of objects in the array} are separated from strings to Arrays-componentsseparatedbystring: nsstring * string = [nsstring alloc] initwithstring: @ "one, two, three, four"];
Nslog (@ "string: % @", string );
Nsarray * array = [String componentsseparatedbystring: @ ","];
Nslog (@ "array: % @", array );
[String release];
// Merge elements from the array to the string-componentsjoinedbystring:
Nsarray * array = [nsarray alloc] initwithobjects: @ "one", @ "two", @ "three", @ "four", nil];
Nsstring * string = [array componentsjoinedbystring: @ ","];
Nslog (@ "string: % @", string );
Delete objects in the specified index of a variable array:
[Mutablearray removeobjectatindex: 1];
Nsdictionary * dictionary = [nsdictionary alloc] initwithobjectsandkeys: @ "one", @ "1", @ "two", @ "2", @ "three", @ "3 ", nil];
Nsstring * string = [dictionary objectforkey: @ "one"];
Nslog (@ "string: % @", string );
Nslog (@ "dictionary: % @", dictionary );
[Dictionary release];
Create a variable dictionary:
// Create
Nsmutabledictionary * dictionary = [nsmutabledictionary dictionary];
// Add a dictionary
[Dictionary setobject: @ "one" forkey: @ "1"];
[Dictionary setobject: @ "two" forkey: @ "2"];
[Dictionary setobject: @ "three" forkey: @ "3"];
[Dictionary setobject: @ "four" forkey: @ "4"];
Nslog (@ "dictionary: % @", dictionary );
// Delete the specified dictionary
[Dictionary removeobjectforkey: @ "3"];
Nslog (@ "dictionary: % @", dictionary );
// There cannot be repeated objects in nsset
Nsmutableset * set1 = [[nsmutableset alloc] initwithobjects: @ "1", @ "2", @ "3", nil];
Nsmutableset * set2 = [[nsmutableset alloc] initwithobjects: @ "1", @ "5", @ "6", nil];
[Set1 unionset: set2]; // obtains the Union set 1, 2, 3, 5, 6
[Set1 intersectset: set2]; // obtain intersection 1
[Set1 minusset: set2]; // gets the difference set
Related Articles:
Http://wenku.baidu.com/link? Url = Response