- A dictionary class
- Two sets of classes
- A fast traversal of a three-array dictionary collection
- Four-array sorting
- Five array elements sorted by numeric size
First, the Dictionary class
A dictionary to save a collection of data that has a mapping relationship (Key-value pairs). for "name: Zhang San", Key is "name", key corresponding value is "Zhang San". A key-value to think of as an entry, a dictionary is a container for storing key-value pairs. Unlike arrays, dictionaries access elements by key. Key cannot be duplicated, value must be an object. Key-value pairs are stored in the dictionary in unordered order. dictionaries are also divided into immutable dictionaries (nsdictionary) and variable dictionaries (nsmutabledictionary).
Nsdictionary, immutable dictionary, inherit nsobject. Once a dictionary is created, key-value pairs cannot be changed, cannot be added, and cannot be deleted. Only key or value can be read.
/ * Immutable dictionary * /
/ * Create object * /
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:
@ "ZhangSan", @ "name",
@ "Man", @ "sex",
@ "18", @ "age",
@ "130", @ "weight",
nil];
/ * Output dictionary * /
NSLog (@ "% @", dic);
/ * Get all key values * /
[dic allKeys];
NSLog (@ "% @", [dic allKeys]);
/ * Get all Value values * /
[dic allValues];
NSLog (@ "% @", [dic allValues]);
/ * Query Value by Key * /
[dic objectForKey: @ "name"];
NSLog (@ "% @", [dic objectForKey: @ "name"]);
[dic valueForKey: @ "name"];
NSLog (@ "% @", [dic valueForKey: @ "name"]);
/ * Traverse dictionary * /
NSArray * keyArr = [dic allKeys];
for (int i = 0; i <keyArr.count; i ++) {
NSString * keyStr = [keyArr objectAtIndex: i];
NSLog (@ "% @", [dic objectForKey: keyStr]);
}
/ * Quick traverse * /
for (NSString * key in dic) {
NSLog (@ "% @", [dic objectForKey: key]);
}
/ * keyArr fast traversal * /
for (NSString * str in keyArr) {
NSLog (@ "% @", str);
}
nsmutabledictionary, variable dictionary, inheriting nsdictionary. You can add, delete, and change the key values for the management of the input line.
/ * Variable dictionary * /
NSMutableDictionary * mDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@ "LiSi", @ "name",
@ "18", @ "age",
@ "woman", @ "sex",
@ "120", @ "weight",
nil];
for (NSString * key in mDic) {
NSLog (@ "% @", [mDic objectForKey: key]);
}
/ * Increase key-value pairs * /
[mDic setObject: @ "170cm" forKey: @ "height"];
[mDic setValue: @ "volleyball" forKey: @ "hobby"];
for (NSString * key in mDic) {
NSLog (@ "% @", [mDic objectForKey: key]);
}
/ * Modify key-value pairs * /
[mDic setObject: @ "20" forKey: @ "age"];
for (NSString * key in mDic) {
NSLog (@ "% @", [mDic objectForKey: key]);
}
/ * Delete key-value pairs * /
[mDic removeObjectForKey: @ "hobby"];
for (NSString * key in mDic) {
NSLog (@ "% @", [mDic objectForKey: key]);
}
Second, the collection class
The Collection class *nsset, similar to the concept of a set in mathematics, is deterministic, unordered, and cross-specific *. That is, the elements stored in the collection must be object types, the stored elements are not sequential, and the stored elements are unique. The collection classes are also divided into Nsset and Nsmutableset.
The sample code for Nsset is as follows:
/ * Immutable Set * /
/ * Create * /
NSSet * set = [NSSet setWithObjects: @ "Siping", @ "Changchun", @ "Jilin", @ "Siping", nil];
/ * Get the number of Set elements * /
NSLog (@ "% lu", set.count);
/ * Get an element in Set * /
NSLog (@ "% @", [set anyObject]);
/ * Determine if a Set contains an object * /
if ([set containsObject: @ "shenyang"]) {
NSLog (@ "yes");
}
else {
NSLog (@ "no");
}
/ * Traversing the collection * /
for (NSString * str in set) {
NSLog (@ "% @", str);
}
The sample code for Nsmutableset is as follows:
/ * Variable collection * /
NSSet * set2 = [NSSet setWithObjects: @ "DaLian", @ "BeiJing", nil];
NSMutableSet * mSet = [NSMutableSet setWithSet: set2];
/ * Merge two collections * /
[mSet unionSet: set];
for (NSString * str in mSet) {
NSLog (@ "% @", str);
}
/ * Intersect two sets * /
[mSet intersectSet: set];
/ * Add element * /
[mSet addObject: @ "Tonghua"];
/ * Delete element * /
[mSet removeObject: @ "BeiJing"];
for (NSString * str in mSet) {
NSLog (@ "*% @ *", str);
}
Nscountedset is a subclass of Nsmutableset that can record the number of repetitions of an element. Added the Count function on the basis of Nsset.
The sample code for Nscountedset is as follows:
/ * NSCountedSet class * /
NSArray * arr = [NSArray arrayWithObjects: @ "Zhang", @ "LiSi", @ "LiSi", @ "Wang", @ "Wang", @ "LiSi", nil];
NSCountedSet * countSet = [NSCountedSet setWithArray: arr];
/ * View the number of countSet elements * /
NSLog (@ "% lu", countSet.count);
/ * LiSi count * /
NSLog (@ "% lu", [countSet countForObject: @ "LiSi"]);
Fast traversal of arrays, dictionaries, and collections
for (<#type *objectin{}
- object is a traversal of the resulting element objects.
- Collection is a collection-type object: An array, a dictionary, a collection.
Feature of collection type enumeration
- An array enumeration gets the element objects in the array.
- The Dictionary enumeration Gets the key value in the dictionary.
- The Collection enumeration Gets the element objects in the collection.
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:
@ "ZhangSan", @ "name",
@ "Man", @ "sex",
@ "18", @ "age",
@ "130", @ "weight",
nil];
/ * Quick traverse * /
for (NSString * key in dic) {
NSLog (@ "% @", [dic objectForKey: key]);
}
Four, array sorting
The sorting of the array depends on the judging condition, and the judging condition determines the sort of the type (ascending, descending). iOS provides a sorting method for an array, while providing an interface to let us pass judgment conditions.
NSMutableArray * mArr = [NSMutableArray arrayWithObjects: @ "3", @ "2", @ "1", @ "4", nil];
/ * Variable array sorting * /
/ * @selector-> method selector, get the meaning of the method name.
* compare:-> method of elements in the array (element is a string, compare is a method of string)
* /
[mArr sortUsingSelector: @selector (compare :)];
NSLog (@ "% @", mArr);
/ * Immutable array sorting * /
NSArray * arr = [NSArray arrayWithObjects: @ "4", @ "2", @ "1", @ "3", nil];
NSArray * sortArr = [arr sortedArrayUsingSelector: @selector (compare :)];
NSLog (@ "% @", sortArr);
Five, array elements sorted by numerical size
The elements stored in the array are object types, and if the stored elements are numeric objects, sorting by the above method will not result in the correct results. Therefore, you need to convert the string to a NSNumber object using Nsnumberformatter. The specific implementation code is as follows:
#pragma mark-Sorting numeric arrays
#if 1
NSArray * arr = @ [@ "1", @ "12", @ "122", @ "67", @ "50", @ "666"];
NSMutableArray * numArr = [NSMutableArray array];
for (NSString * string in arr) {
// NSNumberFormatter format conversion
NSNumberFormatter * format = [[NSNumberFormatter alloc] init];
// NSString becomes NSNumber
NSNumber * num = [format numberFromString: string];
[numArr addObject: num];
}
// Call the corresponding method according to the type of the elements in the array, which is equivalent to the condition judgment in the sort
NSLog (@ "% @", [numArr sortedArrayUsingSelector: @selector (compare :)]);
#endif
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Objective-c Learning Notes _ Dictionaries, collections, array sorting