Common classes 5 in the foundation of objective-C: nsset and nsmutableset in the Set

Source: Internet
Author: User

In the foundation framework, a set of Single-value objects is provided, and the elements in the nsset instance are unordered. Only one object can be saved, similar to the hashset in Java. Java and OC are too similar.

1. Continue to be immutable, nsset

Nsset is similar to array creation.Objective-CMediumNssetAndNsarrayThe difference between them will be detailed later.

 nsset * set1 = [nsset setwithobjects: @ "one", @ "two", @ "three" Nil]; // Similarly, nil cannot be less. // create nsarray * array = [nsarray arraywithobjects: @ "hello", @ "world", @ "hao", nil] Through arrays; nsset * set2 = [nsset setwitharray: array]; // construct nsset * set3 = [nsset setwithset: set2] through an existing set; // The number of objects in the Set nsinteger * objnum = [set3 count]; // return all objects in the set in the form of arrays nsarray * array1 = [set3 allobjects]; // check whether the collection contains an object bool iscontain = [set3 containsobject: @ "one"]; // whether the set2 set is the sub-set bool issub = [set2 issubsetofset: set3]; // whether the two sets are the same bool isequal = [set3 is1_toset: set2]; // Add an element based on the original set to create a new set nsset * set4 = [set3 setbyaddingobject: @ "Yade"]; // combine the two sets into a new set nsset * set5 = [set3 setbyaddingobjectsfromset: set2]; // merge an array of a set into a new set nsset * set6 = [set5 setbyaddingobjectsfromarray: array]; 

2. Variable set nsmutableset

Inherited from nsset

 
// Create two statements first. Then nsmutableset * mset1 = [nsmutableset setwithobjects: @ "one", @ "two", @ "1", nil]; nsmutableset * mset2 = [nsmutableset setwithobjects: @ "three", @ "four", @ "1" Nil]; // remove the same elements as mset2 in mset1, the returned result is mset2 [mset2 minusset: mset1]; // get the intersection of the two sets, and the returned result is mset1 [mset1 intersectset: mset2]; // get the Union set [mset1 unionset: mset2]; // set the content in one set to [mset1 setset: mset2] in another set; // Delete the object [mset1 removeobject: @ "one"]; // Delete All [mset1 removeallobjects];

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.