One, array 1. Immutable variable group Nsarray
- Arraywithobjects: Creates an immutable group using a set of objects as elements, noting that the last value of the array needs to be specified as nil to represent the end of the argument, but nil is not stored in the array.
- Objectatindex: Gets the array element at the specified index position.
- @[elm1,elm2....elmn]: Another easy way to create an array without having to use nil as the trailing element;
- Array[index]: Another way to get the specified index location element.
- Count: The number of elements in the array.
intMainintargcConst Char*argv[]) {@autoreleasepool {/*non-variable groups*/ //Create immutable group mode oneNsarray *arr=[nsarray arraywithobjects:@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",@"Sunday", nil]; for(intI=0; I<[arr count];i++) {NSLog (@"%@", [arr objectatindex:i]); } //Create immutable Group mode twoNsarray *[email protected][@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",@"Sunday"]; for(intI=0; I<[ARR1 count];i++) {NSLog (@"%@", Arr1[i]); } }}
2. Variable array Nsmutablearray
- The Nsmutablearray array method creates an empty mutable array with an undetermined number of elements, which can grow as needed;
- AddObject: Adding elements to the end of a mutable array;
- Insertobject:obj ATINDEX:I: Inserts the object obj into the first element of the array.
- Removeobjectatindex:i deletes the element I of the array.
- REPLACEOBJECTATINDEX:I Withobject:obj replaces the object with the ordinal I in the array with obj.
- Use NSLog to format the%@ to display the entire array, which actually invokes the description method for each element.
intMainintargcConst Char*argv[]) {@autoreleasepool {/*variable Array*/Nsmutablearray*marr=[Nsmutablearray array]; for(intI=0; i<3; i++) {Marr[i][Email protected] (i+1); } [Marr addobject:@ (4)]; for(intI=0; I<[marr Count]; i++) {NSLog (@"%@", Marr[i]); } NSLog (@"%@", Marr); }}
There are two types of Dictionary objects (dictionary): immutable dictionary nsdictionary and variable dictionary nsmutabledictionary. 1. Immutable Dictionary Nsdictionary
- Nsdictionary Dictionarywithobjectsandkeys: Creates an immutable dictionary, which is a combination of value-key pairs (note order), ending with nil.
- AllKeys: Returns an array containing all the keys in the dictionary;
- Count: Returns the number of records in the dictionary;
- Objectforkey: Returns the value object for key.
intMainintargcConst Char*argv[]) {@autoreleasepool {/*Immutable Dictionaries*/nsdictionary*dic=[nsdictionary Dictionarywithobjectsandkeys:@"Student",@"1",@"Teachers",@"2", nil]; for(NSString *keyinchdic) {NSLog (@"key:%@ value:%@", Key,[dic Objectforkey:key]); } }}
2. Variable dictionary nsmutabledictionary
- Nsmutabledictionary Dictionary: Create a mutable dictionary;
- Setobject:forkey: Adding key-value pairs to the variable dictionary;
- Removeallobjects: Delete all the records in the dictionary;
- Removeobjectforkey: Delete the specified key in the dictionary key corresponding record;
intMainintargcConst Char*argv[]) {@autoreleasepool {/*Variable Dictionaries*/ //Create a variable Dictionary objectNsmutabledictionary *mdic=[Nsmutabledictionary dictionary]; //to add a key-value pair[MDic setobject:@"Student"Forkey:@"1"]; mdic[@"2"]=@"Teachers"; //Show key value pairsNSLog (@"key:1 value is:%@", [MDic Objectforkey:@"1"]);//key:1 value is: StudentNSLog (@"key:2 value is:%@", mdic[@"2"]);//key:2 value is: Teacher//Delete[MDic Removeobjectforkey:@"1"]; NSLog (@"MDic Count:%lu", [MDic Count]);//MDic count:1[MDic removeallobjects]; NSLog (@"MDic Count:%lu", [MDic Count]);//MDic count:0 }}
The collection object (set) set is a set of single-valued objects. 1.NSSet Immutable Dictionary
- Setwithobjects: Creates a collection with an array of objects ending in nil.
- Containobject: Detects whether an object is contained in the collection.
- Count: The number of members of the collection.
2.NSMutableSet Variable Dictionary
- AddObject: Adding objects to the collection;
- Removeobject: Removes an object from the collection;
- Removeallobjects: Deletes all objects in the collection;
- Unionset: A set of two sets;
- Intersectset: Finding the intersection of two sets
3. Example first we customize the classification for Nsset to add custom method print. Nsset+printing.h
#import <Foundation/Foundation.h>@interface nsset (Printing)-(void) print; @end
Nsset+printing.m
#import " nsset+printing.h " @implementation Nsset (Printing)-(void) print{ NSLog (@ "------" ); for inch Self ) { NSLog (@ "%li", (long) [item IntegerValue]);} } @end
Main.m
#import<Foundation/Foundation.h>#import "nsset+printing.h"intMainintargcConst Char*argv[]) {@autoreleasepool {//Create immutable CollectionsNsset *Set=[nsset setwithobjects:@"1",@"3",@"5",@"7", nil]; //Creating a mutable collectionNsmutableset *mset=[nsmutableset setwithobjects:@"1",@"2",@"3",@"4",@"5", nil]; //Output[SetPrint]; [Mset print]; //member Test if([SetContainsobject:@"7"]==YES) {NSLog (@"set contain 7"); }Else{NSLog (@"set does not contain 7"); } //adding Objects[Mset AddObject:@"6"]; [Mset print]; //removing Objects[Mset Removeobject:@"6"]; //Seek and set[Mset Unionset:Set]; [Mset print]; [Mset print]; //ask for intersection[Mset Intersectset:Set]; [Mset print]; }}
Learn iOS "3" arrays, dictionaries, and collections