iOS学习笔记(8)——GCD初探

1. AppDelegate.m

 1 #import "AppDelegate.h"
 2 #import "ViewController.h"
 3 @interface AppDelegate ()
 4
 5 @end
 6
 7 @implementation AppDelegate
 8
 9
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11     // Override point for customization after application launch.
12     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
13     self.window.rootViewController = [[ViewController alloc] initWithNibName:@"SlowWorderView" bundle:nil];
14     self.window.backgroundColor = [UIColor whiteColor];
15     [self.window makeKeyAndVisible];
16     return YES;
17 }
18
19 - (void)applicationWillResignActive:(UIApplication *)application {
20     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
21     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
22 }
23
24 - (void)applicationDidEnterBackground:(UIApplication *)application {
25     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
26     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
27 }
28
29 - (void)applicationWillEnterForeground:(UIApplication *)application {
30     // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
31 }
32
33 - (void)applicationDidBecomeActive:(UIApplication *)application {
34     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
35 }
36
37 - (void)applicationWillTerminate:(UIApplication *)application {
38     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
39 }
40
41 @end

2. ViewController.h

1 #import <UIKit/UIKit.h>
2
3 @interface ViewController : UIViewController
4
5 @property (weak, nonatomic) IBOutlet UIButton *startButton;
6 @property (weak, nonatomic) IBOutlet UITextView *resultsTextView;
7
8 @end

3. ViewController.m

 1 #import "ViewController.h"
 2
 3 @interface ViewController ()
 4
 5 @end
 6
 7 @implementation ViewController
 8
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     // Do any additional setup after loading the view, typically from a nib.
12 }
13
14 - (void)didReceiveMemoryWarning {
15     [super didReceiveMemoryWarning];
16     // Dispose of any resources that can be recreated.
17 }
18
19 - (NSString *)fetchSomethingFromServer {
20     [NSThread sleepForTimeInterval:1];
21     NSURL *url = [NSURL URLWithString:@"http://nycode.sinaapp.com/d.php"];
22     NSError *error = nil;
23
24     NSURLRequest *request = [NSURLRequest requestWithURL:url];
25     NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
26     NSDictionary *arr = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
27     return [arr objectForKey:@"name"];
28 }
29
30 - (NSString *)processData:(NSString *)data {
31     [NSThread sleepForTimeInterval:2];
32     return [data uppercaseString];
33 }
34
35 - (NSString *)calculateFirstResult:(NSString *)data {
36     [NSThread sleepForTimeInterval:3];
37     return [NSString stringWithFormat:@"Number of chars %lu", [data length]];
38 }
39
40 - (NSString *)calculateSecondResult:(NSString *)data {
41     [NSThread sleepForTimeInterval:4];
42     return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"];
43 }
44
45 - (IBAction)doWork:(id)sender {
46     NSDate *startTime = [NSDate date];
47     NSString *fetcheData = [self fetchSomethingFromServer];
48     NSString *processdData = [self processData:fetcheData];
49     NSString *firstResult = [self calculateFirstResult:processdData];
50     NSString *secondResult = [self calculateSecondResult:processdData];
51     NSString *resultsSummary = [NSString stringWithFormat:@"First:[%@]\tSecond:[%@]", firstResult, secondResult];
52     self.resultsTextView.text = resultsSummary;
53     NSDate *endTime = [NSDate date];
54     NSLog(@"Complete in %f seconds", [endTime timeIntervalSinceDate:startTime]);
55 }
56
57 @end

4. xib文件放一个textview和button并关联输出口和方法

5. 小结

应用程序启动后,程序会运行10秒。

控制台会显示运行的时间间隔

时间: 2024-10-31 11:11:21

iOS学习笔记(8)——GCD初探的相关文章

iOS学习笔记14-sqlite数据库初探

1,创建数据库:sqlite3 数据库名.db 2,显示数据库: .database 3,创建表:create table 表名(字段名1 字段类型,字段名2 字段类型,...): 4,插入数据:insert into 表名(字段1,字段2..) values(字段1的值,字段2的值...): 5,查找: 1)select *from表名 --显示所有表名的内容 2)select*from表名 where查询条件 3)select 字段名1,字段名3 from 表名 6,更新 update 表名

