- 该笔记源自对传智播客《IOS开发快速入门视频》的学习
- 如有侵权,请联系本人删除。
- 都是比较基础的OC知识,中高级开发者可以忽略本文
- 很多重要内容在代码注释中
// NSDictionary是不可变的
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
//{k = v;}
// 最常用的初始化方式
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil];
NSArray *objects = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
// {k1 = v1;k2 = v2;k3 = v3;}
NSLog(@"%@", dict);
有多少个键值对(key-value)
dict.count
由于NSDictionary是不可变的,所以只能取值,而不能修改值
id obj = [dict objectForKey:@"k2"];
将字典写入文件中
NSString *path = @"/Users/apple/Desktop/dict.xml"; [dict writeToFile:path atomically:YES];
从文件中读取内容
dict = [NSDictionary dictionaryWithContentsOfFile:path];
返回所有的key或者value
NSArray *keys = [dict allKeys];
NSArray *objects = [dict allValues];
根据多个key取出对应的多个value
当key找不到对应的value时,用marker参数值代替
objects = [dict objectsForKeys:[NSArray arrayWithObjects:@"k1", @"k2", @"k4", nil] notFoundMarker:@"not-found"];
结果:objects= (v1,v2,“not-found”)
//方法一 for循环
for (id key in dict) {
id value = [dict objectForKey:key];
NSLog(@"%@=%@", key, value);
}
//方法二 key迭代器
NSEnumerator *enumer = [dict keyEnumerator];
id key = nil;
while ( key = [enumer nextObject]) {
id value = [dict objectForKey:key];
NSLog(@"%@=%@", key, value);
}
//k3=v3 k2=v2 k1=v1
for (NSObject *object in [dict objectEnumerator]) {
NSLog(@"对象迭代器遍历的值: %@",object);
}
//v3 v2 v1
//方法三 use block
[dict enumerateKeysAndObjectsUsingBlock:
^(id key, id obj, BOOL *stop) {
NSLog(@"%@=%@", key, obj);
}];
Student *stu1 = [Student studentWithName:@"stu1"];
Student *stu2 = [Student studentWithName:@"stu2"];
Student *stu3 = [Student studentWithName:@"stu3"];
// 一个对象称为字典的key或者value时,会做一次retain操作,也就是计数器会+1
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
stu1, @"k1",
stu2, @"k2",
stu3, @"k3", nil];
// 当字典被销毁时,里面的所有key和value都会做一次release操作,也就是计数器会-1
可变字典的使用
// 创建一个空的字典
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
Student *stu1 = [Student studentWithName:@"stu1"];
Student *stu2= [Student studentWithName:@"stu2"];
// 添加元素
// stu1的计数器会+1
[dict setObject:stu1 forKey:@"k1"];
NSLog(@"stu1:%zi", [stu1 retainCount]);
// 添加其他字典other到当前字典dict中
NSDictionary *other = [NSDictionary dictionaryWithObject:@"v2" forKey:@"k2"];
[dict addEntriesFromDictionary:other];
// 删除所有的键值对
// [dict removeAllObjects];
// 删除k1对应的元素stu1,stu1会做一次release操作
[dict removeObjectForKey:@"k1"];
NSLog(@"stu1:%zi", [stu1 retainCount]);
// 删除多个key对应的value
// [dict removeObjectsForKeys:[NSArray arrayWithObject:@"k1"]];
// 字典被销毁时,内部的所有key和value计数器都会-1,也就是说stu1会release一次
// 将int类型的10 包装成 一个NSNumber对象
NSNumber *number = [NSNumber numberWithInt:10];
NSLog(@"number=%@", number);
// 添加数值到数组中
NSMutableArray *array = [NSMutableArray array];
[array addObject:number];
// 取出来还是一个NSNumber对象,不支持自动解包(也就是不会自动转化为int类型)
NSNumber *number1 = [array lastObject];
// 将NSNumber转化成int类型
int num = [number1 intValue];
NSLog(@"num=%i", num);
将结构体变量包装成一个对象
CGPoint point = CGPointMake(10, 10);
NSValue *value = [NSValue valueWithPoint:point];
NSMutableArray *array = [NSMutableArray array];
[array addObject:value];// 添加value
// 取出当时放进去的value
NSValue *value1 = [array lastObject];
CGPoint point1 = [value1 pointValue];
BOOL result = CGPointEqualToPoint(point1, point);
typedef struct {
int year;
int month;
int day;
} Date;
void value2() {
Date date = {2013, 4, 7};
// 这里要传结构体的地址&date
// 根据结构体类型生成 对应的 类型描述字符串
char *type = @encode(Date);
NSValue *value = [NSValue value:&date withObjCType:type];
Date date1;// 定义一个结构体变量
[value getValue:&date1];// 取出包装好的结构体
// [value objCType]; 取出类型描述字符串
NSLog(@"year=%i, month=%i, day=%i", date1.year, date1.month, date1.day);
//year=2013, month=4, day=7
}
// [NSNull null]返回的是同一个单粒对象
NSNull *n = [NSNull null];
NSNull *n1 = [NSNull null];
NSLog(@"%i", n == n1);//1
// date方法返回的就是当前时间(now)
NSDate *date = [NSDate date];
NSLog(@"11:%@", date);//11:2016-04-06 14:42:19 +0000
// now: 11:12:40
// date: 11:12:50
date = [NSDate dateWithTimeIntervalSinceNow:10];
NSLog(@"22:%@", date);//22:2016-04-06 14:42:29 +0000
// 从1970-1-1 00:00:00开始
date = [NSDate dateWithTimeIntervalSince1970:10];
NSLog(@"33:%@", date);//33:1970-01-01 00:00:10 +0000
// 随机返回一个比较遥远的未来时间
date = [NSDate distantFuture];
NSLog(@"44:%@", date);//44:4001-01-01 00:00:00 +0000
// 随机返回一个比较遥远的过去时间
date = [NSDate distantPast];
NSLog(@"55:%@", date);//55:0000-12-30 00:00:00 +0000
NSDate *date = [NSDate date];
// 返回1970-1-1开始走过的毫秒数
NSTimeInterval interval = [date timeIntervalSince1970];
NSLog(@"interval = %f",interval);//1459954262.860866
NSDate *date2 = [NSDate date];
// 返回比较早的那个时间
NSLog(@"%@",[date earlierDate:date2]);//2016-04-06 14:51:02 +0000
// 返回比较晚的那个时间
NSLog(@"%@",[date laterDate:date2]);//2016-04-06 14:51:02 +0000
// 跟其他时间进行对比
interval =[date2 timeIntervalSinceDate:date];
NSLog(@"interval = %f",interval);//0.001233
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// HH是24进制,hh是12进制
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//修正stringFromDate时区差异
//formatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease];
NSString *string = [formatter stringFromDate:date];
NSLog(@"%@", string);
// 返回的格林治时间
NSDate *date2 = [formatter dateFromString:@"2010-09-09 13:14:56"];
NSLog(@"%@", date2);//2010-09-09 05:14:56 +0000
[formatter release];
if ( [stu isKindOfClass:[Person class]] ) { // NSLog(@"stu属于Person或者继承自Person");}
BOOL result = [stu isMemberOfClass:[Student class]];
[stu test];
[stu performSelector:@selector(test)];
[stu performSelector:@selector(test2:) withObject:@"abc"];
[stu performSelector:@selector(test2:) withObject:@"abc" afterDelay:2];
// 类的反射
NSString *str = @"Person";
Class class = NSClassFromString(str);
Person *person = [[class alloc] init];
NSLog(@"%@", person);//
// Class变成字符串
NSString *name = NSStringFromClass([Person class]);
NSLog(@"%@", name);//Person
// 方法的反射
NSString *method = @"test";
SEL selector = NSSelectorFromString(method);
[person performSelector:selector];//调用了Person的test方法
// 将SEL转换为字符串
NSString *selectorName = NSStringFromSelector(selector);
NSLog(@"%@", selectorName);//test
[person release];
IOS开发之OC基础学习笔记(上)
IOS开发之OC基础学习笔记(中)
IOS开发之OC基础学习笔记(下)