一、开启子线程、回到主线程:
// 异步子线程 *** 作 dispatch_queue_t globalQueue = dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAulT,0); dispatch_async(globalQueue,^{ NSLog(@"子线程"); /* * add code on child thread */ // 返回主线程 dispatch_async(dispatch_get_main_queue(),^(voID){ NSLog(@"主线程"); /* * add code on main thread */ }); });
二、label等控件在设置动态改变的字体时。如:下载从0.00%变化到100.00%时,因为有些字体会动态改变字体间的行间距,所有看起来很不好看。需要将字体换为:Helvetica Neue字体,不会改变字体间的间距。
三、原生网络请求
GET:
/// 识别引擎判断- (voID)recongnizeType:(voID (^)(Nsstring * _Nullable,NSError * _Nullable))completion { // 请求地址 Nsstring *urlString = @"http://XXXXX?action=jjjj"; // 创建对象 NSURLSession *sessions = [NSURLSession sharedSession]; // 创建请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; // 设置超时 // request.timeoutInterval = 60; // 请求方式post request.httpMethod = @"GET"; NSURLSessionDataTask *datatask = [sessions dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data,NSURLResponse * _Nullable response,NSError * _Nullable error) { if (!error) { Nsstring *string = [[Nsstring alloc]initWithData:data enCoding:NSUTF8StringEnCoding]; if (string.length != 0) { completion (string,nil); } }else { completion (nil,error); } }]; [datatask resume]; }
POST:
- (voID)POST_Param:(NSData *)voiceData completion:(voID (^)(Nsstring * _Nullable,NSError * _Nullable))completion { // 请求地址 Nsstring *urlString = [Nsstring stringWithFormat:@"%@appkey=%@&userID=%@&ID=%@",kUNISOUND_API,kUNISOUND_APPKEY,kUNISOUND_USERID,kUNISOUND_ID]; // 创建对象 NSURLSession *sessions = [NSURLSession sharedSession]; // 创建请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; // 设置超时 request.timeoutInterval = 30; // 请求方式post request.httpMethod = @"POST"; // 当前音频长度 Nsstring *dataLength = [Nsstring stringWithFormat:@"%lu",(unsigned long)voiceData.length]; // 请求头 [request setValue:@"audio/x-wav;codec=pcm;bit=16;rate=16000" forhttpheaderFIEld:@"Content-Type"]; [request setValue:@"text/plain" forhttpheaderFIEld:@"Accept"]; [request setValue:@"zh-CN" forhttpheaderFIEld:@"Accept-Language"]; [request setValue:@"general" forhttpheaderFIEld:@"Accept-topic"];// [request setValue:@"chunked" forhttpheaderFIEld:@"transfer-encoding"]; [request setValue:dataLength forhttpheaderFIEld:@"Content-Length"]; // 请求体 request.httpBody = voiceData; _datatask = [sessions dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data,NSError * _Nullable error) { if (!error) { Nsstring *string = [[Nsstring alloc]initWithData:data enCoding:NSUTF8StringEnCoding];// if (string.length != 0) { completion (string,nil);// } }else { completion (nil,error); } }]; [_datatask resume];}总结
以上是内存溢出为你收集整理的iOS开发日常遇到的小技巧归纳全部内容,希望文章能够帮你解决iOS开发日常遇到的小技巧归纳所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)