Definition of the array:
Arrays: Arrays are collections of ordered objects that are used to store objects with sequential tables (note that basic data types cannot be stored, only objects can be stored). objects: Concrete implementations of Classes
As with strings, arrays also have variables and immutable groups, and variable groups are subclasses of immutable groups, and array objects must be created before using arrays
Nsarray class: (Non-variable group)
1. Initialization of the array:
Example method: nsarray* array=[[nsarray alloc]initwithobject:@ "One", @ "tow", nil];
Class method: Nsarray * Array=[nsarray arraywithobject:@ "one", @ "tow", nil];
2. Get the number of elements in the array
-(Nsuinteger) count;
For example: int count=[array count];
3. Get the elements in the array:
-(ID) Objectatindex: (Nsuinteger) index;
For example: NSString * Srt=[array Objectatindex:1];//str has a value of @ "Tow"
Precautions for using the Nsarray class:
1. Array out of bounds, when accessing Nsarray, it is best to check whether the index is out of bounds
2. Array adds one to the object reference count (memory management aspect)
3. No more intermediate Nil,nil is the identifier of the end of the array
Nsmutablearray class: (Variable group)
Nsmutablearray is a subclass of Nsarray, so some methods of Nsarray are also applicable to Mutablearray
1. Initialization of a mutable array:
instance method:-(ID) initwithcapacity: (Nsuinteger) NumItems;
For example: nsmutablearray* Array=[[nsmutablearray alloc]initwithcapacity:12];
Class method: + (ID) initwithcapacity: (Nsuinteger) NumItems;
2. Add Objects:
-(void) AddObject: (ID) anobject;//added on the last side
For example: [Array addobject:@ "three"];
Specify location to add
-(void) Insetobject: (ID) anobject atindex: (Nsuinteger) index;//inserts a AnObject object at index and moves behind the object after index by one
3. Delete the object:
-(void) removeallobject;//Delete all objects within the array
-(void) removelastobject;//Delete the last object of the array
-(void) Removeobjectindex: (nsuinteger) index;//Delete the specified object
4. Replace:
-(void) Replaceobjectatindex: (nsuinteger) index Withobject: (ID) anobject;//replaced with AnObject object at index position
Traversal of the array:
for (int i=0;i>[array count];i++) {NSL (@ "%@", [array objectatindex:i]);}
Dictionary nsdictionary (immutable):
First of all, consider these questions, what is a dictionary? Why do you use a dictionary? What is the difference between a dictionary and an array?
Dictionary: is the set of keywords and their definitions. Using a dictionary to find information is much faster than an array. The dictionary is stored in a different way than the array, it is a key query of the optimal storage, it can be found through the keyword immediately to find the data, rather than go through the entire collection to find. Dictionaries and strings, arrays also have mutable and immutable points
1. Create:
-(ID) Dictionarywithobjectandkeys: (id) obj,..,..;
Example: Student * stu1=[student new];
Student * Stu2=[student new];
Nsdictionary * dict=[nsdictionary dictionarywithobjectandkeys:stu1,@ "Peter", stu2,@ "Andy", Nil];
Description, the parameters and keys are paired, and the end mark is nil
2. Find:
-(ID) Objectforkey: (ID) akey;//Use the key to find the object to ask for
For example: ID student=[dict objectforkey:@ "Peter"];//the object that gets the "Peter" key
3. Precautions:
When you create a dictionary, the keys and values of the dictionary are the IDs (that is, the object) type;
When creating a dictionary, the arguments passed in are key-value pairs, that is, keys and objects to appear in pairs, and finally do not forget to add nil;
Dictionary keywords (keys) do not repeat, if repeated, the following values will overwrite the previous.
Nsmutabledictionary Class (variable):
A mutable dictionary class is a subclass of an immutable dictionary, so the method used above is also applicable to Nsmutabledictionary
1. Create:
+ (ID) dictionarywithcapacity: (Nsuinteger) num;
Example: nsmutabledictionary* dict=[nsmutabledictionary Dictionarywithcapacity:5]
Description: Created a variable dictionary with 5 key-value pairs initialized, 5 is the initial size for increased efficiency, and more than 5 will continue to be added later
2. Add:
-(void) SetObject: (ID) obj forkey (id) akey;//keyword and object appear in pairs
For example: Student *stu1=[student new];
Student *stu2=[student New];
nsmutabledictuinary* Dict2=[nsmutabledictionary Dictionarywithcapacity:2];
[Dict2 setobject:stu1 forkey:@ "y"];
[Dict2 setobject:stu2 forkey:@ "K"];
Note: If you add a keyword in the original dictionary already has, then you added will overwrite the original keyword value
3. Delete:
-(void) Removeobjectforkey: (id) akey;//Delete the corresponding data based on the keyword
For example: The new dictionary operation above: [Dict2 removeobjectforkey:@ "y"]; result stu1 object removed from the dictionary
-(void) removeallobject;//Delete all data
4. The traversal of the dictionary:
Since dictionaries store data in the form of key-value pairs, it is not possible to directly apply the index to traverse the elements in the dictionary, but there is a method in the dictionary AllKeys:
-(nsarry*) allkeys;//get all the key values of the dictionary, return an array, get the corresponding object by returning the array, immutable and mutable dictionary traversal method
Applies for loop traversal each key that holds all keys, and then gets the relative object based on key
student* stu1=[student New];
student* stu2=[student New];
nsdictionary* dict=[nsdictionary dictionarywithobjectsandkeys:stu1,@ "M", stu2,@ "K", nil];
nsarray* keys=[dict AllKeys];
ID key,value;
for (int i=0; I<[keys count]; i++) {
Key=[keys OBJECTATINDEX:I];
Value=[dict Objectforkey:key];
NSLog (@ "key:%@,value:%@", Key,value);
}
Set set (Nsset, immutable):
The Nsset collection is similar to an array, but the set collection cannot hold the same object, which is a set of single-valued objects that are stored in the unordered order of the data in the collection.
Initialization of 1.NSSet:
-(ID) initwithobjects: (ID) firstobject,..., nil;
+ (ID) setwithobjects: (ID) firstobject,..., nil;
For example: nsset* set=[[nsset alloc]initwithobjects:@ "One", @ "one", Nill;
Number of 2.NSSet:
-(Nsuinteger) count;
Elements in 3.NSSet:
-(nsarray*) allobject;//gets all the elements in the collection, returned as an array
-(ID) anyobject;//randomly obtains an element in the collection
Some of the 4.NSSet judgment methods:
-(BOOL) Isequaltoset: (nsset*) otherset;//judgment and another set of otherset are equal
-(BOOL) member: (ID) obj;//Determine if this collection contains object obj
5. Need to be aware of:
The set collection cannot hold the same object, and the elements are stored in an unordered
It is different from the array:
Nsarray: Ordered episodes together, storing elements in an entire block of memory and arranged sequentially
Nsset: Unordered collection, hash store.
When searching for an object, Nsset is more efficient than nsarray, mainly because Nsset uses an algorithm hash (hash, literal translation), Nsset directly through the hash to find the element, and the array to traverse the entire array, obviously inefficient.
Nsmutableset (variable):
Creation of 1.NSMutableSet:
-(ID) initwithcapacity: (Nsuinteger) numitems;//instance method
+ (ID) setwithcapacity: (Nsuinteger) numtems;//class method
The same numtems is the initial size and does not limit the collection size
2. Add elements to the collection:
-(void) AddObject: (ID) object;
[Set addobject:student];
3. Delete elements from a collection
-(void) removeallobject;//Delete all elements in the collection
-(void) Removeobject: (ID) object;//Delete object in collection
For example: [Set removeobject:student];
Array data:
The above-mentioned collection cannot hold the basic data type, so how to encapsulate the basic data types into objects? NSNumber type is to solve this problem
NSNumber class:
1. Creating the Encapsulation method
+ (NSNumber) Numberwithchar: (char) value;//encapsulates a char data type
+ (NSNumber) Numberwithint: (int) value;//encapsulates an int data type
+ (NSNumber) Numberwithfloat: (float) value;//Package A single-precision data type
+ (NSNumber) Numberwithbool: (BOOL) value;//encapsulates a double-precision data type
The above is the class method enumerated
-(ID) Initwithchar: (char) value;
-(ID) initwithint: (int) value;
-(ID) initwithfloat: (float) value;
-(ID) Initwithbool: (BOOL) value;
For example: NSNumber *number=[nsnumber numberwith:3];//the INT type 3 package into a nsnumber;
The above is the basic type of encapsulation, the structure should be so encapsulated? With Nsvalue
Creation and use of Nsvalue:
+ (nsvalue*) Valuewithbytes: (const void*) value Objctype: (const char*) type;
The first parameter is the body address, and the second parameter is a string that describes the data type, used to describe the type and size of the entity in the struct, and the @encode compiler directive can accept the name of the data type and generate the appropriate string for you.
Nsrect Rect=nsmakerect (4,4,100,100);
nsvalue* value=[nsvalue valuewithbytes:&rect objctype: @encode (nsrect)];
[Array Addobject:value];
So we're going to wrap a struct into an object of type Nsvalue.
Get the value from Nsvalue
-(void) GetValue: (void *) value;
When calling GetValue, the address of the variable to store the value is passed, similar to the scanf () function
For example: Nsvalue *value=[array objectatindex:0];
Nsrect Rect1;
[Value getvalue:&rect1];
In this way, the result of converting the nsvalue into a struct is assigned to rect1 by the address of the rect1.
NSNull
Nil put in the set represents the end, so how to put the empty value in the collection?
Cocoa provides a nsnull class that encapsulates nil null values with this class.
+ (nsnull*) null;
For example: [array addobject:[nsnull null]];
ID Nullvalue=[array objectatindex:0];
if (nullvalue==[nsnull null]) {
If the empty value of the flower, do some operations
}
OC Collection-Learning notes (arrays, dictionaries, set sets, numeric data)