Directly on the code:
/ *
* Quick enumeration
*
* /
NSLog (@ "++++++++++++++++++");
NSArray * testArray = @ [@ 1, @ 2, @ 3, @ 4, @ 5];
// Iterate through array elements by fast enumeration
for (NSArray * object in testArray) {
NSLog (@ "% @", object);
}
// Use id when in doubt
for (id object in testArray) {
NSLog (@ "% @", object);
}
// Quickly traverse the collection
for (id object in set1) {
NSLog (@ "% @", object);
}
// Quickly traverse the dictionary (get the keys of the dictionary)
// Directly traverse the dictionary to get each key of the dictionary, you can get the corresponding value by traversing the obtained keys
for (id object in dic1) {
NSLog (@ "% @", object);
}
// dic1 [key] can get the corresponding value, this is a syntactic sugar, equivalent to [dic1 objectForKey: key]
for (NSString * key in dic1) {
NSLog (@ "dictionary [% @]:% @", key, dic1 [key]);
}
/ *
* Array Sort
*
* /
// Note that when initializing the array, all array element objects have the same type, and an error will occur as follows: @ [@ 1, @ 2, @ "5", @ 3, @ 4]
NSArray * array1 = @ [@ 1, @ 2, @ 5, @ 3, @ 4];
// Use the array sort method to sort the array in ascending order
NSArray * resultArray = [array1 sortedArrayUsingSelector: @selector (compare :)];
NSLog (@ "% @", resultArray);
Student * stu1 = [Student studentWithName: @ "wang" score: @ 85];
Student * stu2 = [Student studentWithName: @ "zhen" score: @ 95];
Student * stu3 = [Student studentWithName: @ "gang" score: @ 65];
NSMutableArray * stus = [NSMutableArray array];
[stus addObject: stu1];
[stus addObject: stu2];
[stus addObject: stu3];
[stus sortedArrayUsingSelector: @selector (scoreAscending :)];
NSLog (@ "% @", stus);
[stus sortedArrayUsingSelector: @selector (scoreDescending :)];
NSLog (@ "% @", stus);
[stus sortedArrayUsingSelector: @selector (nameAscending :)];
NSLog (@ "% @", stus);
[stus sortedArrayUsingSelector: @selector (nameDescending :)];
NSLog (@ "% @", stus);
OBJECTIVE-C----Fast enumeration, array sorting