1.UIStoryBoard
2. segue跳转传值
3. UIColor配色
//1. 十进制配色
[UIColor colorWithRed:163.0/255.0 green:148.0/255.0 blue:128.0/255.0 alpha:1.0];
//2. 设置16进制颜色(同上)
[UIColor colorWithRed:0xba/255.0 green:0x21/255.0 blue:0xd5/255.0 alpha:1.0];
4. XML解析
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(nonatomic,copy) NSString *pid;
@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *sex;
@property(nonatomic,copy) NSString *age;
@end
#import <Foundation/Foundation.h>
#import "Person.h"
@interface XMLUtil : NSObject<NSXMLParserDelegate>
@property(nonatomic,strong) NSXMLParser *par;
@property(nonatomic,strong) Person *person;
//存放每个person
@property(nonatomic,strong) NSMutableArray *list;
//标记当前标签,以索引找到XML文件的内容
@property(nonatomic,copy) NSString *currentElement;
//声明parse方法,通过它实现解析
-(void)parse;
@end
@implementation XMLUtil
-(instancetype)init {
self = [super init];
if (self) {
//获取事先准备好的XML文件
NSBundle *b = [NSBundle mainBundle];
NSString *path = [b pathForResource:@"test" ofType:@".xml"];
NSData *data = [NSData dataWithContentsOfFile:path];
self.par = [[NSXMLParser alloc] initWithData:data];
//添加代理
self.par.delegate = self;
//初始化数组,存放解析后的数据
self.list = [NSMutableArray arrayWithCapacity:5];
}
return self;
}
//几个代理方法的实现,是按逻辑上的顺序排列的,但实际调用过程中中间三个可能因为循环等问题乱掉顺序
//(1).开始解析
-(void)parseDiaStartCocument:(NSXMLParser *)parser {
NSLog(@"parserDidStartDocument...");
}
//(2).准备节点
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
self.currentElement = elementName;
if ([self.currentElement isEqualToString:@"student"]) {
self.person = [[Person alloc] init];
}
}
//(3). 获取节点内容
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([self.currentElement isEqualToString:@"pid"]) {
[self.person setPid:string];
}else if ([self.currentElement isEqualToString:@"name"]) {
[self.person setName:string];
}else if ([self.currentElement isEqualToString:@"sex"]) {
[self.person setSex:string];
}else if ([self.currentElement isEqualToString:@"age"]) {
[self.person setAge:string];
}
}
//(4). 解析完一个节点
-(void)parseDidEndDocument:(NSXMLParser *)parser {
NSLog(@"parserDidEndDocument...");
}
//外部调用借口
-(void)parse {
[self.par parse];
}
@end
5. JSON解析
//1. Json数据封包
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"value3",@"key3", nil];
//isValidJSONSerialization判断对象是否可以构建成json对象
if ([NSJSONSerialization isValidJSONObject:dic]) {
NSError *error;
//创建一个json从Data,NSJSONWritingPrettyPrinted指定的JSON数据产的空白,使输出更具可读性
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"json data:%@",json);
}
//2. Json数据解析
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101120101.html"]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS自带解析类NSJSONSerialization从response中解析出数据放到字典中 - crash here - TBD
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
// NSDictionary *text = [NSString stringWithFormat:@"今天是%@ %@ %@ 的天气状况是:%@ %@",
// [weatherInfo objectForKey:@"day_y"],
// [weatherInfo objectForKey:@"week"],
// [weatherInfo objectForKey:@"city"],
// [weatherInfo objectForKey:@"weather1"],
// [weatherInfo objectForKey:@"temp1"]];
NSDictionary *text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是: %@ %@",[weatherInfo objectForKey:@"day_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]
];
NSLog(@"weatherInfo: %@",text);
6. 多线程
1.NSThread - 苹果封装后,并且完全面向对象
// //(1). 先创建线程类,在启动
// //创建
// NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:nil];
//
// //启动
// [thread start];
//
// //(2). 创建并启动
// [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:nil];
//2. GCD - 苹果为多核的并行运算提出的解决方案
//在GCD中,加入了2个非常重要的概念:任务和队列.
//队列:分串行和并行2种
//(1). 主队列:用于刷新UI
//创建
// dispatch_queue_t queue = dispatch_get_main_queue();
//(2).自己创建的队列:用于耗时渐咋
//串行队列
// dispatch_queue_t queue = dispatch_queue_create("tk.bourne.testQueue", NULL);
//并行队列
// dispatch_queue_t queue = dispatch_queue_create("tk.courne.testQueue", DISPATCH_QUEUE_SERIAL);
//(3). 全局并行队列:只要是并行队列任务一般都加入到这个队列,这是系统提供的一个并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_SERIAL, 0);
//任务:分同步和异步两种
//(1).同步任务:不会另开线程,会阻塞当前线程
dispatch_sync(queue, ^{
//code here
});
//(2).异步任务:会另开线程,不会阻塞当前线程
dispatch_sync(queue, ^{
//code here
});
//3. NSOperation和NSOperationQueue
/*
对GCD的封装,完全面向对象,NSOperation和NSOperationQueue分别对应GCD的任务和队列。
操作步骤: 1. 将要执行的任务封装到一个NSOperation对象中.
2. 将此任务添加到一个NSOperationQueue对象中.
注意:NSOperation只是一个抽象类,所以不能封装任务。但它有2个子类用于封装任务。
分别是:NSInvocationOperation 和 NSBlockOperation。创建一个Operation后,
需要调用start方法来启动任务,它会默认在当前队列同步执行。当然可以在中途取消一个任务,
只需要调用起cancel方法即可.
*/
//NSInvocationOperation:需要传入一个方法名
//1.创建NSInvocation对象
// NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];
//
// //2.开始执行
// [operation start];
//1.创建NSBlockOperation对象
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
}];
//可以添加多个block
for (NSInteger i = 0; i < 5; i ++ ) {
[operation addExecutionBlock:^{
NSLog(@"第%ld次: %@",i,[NSThread currentThread]);
}];
}
//2.开始执行
[operation start];
7.动画效果
8. NSUserDefaults
//.简介
/*
NSUserDefaults是一个单例
他可以用于数据的永久保存,而且简单实用,这是它可以让数据自由传递的一个前提,
也是大家喜欢用它保存简单数据的一个主要原因.
*/
//1. NSUserDefaults
// NSUserDefaults支持的数据类型有: NSNumber(NSInteger、float、double),NSString、NSDate、NSArray、NSDictionary、BOOL;
//将NSString对象存储到NSUserDefaults中
// NSString *passWord = @"123456";
// NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
// [user setObject:passWord forKey:@"userPassWord"];
//将数据取出来
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *passWord = [user objectForKey:@"userPassWord"];
//2.NSUserDefaults存储对象
//NSUserDafaults本身不支持自定义对象的存储,不过它支持NSData的类型,所以可以选择使用归档,将自定义对象转为NSData类型后存入NSUserDefaults.
//(1).将Student类的数据归档
//(2).将Student对象存入NSUserDefaults
Student *student = [[Student alloc] int];
student.name = @"ladyO";
student.sex = @"女";
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:student];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:data forKey:@"oneStudent"];
//(3).从NSUserDefaults取出Student对象
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSData *data = [user objectForKey:@"oncStudent"];
Student *student = [NSKeyedArchiver unarchiveObjectWithData:data];
9. 网络请求