iOS中枚举用法

来源:互联网 发布:mysql share nothing 编辑:程序博客网 时间:2024/05/29 11:13
NSLog(@"Hello, World!");
//字典初始化,key-value        NSDictionary *myDic=[[NSDictionary alloc]initWithObjectsAndKeys:@"张三",@"name",@"李四",@"name", nil];                NSUInteger count = [myDic count];          NSLog(@"词典的数量为:  %lu",count); //获取myDic中所有key的枚举器        NSEnumerator * myEnumerator = [myDic keyEnumerator];        for (NSObject *object in myEnumerator) {              NSLog(@"遍历KEY的值: %@",object);          }        //获取myDic的所有value值的枚举器        myEnumerator = [[myDic allValues] objectEnumerator];        NSString *value;        while((value = [myEnumerator nextObject]))        {            NSLog(@"遍历的值: %@",value);        }                //通过KEY找到value          NSObject *myObject = [myDic objectForKey:@"name"];                if (myObject != nil) {              NSLog(@"通过KEY找到的value是: %@",myObject);          }                  NSMutableDictionary *mydic2 = [NSMutableDictionary dictionaryWithCapacity:10];          [mydic2 setObject:@"Alex Hu" forKey:@"name"];          [mydic2 setObject:@"1388888888" forKey:@"mobile number"];         //获取mydic2字典的value值的枚举器        for (NSObject *object in [mydic2 objectEnumerator]) {              NSLog(@"遍历的值: %@",object);          }                NSSet *mySet=[NSSet setWithObjects:@"A",@"B",@"C",@"D",[NSNumber numberWithInteger:123], nil];        count=[mySet count];        NSLog(@"count= %lu",count);        //获取mySet的枚举器        myEnumerator=[mySet objectEnumerator];        for (NSObject *object in myEnumerator) {            NSLog(@"myEnumerator value=%@",object);            if ([object isEqualTo:@"A"]) {                NSLog(@"找到A了");            }            if ([object isEqual:@"B"]) {                NSLog(@"找到B");            }        }        //获取mySet的NSArray的值        NSArray *mySetArr=[mySet allObjects];        for (NSUInteger i=0; i<[mySetArr count];i++) {            NSLog(@"%lu =>%@",i,[mySetArr objectAtIndex:i]);        }                if ([mySet containsObject:@"D"]) {              NSLog(@"集合中包含 D这个对象");          }