您的位置:首页 > 其它

oc之NSDictionaryAndGather

2015-08-04 10:18 387 查看
//
//  main.m
//  8_4_字典and集合
//
//  Created by lanou3g on 15/8/4.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

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

//OC字典

//不可变字典-----NSDictionary
//字典里面的键值对,key为任意类型的对象,实际上一般最多的是字符串,value值:任意类型的对象。
//第一种初始化方法
NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys: @"value1",@"key1",@"value2",@"key2", nil];

NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1", nil];

//语法糖,字典是大括号,数组是小括号。
//字典里面key值不可以重复,value值可以重复.如果key一样,对于不可变字典来说
NSDictionary *dict2 = @{
@"key1":@"value1",
@"key2":@"value2",
@"key1":@"value3"
};

NSLog(@"dict2  %@",dict2);

Person *p1 = [[Person alloc]initWithName:@"小丽" withSex:@"女"];
Person *p2 = [[Person alloc]initWithName:@"小张" withSex:@"女"];
Person *p3 = [[Person alloc]initWithName:@"小白" withSex:@"女"];
Person *p4 = [[Person alloc]initWithName:@"小乔" withSex:@"女"];

NSDictionary *dict4 = @{
@"person1":p1,
@"person2":p2,
@"person3":p2,
@"person4":p4,
};

//字典是无序的,不可以根据下标去取出某一value值,会自动对key值进行排序。

//1.拿出某个value值
NSLog(@"%@",[ dict4 objectForKey:@"person2"]);

//2.语法糖
NSLog(@"%@",dict4[@"person1"]);

NSLog(@"%lu",[dict4 count]);

//3.可以根据value值,拿出key值(可能拿出一堆值)

NSArray *arr =  [dict4 allKeysForObject:p2];
NSLog(@"%@",arr);

//4.取出字典里面的所有key值

NSArray *arr1 = [dict4 allKeys];
NSLog(@"%@",arr1);

//可变的字典

//初始化方法一
NSMutableDictionary * mudict = [NSMutableDictionary dictionaryWithCapacity:10];
//每次开辟10个空间,使用完后,再开辟10个。避免每次都开辟

//初始化方法二
NSMutableDictionary *dict5 = [NSMutableDictionary dictionary];

//可变的常用方法

[dict5 setObject:@"sdf" forKey:@"person5"];
//对于可变的字典,重复的key值,后面的覆盖前面的。
for (Person *p in dict5) {
NSLog(@"-------%@",p);
}
//删除一个元素
[dict5 removeObjectForKey:@"person5"];

//删除全部元素
[dict5 removeAllObjects];

//删除某几个元素
[dict5 setObject:@"" forKey:@"person5"];

#pragma mark -----------------集合

//无序,不可重复
NSSet *set = [NSSet setWithObjects:@"one",@"two",@"three",@"one", nil];

NSLog(@"%@",set);

//集合的常用方法

//随机取出某一个元素,
NSLog(@"%@",[set anyObject]);
NSLog(@"%lu",[set count]);

//是否包含某元素
NSLog(@"%d",[set containsObject:@"one"]);

NSSet *set1 = [NSSet setWithObjects:@"one",@"two", nil];

NSLog(@"%d",[set1 isSubsetOfSet:set]);

//可变集合

NSMutableSet *mutset = [NSMutableSet set];

//添加元素
[mutset addObject:@"xilali"];
[mutset addObject:@"songhuqiao"];
[mutset addObject:@"gaoyuanyuan"];
[mutset addObject:@"zhaowei"];
[mutset addObject:@"ziwei"];

//删除某个元素
[mutset removeObject:@"xilali"];

//删除全部元素
//        [mutset removeAllObjects];

//创建可变集合二

NSMutableSet *mutset1 = [NSMutableSet set];
[mutset1 addObject:@"gaoyuanyuan"];
[mutset1 addObject:@"zhaowei"];
[mutset1 addObject:@"ziwei"];
[mutset1 addObject:@"qiaoyaxin"];
[mutset1 addObject:@"pengjie"];

[mutset1 isSubsetOfSet:mutset1];
[mutset1 unionSet:mutset];
[mutset1 intersectSet:mutset];
[mutset1 minusSet:mutset];
NSLog(@"%@",mutset1);

//显示次数
NSCountedSet *set11 = [NSCountedSet setWithObjects:@"one",@"two",@"three", nil];
NSLog(@"%@",set11);

//计算次数

[set11 countForObject:@"one"];

//排序
NSArray *array = @[@"b",@"e",@"d"];

//只能升序
NSArray * array1 = [array sortedArrayUsingSelector:@selector(compare:)];

NSLog(@"%@",array1);

//sortUsingSelector用于可变数组,只能对字符串和数字,只能是其中的一种,上面的那个排序也一样。

NSArray *personArr = @[p1,p2,p3,p4];

//        NSArray *temp = [personArr sortedArrayUsingSelector:@selector(compare:)];
//
//
//        NSLog(@"%@",temp);

#pragma mark ----------快速遍历

//如果数组中类型不统一,用id,否则是什么类型就用什么类型

NSArray *array11 = @[@"曹操",@"刘备",@"关羽"];

for (id obj in personArr) {
NSLog(@"%@",obj);
}

//字典遍历的是key值
for (id obj in dict4) {
NSLog(@"%@",obj);
}

//所以要使用某种
for (id obj in dict4) {
NSLog(@"%@",dict4[obj]);
}

//取出字典里面所有的key值

NSArray *keysArray = [dict4 allKeys];
//对key进行排序

keysArray = [keysArray sortedArrayUsingSelector:@selector(compare:)];
//遍历keysArray,取出value值
for (id obj in dict4) {
NSLog(@"%@",dict4[obj]);
}

//forin用于集合set

NSSet *setTest = [NSSet setWithObjects:@"gxm",@"xh",@"yhy", nil];

for (id obj in setTest) {
NSLog(@"%@",  obj);
}
}
return 0;
}


重写description方法,可以改变%@的输出结果,不只是输出地址。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: