"Go" OC base data type-nsset

Source: Internet
Author: User

Original: http://www.cnblogs.com/GISerYang/p/3340937.html

1, Collections: Collections (Nsset) and Arrays (Nsarray) have similarities, are the addresses of different objects, but Nsarray are ordered collections, Nsset are unordered collections.
A collection is a hash table that uses a hashing algorithm to find elements in a collection faster than an array, but it has no order.

set = [[Nsset alloc] Initwithobjects: @ "One", @ "one",@ "three",@ "Four ", nil]; 2 [// Returns the number of objects in the collection               

Determine if an element is in the collection

determines whether the collection has @ "2"BOOL ret = [set Containsobject:@ ""];     

Determines whether two sets are equal

1 Nsset * Set2 = [[Nsset alloc] initwithobjects:@ "One", @ "one", @"three",@ " four", nil);  Determines whether two sets are equal 3 BOOL ret = [set Isequaltoset:set2];         

Determines whether a set is a subset of Set2

 1 nsset * Set2 = [[Nsset alloc] Initwithobjects:@ " Span style= "color: #800000;" >one@" two ", @" three@ "four" five " Nil];// Determine if set is a subset of Set2 3 BOOL ret = [set Issubsetofset:set2];        

Collections can also use enumerators to traverse

The collection can also be traversed with an enumerator for 2 Nsenumerator * enumerator = [set objectenumerator];  3 NSString *str;  while (str = [Enumerator nextobject]) { ... 6}         

Initialize a collection by an array (array to collection)

1 Nsarray * array = [[Nsarray alloc] initwithobjects:@ "One", @ "one", @"three",  @ "four", nil];  set = [[Nsset alloc] initwitharray:array];             

Collection Conversions to arrays

1 Nsarray * array2 = [set allobjects];

2. Variable Set Nsmutableset

1//Variable set Nsmutableset2 Nsmutableset * set = [[Nsmutableset alloc] init];  3 [Set AddObject:@ "one"];  4 [Set AddObject:@ ""];  5 [Set AddObject:@ ""]; // If the added element has duplicates, it actually retains only one               

Delete Element

Delete Element 2 [set Removeobject:@ ""];  3 [set removeallobjects];        

Add the elements in the Set2 to the set, and if there are duplicates, keep only one

Add the elements in the Set2 to the set, and if there are duplicates, keep only one 2 Nsset * Set2 = [[Nsset alloc] initwithobjects:@ "Two",@ "  Three",@"four", nil];  3 [set Unionset:set2];             

Delete the same element in set as Set2

1 [set Minusset:set2];

3. Index set (index set) nsindexset

index set (Index collection) Nsindexset2 Nsindexset * indexset = [[Nsindexset alloc] Initwithindexesinrange:nsmakerange (/// the number in the collection is 123   

Extracts an element from a specified position in an array based on a collection

extracts an element from the specified position in the array according to the collection 2 Nsarray * array = [[Nsarray alloc] initwithobjects:@ "one"@"One", @ "three",@ "four", nil];  return @             "", @ "three" @ "four" 

4. Variable index set Nsmutableindexset

1 Nsmutableindexset *indexset =[[Nsmutableindexset alloc] init];2 [Indexset Addindex:0]3 [Indexset Addindex:3];4 [Indexset Addindex:5];5//Gets the specified element in the array by collection6 Nsarray *array = [[Nsarray alloc] Initwithobjects:@ "One", @ "one", @ " three",@ "four" @ "five"@ "six", nil];  7 Nsarray *newarray = [array objectsatindexes:indexset]; // return @ "one" @ "four" @ "six"              

"Go" OC base data type-nsset

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.