A preliminary knowledge of objective-c collection

Source: Internet
Author: User



Nsarray ordered Natural order



Nsset disorderly.



Nsset cannot store duplicate data, it can be used to remove duplicate data



1. Create a Collection



1.1 Creating immutable Collections





 
 
1     NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"two",@"three", nil];
2     NSLog(@"%@",set);
3     
4     NSSet * set1 = [[NSSet alloc] initWithObjects:@"one",@"two",@"one",@"three", nil];
5     NSLog(@"%@",set1);


1.2 Creating a Mutable collection


 
1         NSMutableSet * muset = [[NSMutableSet alloc] initWithObjects:@"one",@"two", nil];
2         
3         NSLog(@"%@",muset);





2. Get the number of elements



1 nsuinteger count = [Set1 count]; 2 NSLog (@ "%lu", count);



3. Determine if the object is included


1 BOOL isContain = [set1 containsObject: @ "T"];
2 if (isContain) {
3 NSLog (@ "include");
4} else {
5 NSLog (@ "excluding");
6}


-(BOOL) Containsobject: (ObjectType) AnObject;






4. Array conversion to Collection


 
 
1     NSArray * arr = @[@"one",@"two",@"three",@"one"];
2     NSSet * set2 =[NSSet setWithArray:arr];
3     NSLog(@"set2 %@",set2);





5. Variable array additions and deletions



5.1 Add



1 [muset addobject:@ "three"]; 2 NSLog (@ "%@", muset);



5.2 Delete



1 [muset removeobject:@ "one"]; 2 NSLog (@ "%@", muset);



Delete all



1 [Muset removeallobjects]; 2 NSLog (@ "%@", muset);






A preliminary knowledge of objective-c collection


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.