标签:
1、文字的宽高
/*
text:文字
font:文字的字体
maxsize:文字的最大宽高
*/
- (CGSize)sizeWithText:(NSString *)text andFont:(NSFont *)font andMaxSize:(CGSize)maxsize{
NSDictionary * dict = @{NSFontAttributeName : font};
return [text boundingRectWithSize:maxsize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
}
2、多线程的简单使用
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:nil)
object:nil];
[queue addOperation:operation];
/***********************************GCD**************************************************/
//一次性执行
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//code be executed once
});
//延迟调用
double delayInSeconds = 2.0;
dispatch_time_t timer = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds *NSEC_PER_SEC);
dispatch_after(timer, dispatch_get_main_queue(), ^{
//code be executed once after delay
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//code be executed once after delay
});
//自定义queque
dispatch_queue_t fun = dispatch_queue_create("funfun", NULL);
dispatch_async(fun, ^{
// your code
});
MRC 需要释放
//dispatch_release(fun);
//GCD组 同时进行多个任务,任务完成之后再主线程执行其他任务
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//your code
});
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//your code
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//your code
});
//多线程的线程安全
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// 线程锁
NSLock *lock = [[NSLock alloc] init];
[lock lock];
//your code
[lock unlock];
});
dispatch_async(queue, ^{
NSLock *lock = [[NSLock alloc] init];
[lock lock];
//your code
[lock unlock];
});
3、简单的倒计时效果
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerMethod:) userInfo:nil repeats:YES];
}
- (void)timerMethod:(NSTimer *)timer
{
//创建一个NSCalendar对象
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *Boom = [[NSDateComponents alloc] init];
Boom.year = 2015;
Boom.month = 7;
Boom.day = 8;
Boom.hour = 13;
Boom.minute = 59;
Boom.second = 30;
//把目标时间装载入date
NSDate *date = [calendar dateFromComponents:Boom];
//得到当前时间
NSDate *currentDate = [NSDate date];
//用来得到具体的时差
unsigned int flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *rub = [cal components:flags fromDate:currentDate toDate:date options:0];
_Boom.text = [NSString stringWithFormat:@"??????%ld:%ld:%ld", d.hour, d.minute, d.second];
}
4、基本的网络请求
一、同步GET
// 1.将网址初始化成一个OC字符串对象
NSString *urlStr = [NSString stringWithFormat:@
"%@?wewe=%@", @"111", @"222"];
// 2.如果网址中存在中文,进行URLEncode
NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 3.构建网络URL对象, NSURL
NSURL *url = [NSURL URLWithString:newUrlStr];
// 4.创建网络请求
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
// 5.创建同步链接
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// 1.将网址初始化成一个OC字符串对象
NSString *urlStr = [NSString stringWithFormat:@
"%@?wewe=%@", @"111", @"222"];
// 2.如果网址中存在中文,进行URLEncode
NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 3.构建网络URL对象, NSURL
NSURL *url = [NSURL URLWithString:newUrlStr];
// 4.创建网络请求
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
[NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//your code
}]
// 异步连接(形式2)
// 服务器接收到请求时
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}
// 当收到服务器返回的数据时触发, 返回的可能是资源片段,需要拼接资源片段
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}
// 当服务器返回所有数据时触发, 数据返回完毕
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}
// 请求数据失败时触发
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@
"%@"
, error);
}
// 1.将网址初始化成一个OC字符串对象
NSString *urlStr = [NSString stringWithFormat:@
"%@?wewe=%@", @"111", @"222"];
// 2.如果网址中存在中文,进行URLEncode
NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 3.构建网络URL对象, NSURL
NSURL *url = [NSURL URLWithString:newUrlStr];
// 4.创建请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 5.创建参数字符串对象
NSString *parmStr = @
"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10"
;
// 6.将字符串转为NSData对象
NSData *pramData = [parmStr dataUsingEncoding:NSUTF8StringEncoding];
// 7.设置请求体
// 8.设置请求方式
[request setHTTPMethod:@
"POST"
];
// 9.创建同步链接
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// 1.将网址初始化成一个OC字符串对象
NSString *urlStr = [NSString stringWithFormat:@
"%@?wewe=%@", @"111", @"222"];
// 2.如果网址中存在中文,进行URLEncode
NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 3.构建网络URL对象, NSURL
NSURL *url = [NSURL URLWithString:newUrlStr];
// 4.创建请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 5.创建参数字符串对象
NSString *parmStr = @
"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10"
;
// 6.将字符串转为NSData对象
NSData *pramData = [parmStr dataUsingEncoding:NSUTF8StringEncoding];
// 7.设置请求体
[request setHTTPBody:pramData];
// 8.设置请求方式
[request setHTTPMethod:@
"POST"
];
// 9.创建异步连接(形式2)
[NSURLConnection connectionWithRequest:request delegate:self];
GET请求和POST请求的区别:
1. GET请求的接口会包含参数部分,参数会作为网址的一部分,服务器地址与参数之间通过 ? 来间隔。POST请求会将服务器地址与参数分开,请求接口中只有服务器地址,而参数会作为请求的一部分,提交后台服务器。
2. GET请求参数会出现在接口中,不安全。而POST请求相对安全。
3.虽然GET请求和POST请求都可以用来请求和提交数据,但是一般的GET多用于从后台请求数据,POST多用于向后台提交数据。
同步和异步的区别:
同步链接:主线程去请求数据,当数据请求完毕之前,其他线程一律不响应,会造成程序就假死现象。
异步链接:会单独开一个线程去处理网络请求,主线程依然处于可交互状态,程序运行流畅。
标签:
原文地址:http://www.cnblogs.com/liyanlan/p/4650466.html