The use of Objective-c:nsset and Nsmutbaleset

Source: Internet
Author: User


    1. #import <Foundation/Foundation.h>
    2. int main (int argc, const char * argv[])
    3. {
    4. @autoreleasepool {
    5. Nsset *set1 = [Nsset setwithobjects:@ "a", @ "B", @ "C", @ "D", nil];
    6. Nsset *set2 = [[Nsset alloc] initwithobjects:@ "1", @ "2", @ "3", nil];
    7. Nsarray *array = [Nsarray arraywithobjects:@ "a", @ "B", @ "C", nil];
    8. Nsset *set3 = [Nsset Setwitharray:array];
    9. NSLog (@ "Set1:%@", Set1);
    10. NSLog (@ "Set2:%@", Set2);
    11. NSLog (@ "Set3:%@", Set3);
    12. Get the number of collections
    13. NSLog (@ "Set1 count:%d", set1.count);
    14. Gets all the objects in the collection as an array
    15. Nsarray *allobj = [Set2 allobjects];
    16. NSLog (@ "AllObj:%@", allobj);
    17. Get an arbitrary object
    18. NSLog (@ "Anyobj:%@", [Set3 Anyobject]);
    19. Whether to include an object
    20. NSLog (@ "contains:%d", [Set3 containsobject:@ "Obj2"]);
    21. Whether to include objects in the specified set
    22. NSLog (@ "intersect obj:%d", [Set1 Intersectsset:set3]);
    23. is exactly matched
    24. NSLog (@ "IsEqual:%d", [Set2 Isequaltoset:set3]);
    25. Whether it is a child collection
    26. NSLog (@ "Issubset:%d", [Set3 Issubsetofset:set1]);
    27. Nsset *set4 = [Nsset setwithobjects:@ "a", @ "B", nil];
    28. Nsarray *ary = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil];
    29. Nsset *set5 = [Set4 setbyaddingobjectsfromarray:ary];
    30. NSLog (@ "Addfromarray:%@", SET5);
    31. Nsmutableset *mutableset1 = [Nsmutableset setwithobjects:@ "1", @ "2", @ "3", nil];
    32. Nsmutableset *mutableset2 = [Nsmutableset setwithobjects:@ "a", @ "2", @ "B", nil];
    33. Nsmutableset *mutableset3 = [Nsmutableset setwithobjects:@ "1", @ "C", @ "B", nil];
    34. Subtract from collection element
    35. [MutableSet1 Minusset:mutableset2];
    36. NSLog (@ "Minus:%@", MutableSet1);
    37. Leaving only the equal elements
    38. [MutableSet1 Intersectset:mutableset3];
    39. NSLog (@ "intersect:%@", MutableSet1);
    40. Merging collections
    41. [MutableSet2 Unionset:mutableset3];
    42. NSLog (@ "union:%@", MutableSet2);
    43. Delete the specified element
    44. [MutableSet2 removeobject:@ "a"];
    45. NSLog (@ "Removeobj:%@", MutableSet2);
    46. Delete all data
    47. [MutableSet2 removeallobjects];
    48. NSLog (@ "RemoveAll:%@", MutableSet2);
    49. }
    50. return 0;
    51. }

The use of Objective-c:nsset and Nsmutbaleset

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.