欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS学习之NSSet和NSMutableSet测试讲解

程序员文章站 2022-07-05 22:13:08
1、nsset、nsmutableset nsset元素无序、不能重复 nsmutableset元素无序、不能重复,有一些向集合中增加元素的功能、删除元素....... 2、测试demo int...

1、nsset、nsmutableset

nsset元素无序、不能重复

nsmutableset元素无序、不能重复,有一些向集合中增加元素的功能、删除元素.......

2、测试demo

int main(int argc, char * argv[]) {
 @autoreleasepool {
  
  nsset *set = [nsset setwithobjects:@"chenyu", @"hello", @"word", @"see", nil];
  set = [set setbyaddingobject:@"chenxuan"];
  for (id object in set)
  {
nslog(@"%@", object);
  }
  nsset *set1 = [nsset setwithobjects:@"chenyu1", @"hello1", @"word", @"see", @"see", @"hello1", nil];
  //set和set1的并集
  nslog(@".......");
  nsset *s = [set setbyaddingobjectsfromset:set1];
  for (id object in s)
  {
nslog(@"%@", object);
  }
  //是否有交集
  nslog(@"set1 和 set 是否有交集:%d", [set1 intersectsset:set]);
  //set1是否是set集合的子集
  
  bool bo = [set1 containsobject:@"see"];
  nslog(@"set1 是否包含 see %d", bo);
  
  nslog(@"-------");
  nsmutableset *set2 = [nsmutableset setwithcapacity:10];
  [set2 addobject:@"chenyu"];
  [set2 addobject:@"hello"];
  [set2 addobject:@"hello"];
  [set2 addobject:@"hello"];
  [set2 addobject:@"hello1"];
  [set2 addobject:@"hello3"];
  nslog(@"-------");
  for (id object in set2)
  {
nslog(@"%@", object);
  }
  nslog(@"-------");
  [set2 removeobject:@"hello1"];
  for (id object in set2)
  {
nslog(@"%@", object);
  }
 }
}

3、运行结果


2018-07-18 23:26:20.748305+0800 cytest[33554:12766983] hello
2018-07-18 23:26:20.748415+0800 cytest[33554:12766983] chenyu1
2018-07-18 23:26:20.748554+0800 cytest[33554:12766983] word
2018-07-18 23:26:20.749114+0800 cytest[33554:12766983] set1 和 set 是否有交集:1
2018-07-18 23:26:20.749626+0800 cytest[33554:12766983] set1 是否包含 see 1
2018-07-18 23:26:20.749838+0800 cytest[33554:12766983] -------
2018-07-18 23:26:20.750052+0800 cytest[33554:12766983] -------
2018-07-18 23:26:20.773300+0800 cytest[33554:12766983] chenyu
2018-07-18 23:26:20.789523+0800 cytest[33554:12766983] hello1
2018-07-18 23:26:20.791472+0800 cytest[33554:12766983] hello3
2018-07-18 23:26:20.791896+0800 cytest[33554:12766983] hello
2018-07-18 23:26:20.797038+0800 cytest[33554:12766983] -------
2018-07-18 23:26:20.800879+0800 cytest[33554:12766983] chenyu
2018-07-18 23:26:20.804327+0800 cytest[33554:12766983] hello3
2018-07-18 23:26:20.804808+0800 cytest[33554:12766983] hello