OBJECTIVE-C----Fast enumeration, array sorting

Source: Internet
Author: User
Tags array sort


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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.