The first chapter of IOS reading notes

Source: Internet
Author: User

1. Determine if an instance or class method is available.

1) Use the NSObject class method Instancesrespondtoselector: To determine whether a particular selector exists in an instance of the class.

    Nsarray *array = @[@ "1", @ "2"];        if ([Nsarray instancesrespondtoselector: @selector (sortusingcomparator:)]) {        //do something use Sortusingcomparator:            }else if ([Nsarray instancesrespondtoselector: @selector (sortusingfunction:context:)]) {        //do something use Sortusingfunction:context:            } else {            }        //or you can use    if ([Array respondstoselector: @selector (sortusingcomparator:)]) {        //do something Use Sortusingcomparator:            }else if ([Array respondstoselector: @selector (sortusingfunction:context:)]) {        // Do something use Sortusingfunction:context:            }else {            }

2. Determine if a class is available at run time

1) Use the Nsclassformstring function. You can pass this method to the class name that you want to use as a string. If this function returns a value of 0, this class is not available in this device;

    

    if (nsclassfromstring (@ "nsjsonserialization") = nil) {        //can use    }else {        //Not available            }

The difference between 3.NSSet and Nsarray

Nsset exactly what type, in fact it and nsarray functional nature, for storing objects, belong to the collection; Nsset, Nsmutableset class declares programming interface objects, unordered collections, which are stored in memory in a discontinuous way, unlike Nsarray, Nsdictionary (all ordered collections) class declares that the programming interface object is an ordered set, where the memory location is contiguous;

Nsset and our Common Nsarry difference is: In the search for an element nsset more efficient than nsarray, mainly because it uses an algorithm hash (hash, can also be translated as a hash); The development documentation explains this: you can use sets as an Alternative to arrays when the order of elements isn ' t important and performance in testing whether a object is contained The set is a consideration-while arrays was ordered, testing for membership was slower than with sets.

An object in Nsset is allowed to be added only once. The second time you try to add the same object, it will be rejected.

For example, if you want to store element A, a hash algorithm can directly find the location where a should be stored, and also, when you want to access a, a hash process can find the location of a storage. For Nsarray, if you want to know if a is not in the array, you need to facilitate the entire array, obviously less efficient;

Nsset,nsarray are classes, you can add only cocoa objects, and if you need to include basic data types (int,float,bool,double, etc.), you need to encapsulate the data into a nsnumber type.

Where Containsobject: Methods support generic Types. If you want to support a custom class, you overload the class with the-(BOOL) IsEqual: (ID) object;

For example:

  

#import <Foundation/Foundation.h> @interface rrpdemoclass:nsobject {        nsstring    *name;    NSNumber    *age;} @property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSNumber *age; @end #import "RRPDemoClass.h" @ implementation rrpdemoclass@synthesize name; @synthesize age;-(BOOL) IsEqual: (ID) object {        if (![ Object Iskindofclass:[rrpdemoclass class]] {        return NO;    }    Rrpdemoclass *a = (Rrpdemoclass *) object;    BOOL result = NO;    result = [Self.name isequaltostring:a.name];    if (!result) {        return result;    }    result = [Self.age integervalue] = = [A.age integervalue]? Yes:no;    return result;} @end

  

      

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.