10. OC Foundation (under Foundation framework)
1. The following describes the storage structure of several OC object types: Nsarray,nsdictionary,nsset, and the basic data type encapsulation class.
NSNumber: * since these kinds of data structures are used to The basic data types that hold the type of object you want to put in are encapsulated first. Use NSNumber to encapsulate the base data type.
////Created by Keeganlee on 15/3/2.//Copyright (c) 2015 Keeganlee. All rights reserved.//#import<Foundation/Foundation.h>intMainintargcConst Char*argv[]) {@autoreleasepool {nsnumber*number1=[nsnumber Numberwithint:Ten]; NSNumber*number1=[nsnumber numberwithfloat:12.5]; NSNumber*number1=[nsnumber Numberwithchar:'k']; NSNumber*number1=[nsnumber Numberwithbool:true]; } return 0;}
The above lines of code, respectively, the Int,float,char,bool type of data is encapsulated. Make them an object. This makes it possible to deposit into arrays, collections, and dictionaries like objects.
2. Set Nsarray
*nsarray: is an immutable group, a single creation cannot be changed.
* How to create: Created by construction method,
int Main (intconstChar * argv[]) { @autoreleasepool { *array=[ Nsarray alloc] initwithobjects:<# (ID), ...#>, nil } return0 ;}
You can add multiple elements to them at once, but once you create them, you can't change them. (Note: The last element in the collection must be nil).
The address of each object is stored in the array, not the data. Because stored objects can be of various types, it is best to re-write the various description methods when you want to print the objects in a group.
Be careful not to store nil as an element in an array, or it will cause the array to be lost.
* Create a shortcut to an array: Nsarray *[email protected][@ "A", "B"];
* Get shortcut to element: NSString *str=array[0]
Another way to get an element: [array objectatindex:10];
* How strong for loop for (NSObject *OBJC in array)
Each time an element is removed from an array of arrays, the element is assigned to the NSObject element. Manipulate the element.
* Enumerator Nsenumerator:(equivalent to iterator in the Java language)
//#import<Foundation/Foundation.h>intMainintargcConst Char*argv[]) {Nsarray*[email protected][@"Good",@"Good",@"study"]; Nsenumerator*enumer=[arr Objectenumerator]; NSString*value; while(value=[Enumer Nextobject]) {NSLog (@"%@", value); } }
* Sorting of arrays: no longer like the C language to write their own sorting algorithm, directly with the internal function can be, the teacher in class altogether said 4 sort method actually only three kinds of
The first type : Using Sortarrayusingselector
int Main (intconstChar * argv[]) { *[email protected][@ " b "@"C",@"d"]; [Array sortedarrayusingselector: @selector (compare:)]; }
It is worth noting that the compare here: is for the nsstring type, so other types cannot use this sort method.
The second type: sort by using block mode
#import<Foundation/Foundation.h>intMainintargcConst Char*argv[]) {Nsarray*[email protected][@"b",@"C",@"D"]; [Array Sortedarrayusingcomparator:^nscomparisonresult (IDObj1,IDobj2) {Nscomparisonresult result=[Obj1 Compare:obj2]; returnresult; }]; }
The same compare method is used for nsstring types.
The third type: custom objects to sort
#import <Foundation/Foundation.h>int main (intconstChar * Argv[]) { *array=@[person1,person2,person3]; *d1=[nssortdescriptor Sortdescriptorwithkey:@ "Age" Ascending:yes]; *compare=@[d1]; [Array sortedarrayusingdescriptors:compare]; }
This way is more flexible, can be based on the different objects of the common keyword to sort, and can be the key word ascending, descending operation. Features are more powerful.
The fourth kind: actually is the above several varieties
#import <Foundation/Foundation.h>int main (intconstChar * Argv[]) { *array=@[person1,person2,person3]; Array=[array sortedarrayusingcomparator:^nscomparisonresult (IDID obj2) { *p1=obj1; *p2=obj2; return [P1.year compare:p2.year];} }
In fact, it also uses the nssting type of compare method.
Array traversal: Normal for loop, enhanced for loop enumerator traversal
* First type : normal for loop traversal
#import <Foundation/Foundation.h>int main (intconstChar * Argv[]) { *array=@[person1,person2,person3]; for (int i=0; i<=array.count; i++) { NSLog (@ "%@") , Array[i]); } }
* Second type: enhanced for loop
int Main (intconstChar * argv[]) { *array=@[person1,person2, Person3]; for inch Array) { NSLog (@ "%@"); } }
* Third Type: Enumerator traversal
int Main (intconstChar * argv[]) { *array=@[person1,person2, Person3]; *enmer=[Array objectenumerator]; *per; while (per=[Enmer Nextobject]) { NSLog (@ "%@", per); } }
* Dictionary
Nsdictionary: A dictionary is a collection of keywords and their definitions (descriptions). The collection of implementation dictionaries in cocoa Nsdictionary stores a numeric value (which can be any type of object) under a given keyword (usually a nsstring). Then you can use this keyword to find the corresponding value.
Unlike arrays, dictionaries (also known as hash lists or associative arrays) use an optimized storage method for key queries. It can immediately find the data to be queried without having to traverse the entire array to find it.
You can use Dictionarywithobjectsandkeys to create a dictionary
Query Dictionary value: Objectforkey
Nsmutabledictionary's Dictionary method can either create a mutable dictionary or use the Dictionarywithcapaticy:.
Use the Setobject:forkey: method to add a dictionary element and replace the old value with a new implant if the keyword already exists.
Similarly, the Nsmutabledictionary class allows you to arbitrarily add or remove dictionary elements.
add element: Setobject:forkey:
Delete element: Removeobjectforkey:
Myclassdict = [nsdictionary dictionarywithobjectsandkeys:
@" my1 " ,
@" My2 " , @" my3 " , @" My4 " , nil];
*set:The element stored in set cannot be the same, he will compare the address of these two elements first, if the same join element fails, if not in the content of the comparison element. Different classes in Java need to define different equal methods. To make sure that the values you have deposited are different.
Dark Horse Programmer--oc Foundation Tenth (Under Foundation framework, collection)