iOS学习笔记-05线程

5.线程

5.1 GCD

  • GCD是一套多线程库,可以有效的替换NSThread或者NSOperation。它的基本结构是dispatch_async(queue, block);参数中的queue可以通过dispatch_queue_create或者系统提供的标准dispatch queue。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 生成一个serial dispatch queue
dispatch_queue_t serialQueue = dispatch_queue_create("com.demo.sai", NULL);
// 生成一个concurrent dispatch queue
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.demo.sai", DISPATCH_QUEUE_CONCURRENT);

// 生成的dispatch queue需要手动release,注意ARC不会释放dispatch_queue_t类型的变量
dispatch_release(serialQueue);
dispatch_release(concurrentQueue);

// 使用系统已经提供的方法来create queue
dispatch_queue_t serialQueue2 = dispatch_get_main_queue();
dispatch_queue_t concurrentQueue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

// 因此真正在代码中经常是这样写的
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"execute in main thread");
});

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"execute in a concurrent thread");
});

iOS学习笔记-06数据处理

6.数据处理

6.1 plsit

  • 创建plist文件及添加数值

  • 数据读取,注意因为plist创建的时候可以选择dictionary或者array,这个也决定了后面怎么去读取它。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"dict_data" ofType:@"plist"];
if (!isEmpty(filePath)) {
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"%@", dict[@"name"]);

NSArray* arr = (NSArray*)dict[@"favor"];
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL* _Nonnull stop) {
NSLog(@"%@", obj);
}];
}

NSString *filePath2 = [[NSBundle mainBundle] pathForResource:@"arr_plist" ofType:@"plist"];
NSArray *arr2 = [NSArray arrayWithContentsOfFile:filePath2];
NSLog(@"%@", arr2);

iOS学习笔记-03UI

3.UI

  • iOS的UI开发说起来复杂也不复杂,复杂在于控件很多,属性很多,各种delegate,但是众多的控件也遵循着相同的模式,让人学起来会觉得可以举一反三。同时在UI这个大的标题下还有Draw、layer、transforms、animation、touch这些技术点。想要做出一些非常规UI效果往往不是依靠简单的使用SDK提供的控件可以做到的。

3.1 手动编写UI

  • 个人认知:手动编写UI在iOS不需要考虑手机屏幕尺寸的时代是很好用的,但是随着多种尺寸屏幕的出现,在自适应上手动编写UI代码就有了一些局限性。
  • 手动编写的核心在于创建UI对象,设置frame及各种属性。
  • 当然也可以使用xib来拖动UI,然后让UI与代码关联进而编写相关功能。

iOS学习笔记-07必要知识

7.必要知识

7.1 APNS

  • 目前在8.0以后的推送需要以下几个步骤:

    1.使用registerUserNotificationSettings: & registerForRemoteNotifications方法

    1
    2
    3
    4
    5
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)]) {
    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    }

关于registerForRemoteNotifications方法有以下说明,它会需要实现两个delegate方法,并且方法能够被执行的条件是通过registerUserNotificationSettings:方法成功注册用户的notification,或者enabled for Background App Refresh。

// Calling this will result in either application:didRegisterForRemoteNotificationsWithDeviceToken: or application:didFailToRegisterForRemoteNotificationsWithError: to be called on the application delegate. Note: these callbacks will be made only if the application has successfully registered for user notifications with registerUserNotificationSettings:, or if it is enabled for Background App Refresh

2.实现delegate方法:每个APP都不同,就不写了。

iOS学习笔记-08杂

8.杂

表达式

表达式,是由数字、算符、数字分组符号(括号)、自由变量和约束变量等以能求得数值的有意义排列方法所得的组合。如:算术表达式、逻辑表达式、关系表达式、赋值表达式、逗号表达式等等。

iOS学习笔记-04内存管理

4.内存管理

4.1 手动管理

  • 自己生成的对象自己持有,可以调用release方法减少retain数量。
  • 非自己生成的对象也可以持有,通过调用retain方法可以持有对象,引用数+1。
  • 不再需要自己持有的对象要及时释放,注意类中的property要在- (void)dealloc方法中赋值nil,这样写相当于release了。
1
2
3
4
5
- (void)dealloc
{
self.arr = nil;
[super dealloc];
}

iOS学习笔记-01基本语法

1.基本语法

  • 与其它编程语言一样有基本类型,比如int,bool,double,float等等。为了64位的问题需要使用OC中定义的类型,例如NSInteger
1
2
3
4
5
6
7
#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×