2. Class
3. Common Collections
void Arraytest () {Nsarray *array = [Nsarray arraywithobjects:@ "one", @ "one", @ "three", nil]; NSLog (@ "len =%lu", [array Count]); For (NSObject *obj in array) {NSLog (@ "= =%@", obj); } for (int i=0; I<[array count]; i++) {NSLog (@ "%@", [array objectatindex:i]); }//write into the file, atomically whether to save the file to a temporary file, after the successful save, and then replace with the original file, is a security mechanism. [Array writetofile:@ "/users/zf/desktop/1.txt" atomically:yes]; Read file Nsarray *arr = [Nsarray arraywithcontentsoffile:@ "/users/zf/desktop/1.txt"]; NSLog (@ "Len3 =%lu", [arr Count]); }
void Mutablearraytest () {Nsmutablearray *array = [Nsmutablearray arraywithcapacity:3]; [Array addobject:@ "one"]; [Array addobject:@ ""]; [Array addobject:@ "three"]; [Array addobject:@ "TWO1"]; NSLog (@ "len =%lu", [array Count]); [Array Removeobjectatindex:3]; NSLog (@ "len =%lu", [array Count]); Nsenumerator *e = [array objectenumerator]; NSString *x; while (x = [E Nextobject]) {NSLog (@ "x = =%@", x); } nsenumerator *e1 = [array reverseobjectenumerator];}
void Dicttest () { nsdictionary *dict = [nsdictionary dictionarywithobjectsandkeys:@ "value1", @ "Key1", @ "value2", @ " Key2 ", nil]; NSLog (@ "%@", [dict objectforkey:@ "Key2"]); Nsmutabledictionary *d = [Nsmutabledictionary dictionarywithcapacity:3]; [D setobject:@ "wwwwww" forkey:@ "Key1"]; [D removeobjectforkey:@ "Key1"]; NSLog (@ "%lu, key1=%@", [D Count], [D objectforkey:@ "Key1"]);}
Nsarray: An ordered set of elements in an entire block of memory and arranged sequentially;
Nsset: Unordered collection, hash store.
Read Developer.apple's explanation of Nsset: You can use sets as a alternative to arrays when the order of elements isn ' t important and per Formance in testing whether an object was contained in the set is a consideration-while arrays be ordered, testing for MEM Bership is slower than with sets.
Searching for an element, Nsset is more efficient than nsarray. Why is it? The principle is simple: the storage and access of elements in Nsset is a hash process. For example, if you want to store element A, a hash algorithm can directly find the location where a should be stored, and also, when you want to access a, a hash process can find the location of a storage. For Nsarray, if you want to know if a is not in the array, you need an element comparison, obviously inefficient.