I. nsarray
Nsarray is a class of cocoa used to storeObject.
Restrictions:
1. Only objects in object-C can be stored, and basic data types in C language cannot be stored, such as random pointers in int, float, Enum, struct, or nsarray;
2. Nil cannot be stored (zero or null value of the object );Why continue to look down
How to create?
Create nsarray using arraywithobjects, and separate the Object List with commas (,). Adding nil at the end of the list indicates that the list ends (This is the first reason why nil cannot be stored in the array (the second one goes down))
Nsnull can be used to indicate no value+ (Nsnull *) NULL;
Nsarray *Array; Array= [Nsarray arraywithobjects:@"1",@"2",@"3", Nill];
Number of array objects:-(unsigned) count;
Get the object based on the index:-(ID) objectatindex :( unsigned INT) index;
Eg:
# Import <Foundation/Foundation. h> Int Main ( Int Argc, Const Char *Argv) {nsarray * Array; Array = [Nsarray arraywithobjects: @" 1 " , @" 2 " , @" 3 " , Nil]; Int I; For (I = 0 ; I <[array count]; I ++ ) {Nslog ( @" % D is: % @ " , I, [array objectatindex: I]) ;}}
Effect:
Split Array
Use-componentsseparatedbystring to split nsarray
Nsstring * string = @ "Kaka: 18: Pepe: 20 ";
Nslog (@ "I: % @", string );
Nsarray * array = [String componentsseparatedbystring: @ ":"];[K comment m'p comment UN comment nt]ConsistingN. component; Component['Sep retry REIT, 'sep restart rit]Vt. Enable separation; enable separation
Use componentsjoinedbystring to merge elements in nsarray and create a string
String = [array componentsjoinedbystring: @ "*"];
Nslog (@ "changed me: % @", string );
Eg:
2. variable array nsmutablearray
Nsstring has a variable stringNsmutablestringSimilarly, the nsarray array also has a variable array nsmutablearray created in the same way as nsmutablestring:
+ (ID) arraywithcapacity :( unsigned) numitems
Eg:
Nsmutablearray * Array2; array2 = [Nsmutablearray arraywithcapacity: 17 ]; Nsarray * Array3 = [nsarray arraywithobjects:@" One " , @" Two " , @" Three " , Nil]; [array2 addobject: array3]; Int I; For (I = 0 ; I <[array2 count]; I ++ ) {Nslog ( @" % D is: % @ " , I, [array2 objectatindex: I]);}
Effect:
Delete:-(void) removeobjectatindex :( unsigned) index;
Iii. Enumeration kingdoms
Nsenumerator is a cocoa method used to describe the iterative operation of a set. You can use objectenumerator to request the enumerator from the array:
-(Nsenumerator *) objectenumerator;
Eg:
# Import <Foundation/Foundation. h> Int Main ( Int Argc, Const Char * Argv) {nsarray * Array; Array = [Nsarray arraywithobjects: @" A " , @" B " , @" C " , Nil]; nsenumerator * Enumerator; enumerator =[Array objectenumerator]; ID Thing; // If nextobject returns the nil value, the loop ends. While (Thing = [Enumerator nextobject]) {nslog ( @" I found % @ " , Thing );} // Reverseobjectenumerator views the set from the back to the front Enumerator = [Array reverseobjectenumerator]; While (Thing = [Enumerator nextobject]) {nslog ( @" I found % @ " , Thing );}}
Effect:
Note:
1. When xtobject returns nil, the loop ends. This is the second reason why nsarray cannot have nil.
2. You cannot change the array container by adding or deleting objects.
Iv. Quick Enumeration
The book says this:
Nsstring *String;For(String InArray) {nslog (@"I found % @",String);}
However, I am nodepad ++ vs GNU. As mentioned in the book, this is a new feature and cannot be run. It is suitable for running on a leopard or more advanced version of the system.Program
Summary:
Three methods are introduced to traverse Arrays: Index, nsenumerator, and quick enumeration.
If your system is a later version of the operating system or leopard ['Lep proposal D]N. Leopard; leopardSo select the third quick enumeration. The reason is simple: quick and concise.
5. dictionaries (also known as scattered lists and associated arrays)
Nsdictionary: stores a value (can be any type of object) under a given keyword (usually an nsstring string ).
Both the dictionary and array can store data, but it is more efficient to frequently query large datasets because the dictionary is an optimized storage method for key queries, you can immediately find the data to be queried without repeating the entire array to query the data.
Use the class method dictionarywithobjectsandkeys to create a dictionary. Use the nil value as the Terminator.
+ (ID) dictionarywithobjectsandkeys:(ID) firstobject ,......