iOS学习笔记-精华整理

iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始等待用户的操作,自动释放池就会被释放掉(调用dealloc),池中的对象都会收到一个release,有可能会因此被销毁. 2-成员属性:     readonly:不指定readonly,默认合成getter和setter方法.外界毫不关心的成员,则不要设置任何属性,这样封装能增加代码的独立性和安全

IOS学习笔记 -- 多线程

多线程1.多线程的原理 1>.同一时间,CPU只能处理1条线程,只有1条线程在工作(执行) 2>.多线程并发(同时)执行,其实是CPU快速地在多条线程之间调度(切换) 3>.如果CPU调度线程的时间足够快,就造成了多线程并发执行的假象如果线程非常非常多,会发生: 1>.CPU会在N多线程之间调度,CPU会累死,消耗大量的CPU资源 2>.每条线程被调度执行的频次会降低(线程的执行效率降低) 2.多线程的优缺点 1>.多线程的优点 能适当提高程序的执行效率 能适当提高资源

iOS学习笔记---c语言第十一天

函数指针 一.函数指针定义 //函数声明:声明我是一个什么函数 //求两个数的和 //函数的类型:int (int x,int y) //即:我是一个返回值为整型,有两个整型参数的函数. //函数名是 sum int sum(int x,int y); 函数指针定义p是变量,其他是类型(通常没有形参a,b) //函数指针类型 int (*)(int x,int y) //描述:指向 返回值为 int 两个int参数 的 指针类型 //函数指针变量: p //初始值 : sum printf("%

iOS学习笔记之UITableViewController&amp;UITableView

iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论文,因此需要继续思考新的算法.这是一件挺痛苦的事情,特别是在很难找到与自己研究方向相关的文献的时候.也许网格序列水印这个课题本身的研究意义就是有待考证的.尽管如此,还是要努力的思考下去.由于实验室的原因,iOS的学习进度明显受到影响,加之整理文档本身是一件耗费时间和精力的事情,因此才这么久没有写笔记了. M

iOS: 学习笔记, Swift操作符定义

Swift操作符可以自行定义, 只需要加上简单的标志符即可. @infix 中置运算. 如+,-,*,/运算 @prefix 前置运算. 如- @postfix 后置运算. a++, a-- @assignment 赋值运算. +=, -=, --a, ++a // // main.swift // SwiftBasic // // Created by yao_yu on 14-7-27. // Copyright (c) 2014年 yao_yu. All rights reserved.

IOS学习笔记 -- Modal和Quartz2D

一. Modal1.Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止;Modal只是改变了View的现实,没有改变rootViewController 2.常用方法1>.以Modal的形式展示控制器- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion2>.关

iOS: 学习笔记, 添加一个带界面约束的控制器

1. 创建一个空iOS应用程序(Empty Application). 2. 添加加控制器类. 修改控制器类的viewDidLoad 1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 //创建标题 5 UILabel *header = [[UILabel alloc] init]; 6 header.text = @"欢迎来到我的世界!"; 7 header.textAlignment = NSTextAlignmentCenter

iOS学习笔记(2)— UIView用户事件响应

iOS学习笔记(2)— UIView用户事件响应 UIView除了负责展示内容给用户外还负责响应用户事件.本章主要介绍UIView用户交互相关的属性和方法. 1.交互相关的属性 userInteractionEnabled 默认是YES ,如果设置为NO则不响应用户事件,并且把当前控件从事件队列中删除.也就是说设置了userInterfaceEnabled属性的视图会打断响应者链导致该view的subview都无法响应事件. multipleTouchEnabled  默认是NO,如果设置为YE

iOS学习笔记(3)— 屏幕旋转

iOS学习笔记(3)— 屏幕旋转 一.屏幕旋转机制: iOS通过加速计判断当前的设备方向和屏幕旋转.当加速计检测到方向变化的时候,屏幕旋转的流程如下: 1.设备旋转时,系统接收到旋转事件. 2.系统将旋转事件通过AppDelegate通知当前的主Window. 3.window通知它的rootViewController. 4.rootViewController判断所支持的旋转方向,完成旋转. iOS系统中屏幕旋转事件没有像触碰事件那样进行hitTest,所以只有rootViewControl