OC中的重要知识点

1.判断一个数组为空的直接可以用.count 来进行判定,例如if(array.count == 0)

2. 遍历数组,字典,集合的三种方法,for ,forin,NSEnumerator

        1.数组

NSArray *cityArray = [NSArray arrayWithObjects:@"中国北京",@"中国郑州",@"中国洛阳",@"中国杭州",@"中国香港",@"中国台湾", nil];

       2.字典
        NSDictionary *personInforDic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"nan",@"sex",[NSNumber numberWithInt:28],@"age",nil];

        3.集合
        NSSet *stringSet = [NSSet setWithObjects:@"123",@"456",@"789",@"0X0",nil];

       三种遍历方式

 /*
         for循环遍历
         */
        //遍历数组
        for(int i = 0;i < cityArray.count;i++){
           NSString *string = [cityArray objectAtIndex:i];
            NSLog(@"cityArray[%d] = %@",i,string);
        }
        //遍历字典
        NSArray *keys = [personInforDic allKeys];
        for (int i = 0; i < personInforDic.count; i++) {
            NSString *key = [keys objectAtIndex:i ];
            NSString *value  = [personInforDic valueForKey:key];
            NSLog(@"%@ = %@",key,value);
        }
        //遍历集合
        NSArray *setObject = [stringSet allObjects];
        for (int i = 0; i < stringSet.count; i++) {
            NSLog(@"%@",[setObject objectAtIndex:i]);
        }
        
        /*
         NSEnumerator
         */
        //NSEnumerator 数组----正序遍历
        NSEnumerator *arrEnu = [cityArray objectEnumerator];
        
        id value = nil;
        while (value = [arrEnu nextObject]) {
            NSLog(@"%@",value);
        }
        NSLog(@"------------------");
        //NSEnumerator 数组----倒序遍历
        NSEnumerator *arrayEnu = [cityArray reverseObjectEnumerator];
        id value1 = nil;
        while (value1 = [arrayEnu nextObject]) {
            NSLog(@"%@",value1);
        }
        
        //NSEnumerator 字典
        NSEnumerator *arrEnu1 = [personInforDic objectEnumerator];
        
        id value2 = nil;
        while (value2 = [arrEnu1 nextObject]) {
            NSLog(@"%@",value2);
        }
        //NSEnumerator 集合
        NSEnumerator *setEnu = [stringSet objectEnumerator];
        id value3 = nil;
        while (value3 = [setEnu nextObject]) {
            NSLog(@"%@",value3);
        }
        /*
         for----in 遍历
         */
        //for----in 遍历数组
        for (NSString *cityName in cityArray) {
            NSLog(@"cityName = %@",cityName);
        }
        //for----in 遍历字典
        for (NSString *key in personInforDic) {
            NSLog(@"%@ = %@",key,[personInforDic objectForKey:key]);
        }
        //for----in 遍历集合
        for (NSString *ss in setEnu) {
            NSLog(@"%@",ss);
        }

3.//生成排序描述符 --------按人的姓名降序
        NSSortDescriptor *sortWithName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
        
        //通过排序描述符对数组进行排序
        [marray sortUsingDescriptors:[NSArray arrayWithObject: sortWithName]];
        NSLog(@"%@",marray);

posted @ 2016-01-16 20:09  恒远也  阅读(189)  评论(0编辑  收藏  举报