iOS学习笔记(8)——GCD初探
2024-10-21 07:27:37
1. AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[ViewController alloc] initWithNibName:@"SlowWorderView" bundle:nil];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
} - (void)applicationWillResignActive:(UIApplication *)application {
// 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.
// 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.
} - (void)applicationDidEnterBackground:(UIApplication *)application {
// 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.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
} - (void)applicationWillEnterForeground:(UIApplication *)application {
// 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.
} - (void)applicationDidBecomeActive:(UIApplication *)application {
// 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.
} - (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
} @end
2. ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIButton *startButton;
@property (weak, nonatomic) IBOutlet UITextView *resultsTextView; @end
3. ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (NSString *)fetchSomethingFromServer {
[NSThread sleepForTimeInterval:];
NSURL *url = [NSURL URLWithString:@"http://nycode.sinaapp.com/d.php"];
NSError *error = nil; NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *arr = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
return [arr objectForKey:@"name"];
} - (NSString *)processData:(NSString *)data {
[NSThread sleepForTimeInterval:];
return [data uppercaseString];
} - (NSString *)calculateFirstResult:(NSString *)data {
[NSThread sleepForTimeInterval:];
return [NSString stringWithFormat:@"Number of chars %lu", [data length]];
} - (NSString *)calculateSecondResult:(NSString *)data {
[NSThread sleepForTimeInterval:];
return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"];
} - (IBAction)doWork:(id)sender {
NSDate *startTime = [NSDate date];
NSString *fetcheData = [self fetchSomethingFromServer];
NSString *processdData = [self processData:fetcheData];
NSString *firstResult = [self calculateFirstResult:processdData];
NSString *secondResult = [self calculateSecondResult:processdData];
NSString *resultsSummary = [NSString stringWithFormat:@"First:[%@]\tSecond:[%@]", firstResult, secondResult];
self.resultsTextView.text = resultsSummary;
NSDate *endTime = [NSDate date];
NSLog(@"Complete in %f seconds", [endTime timeIntervalSinceDate:startTime]);
} @end
4. xib文件放一个textview和button并关联输出口和方法
5. 小结
应用程序启动后,程序会运行10秒。
控制台会显示运行的时间间隔
最新文章
- [译]Writing Custom Middleware in ASP.NET Core 1.0
- Contiki学习笔记 第一个程序:Hello World
- linux文件编码
- Android JNI之调用JAVA方法的返回类型签名
- [C语言(VC)] 打造自己的键盘记录器 (zaroty)
- 2014-07-31 ASP.NET的母版页使用
- Js 自定义回调函数
- 调整 CComboBox 控件的下拉列表的高度
- Qt打开外部程序和文件夹需要注意的细节(Qt调用VC写的动态库,VC需要用C的方式输出函数,否则MinGW32编译过程会报错)
- 用python实现模拟登录人人网
- The Problems of Mysql Installation
- hdu 5016 点分治(2014 ACM/ICPC Asia Regional Xi&#39;an Online)
- SpringCloud学习之SpringCloudBus
- ASP.Net Core开发(踩坑)指南
- Selenium3+python几种定位元素的方法
- struts2 contextMap
- 利用nodejs安装并运行express的三个坑
- request.GetResponse()超时的解决办法
- MVC实现加载更多
- Vue学习笔记:Slot