main.m// nsset//// Created by passer-by on 15-3-23.// Copyright (c) 2015 MAC. All rights reserved.//#import <foundation/foundation.h>int Main (int argc, const char * argv[]) { @autor eleasepool { /* Nsset vs. Nsarray &NB Sp 1> Common * are collections that can hold multiple OC objects * can only hold OC objects, cannot save Put non-OC object type (basic data type: int, char, float, etc., struct, enum) * itself immutable, there is a variable subclass 2> different points * Nsarray in order, Nsset no order &NB Sp */ Nsset *set1 = [Nsset setwithobjects:@ "a", @ "b "@" C ", @" D ", nil]; Nsset *set2 = [[Nsset alloc] initwithobjects:@" 1 "@" 2 "@" 3 ", nil]; Nsarray *array = [Nsarray arraywithobjects:@ "a", @ "B", @ "C", nil]; Nsset *set3 = [Nsset setwitharray:array];& nbsp; NSLog (@ "Set1:%@", Set1); NSLog (@ "s Et2:%@ ", Set2); NSLog (@" Set3:%@ ", Set3); //Get Set number NSLog (@ "Set1 count:%d", Set1.count); & nbsp //Get all objects in the collection as an array Nsarray *allobj = [Set2 allobjects]; &NBSP ; NSLog (@ "AllObj:%@", AllObj); //Get any object &N Bsp NSLog (@ "Anyobj:%@", [Set3 Anyobject]); //contains an object &NBS P NSLog (@ "contains:%d", [Set3 containsobject:@ "Obj2"]); &NBsp; //whether to include objects in the specified set NSLOG (@ "intersect obj:%d", [Set1 Intersectsset: SET3]); //full match NSLog (@ "IsEqual:% D ", [Set2 Isequaltoset:set3]); //Whether it is a subset of the &N Bsp NSLog (@ "Issubset:%d", [Set3 Issubsetofset:set1]); & nbsp; Nsset *set4 = [Nsset setwithobjects:@ "a", @ "B", nil]; &N Bsp Nsarray *ary = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil]; Nsset *set 5 = [Set4 setbyaddingobjectsfromarray:ary]; NSLog (@ "Addfromarray:%@", Set5); & nbsp &N Bsp Nsmutableset *mutableset1 = [Nsmutableset setwithobjects:@ "1", @ "2", @ "3", nil]; Nsmutableset *mutableset2 = [Nsmutableset setwithobjects:@ "a", @ "2", @ "B", nil]; Nsmutableset *mutableset3 = [Nsmutableset setwithobjects:@ "1", @ "C", @ "B", nil]; //Set element subtraction [MutableSet1 minusset:mutableset2]; NSLog (@ "Minus:%@", Mutableset 1); //leave only equal elements [MutableSet1 intersec tset:mutableset3]; NSLog (@ "intersect:%@", MutableSet1); & nbsp //merge set [MutableSet2 unionset:mutableset3]; Nslo G (@ "union:%@", MutableSet2); //delete specified elements &NB Sp [MutableSet2 Removeobject:@ "A"]; NSLog (@ "Removeobj:%@", MutableSet2); & nbsp //delete all data [MutableSet2 removeallobjects]; NSLog (@ "RemoveAll:%@", MutableSet2); } R Eturn 0;}
iOS Note-nsset