The difference between Nsset and Nsarray

Source: Internet
Author: User

 the difference between 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.

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.

Summary of common methods of Nsset

+ (ID) setwithobjects:obj1,obj2,... nil Create a new collection with a set of objects
-(ID) initwithobjects:obj1,obj2,.... nil Initializes a newly allocated collection with a set of objects
-(Nsuinteger) Count Returns the number of members of a collection
-(BOOL) containsobject:obj Determines whether the collection contains object obj
-(BOOL) member:obj Determines whether the collection contains object obj
-(nsenumerator*) Objectenumerator Returns all objects in the collection to an object of type Nsenumerator
-(BOOL) Issubsetofset:nsset Determines whether a collection is a subset of Nsset
-(BOOL) Intersectsset:nsset Determines whether the intersection of two sets has at least one element
-(BOOL) Isequaltoset:nsset Determines whether two sets are equal



Summary of common methods of Nsmutableset

-(ID) setwithcapcity:size To create a new collection with size sizes
-(ID) initwithcapcity:size Initializes a newly allocated collection of size
-(void) addobject:obj Add Object obj to the collection
-(void) removeobject:obj To delete an object from the collection obj
-(void) removeallobjects Delete all objects in the collection
-(void) Unionset:nsset Add all elements of Nsset to the collection
-(void) Minusset:nsset Remove all nsset elements from the collection
-(void) Interectset:nsset Set and Nsset do intersection operations

The difference between Nsset and Nsarray

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.