淘先锋技术网

首页 1 2 3 4 5 6 7
//
//  main.m
//  test
//
//  Created by Bin Shen on 12/28/14.
//  Copyright (c) 2014 Bin Shen. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
    
        /* ---------- NSArray ---------- */

        //数组的创建
        NSArray *array1 = [NSArray arrayWithObject:@"one"];
        NSArray *array2 = [NSArray arrayWithObjects:@"one", @"two", nil];
        NSArray *array3 = [NSArray arrayWithArray:array2];
        NSLog(@"array1 : %@", array1);
        NSLog(@"array2 : %@", array2);
        NSLog(@"array3 : %@", array3);
        NSArray *array4 = [[NSArray alloc] initWithArray:array1];
        NSLog(@"array4 : %@", array4);

        //获取数组中的元素个数和访问
        long count = [array2 count];
        NSLog(@"array2 all element :%ld", count);
        NSString *string1 = [array2 objectAtIndex:1];
        NSLog(@"element of array: %@", string1);
        
        //追加数组中的内容
        NSArray * array5 = [array2 arrayByAddingObject:@"end"];
        NSLog(@"array5 : %@", array5);
        
        //数组 -> 字符串
        NSString *string2 = [array5 componentsJoinedByString:@","];
        NSLog(@"string2 : %@", string2);

        //判断数组中是否存在指定的对象
        BOOL isContain = [array5 containsObject:@"end"];
        NSLog(@"isContain : %d", isContain);
        
        //根据指定的对象返回索引下标,返回数组中最后一个元素
        NSInteger index = [array5 indexOfObject:@"end"];
        NSLog(@"index : %ld", index);
        NSString *lastObject = [array5 lastObject];
        NSLog(@"lastObject : %@", lastObject);
        
        
        // 可变数组
        // 创建一个空的数组
        //NSMutableArray *mutableArray1 = [NSMutableArray array];
        //NSMutableArray *mutableArray2 = [NSMutableArray arrayWithCapacity:5];
        NSMutableArray *mutableArray1 = [NSMutableArray arrayWithObjects:@"one", @"two", @"end", @"Five", nil];
        NSLog(@"mutableArray1 : %@", mutableArray1);
        // 添加,替换,删除
        [mutableArray1 addObject:@"Four"];
        NSLog(@"mutableArray1 : %@", mutableArray1);
        [mutableArray1 insertObject:@"Three" atIndex:2];
        NSLog(@"mutableArray1 : %@", mutableArray1);
        [mutableArray1 removeLastObject];
        NSLog(@"mutableArray1 : %@", mutableArray1);
        [mutableArray1 removeObject:@"end"];
        NSLog(@"mutableArray1 : %@", mutableArray1);
        [mutableArray1 removeObjectAtIndex:3];
        NSLog(@"mutableArray1 : %@", mutableArray1);
        
        [mutableArray1 addObject:@"Four"];
        NSLog(@"add four : %@", mutableArray1);
        NSArray *deleteArray = [NSArray arrayWithObjects:@"one", @"two", nil];
        [mutableArray1 removeObjectsInArray:deleteArray];
        NSLog(@"removeObjectsInArray : %@", mutableArray1);
        
        [mutableArray1 replaceObjectAtIndex:0 withObject:@"3"];
        NSLog(@"replace : %@", mutableArray1);
        
        [mutableArray1 removeAllObjects];
        NSLog(@"empty array : %@", mutableArray1);

        // 遍历数组
        NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];
        
        for (int index = 0; index < [array count]; index++) {
            NSString *string = [array objectAtIndex:index];
            NSLog(@"string : %@", string);
        }
        
        for (NSString *string in array) {
            NSLog(@"string : %@", string);
        }
        
        for (id string in array) {
            NSLog(@"string : %@", string);
        }
 
        /* ---------- NSDictionary ---------- */

        // 字典的创建
        NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"value" forKey:@"k1"];
        NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", @"v3", @"k3", @"v4", @"k4", @"v5", @"k5", nil];
        NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic1];
        NSLog(@"dic1 : %@", dic1);
        NSLog(@"dic2 : %@", dic2);
        NSLog(@"dic3 : %@", dic3);

        //获取字段的数量
        long count = [dic2 count];
        NSLog(@"count : %ld", count);
        //获取字典的value
        NSString *string = [dic2 objectForKey:@"k3"];
        NSLog(@"string : %@", string);
        //获取字段所有的key和value
        NSArray *keyArray = [dic2 allKeys];
        NSArray *valueArray = [dic2 allValues];
        NSLog(@"keyArray : %@", keyArray);
        NSLog(@"valueArray : %@", valueArray);
        
        
        //可变字典
        NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", @"v3", @"k3", @"v4", @"k4", @"v5", @"k5", nil];
        NSDictionary *dic4 = [NSDictionary dictionaryWithObject:@"v5" forKey:@"k6"];
        [mutableDic addEntriesFromDictionary:dic4];
        NSLog(@"mutableDic : %@", mutableDic);
        [mutableDic setValue:@"object" forKey:@"key"];
        NSLog(@"mutableDic : %@", mutableDic);

        NSMutableDictionary *mutableDic2 = [NSMutableDictionary dictionary];
        [mutableDic2 setDictionary:mutableDic];
        NSLog(@"mutableDic2 : %@", mutableDic2);

        [mutableDic2 removeObjectForKey:@"k4"];
        NSLog(@"mutableDic2 : %@", mutableDic2);
        
        NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
        [mutableDic2 removeObjectsForKeys:keys];
        NSLog(@"mutableDic2 : %@", mutableDic2);
        
        [mutableDic2 removeAllObjects];
        NSLog(@"empty mutableDic2 : %@", mutableDic2);

        //遍历字典
        for (int index = 0; index < [mutableDic count]; index++) {
            NSString *object = [mutableDic objectForKey:[[mutableDic allKeys] objectAtIndex:index]];
            NSLog(@"object : %@", object);
        }
        
        for (id key in mutableDic) {
            NSString *object = [mutableDic objectForKey:key];
            NSLog(@"object : %@", object);
        }

        NSEnumerator *enumerator = [mutableDic keyEnumerator];
        id key;
        while (key = [enumerator nextObject]) {
            id object = [mutableDic objectForKey:key];
            NSLog(@"object : %@", object);
        }
 
        /* ---------- NSSet ---------- */
        // 集合的创建
        NSSet *set1 = [NSSet setWithObjects:@"1", @"2", @"3", @"4", nil];
        NSSet *set2 = [[NSSet alloc] initWithObjects:@"3", @"4", @"2", @"1", nil];
        NSArray *array1 = [NSArray arrayWithObjects:@"7", @"10", nil];
        NSSet *set3 = [NSSet setWithArray:array1];
        NSSet *set4 = [NSSet setWithSet:set1];
        NSLog(@"set1 : %@", set1);
        NSLog(@"set2 : %@", set2);
        NSLog(@"set3 : %@", set3);
        NSLog(@"set4 : %@", set4);
        
        // 获取集合的个数
        long count = [set2 count];
        NSLog(@"count %ld", count);
        
        // 将集合返回成一个数组
        NSArray *objects = [set1 allObjects];
        NSLog(@"objects %@", objects);

        // 返回一个任意对象
        id object = [set3 anyObject];
        NSLog(@"anyObject : %@", object);
        
        // 是否包含某个元素
        BOOL isContain = [set4 containsObject:@"1"];
        NSLog(@"isContain : %d", isContain);
        
        // 集合间是否存在交集
        BOOL isInterset = [set1 intersectsSet:set2];
        NSLog(@"isInterset : %d", isInterset);

        //集合是否匹配
        BOOL isEqual = [set1 isEqualToSet:set2];
        NSLog(@"isEqual : %d", isEqual);
        
        //集合是否是另一个几个的子集
        BOOL isSub = [set1 isSubsetOfSet:set2];
        NSLog(@"isSub : %d", isSub);
        
        NSSet *set5 = [NSSet setWithObject:@"one"];
        //
        NSSet *appSet1 = [set5 setByAddingObject:@"two"];
        NSLog(@"appSet1 : %@", appSet1);
        //
        NSSet *appSet2 = [set5 setByAddingObjectsFromSet:set3];
        NSLog(@"appSet2 : %@", appSet2);
        //
        NSArray *array2 = [NSArray arrayWithObjects:@"end", nil];
        NSSet *appSet3 = [set5 setByAddingObjectsFromArray:array2];
        NSLog(@"appSet3 : %@", appSet3);

        //
        NSMutableSet *set6 = [NSMutableSet setWithObjects:@"1", @"2", @"a", nil];
        NSMutableSet *set7 = [NSMutableSet setWithObjects:@"1", @"3", @"a", nil];
        //减去
//        [set6 minusSet:set7];
//        NSLog(@"minusSet : %@", set6);
        //交集
//        [set6 intersectSet:set7];
//        NSLog(@"intersectSet : %@", set6);
        
//        [set6 unionSet:set7];
//        NSLog(@"minusSet : %@", set6);
        
//        [set6 removeObject:@"a"];
//        NSLog(@"removeObject : %@", set6);

    }
    return 0;
}