1. Objective-C语法
1.1 基本数据类型
- C语言的基本数据类型(int,flot, double, bool)在OC中也适用
- OC中的数据类型
值类型:NSInterger
, CGFloat
, BOOL
, CGPoint
, CGRect
引用类型:NSNumber
,NSString
、NSSet
、NSArray
、NSMutableArray
、NSDictionary
、NSMutableDictionary
-
id:
id
是指向Objective-C对象的指针,等价于C语言中的void*
,可以映射任何对象指针指向它,或者映射他指向其他的对象。 -
枚举
OC的枚举值只能是整数
typedef NS_ENUM(NSUInteger, AFLoadingStatus) {
AFNormal,
AFLoading,
AFSuccess
};
1.2 方法类、定义
方法的声明
@interface Circle: NSObject
//MARK: variables
@property (nonatomic, assign) NSInteger count;
//MARK: - methods
+ (void) setColor: (UIColor *)color atIndex: (NSInteger)index;
- (UIColor *) getColor;
@end
方法的实现
@implementation Circle
+ (void) setColor: (UIColor *)color atIndex: (NSInteger)index {
//TODO
}
- (UIColor *) getColor {
return UIColor.redColor;
}
@end
2. iOS开发基础
2.1 创建项目
演示...
2.2 cocopods
2.2.1 podfile文件
use_frameworks!
source 'https://github.com/CocoaPods/Specs.git'
target 'OCDemo' do
pod 'AFNetworking'
end
2.3 PrefixHeader.pch
公共的头文件可以放到PrefixHeader.pch中
2.4 页面搭建storyboard
2.5 页面跳转和参数传递
2.5.1 push
跳转到storyboard页面
- (IBAction)buttonTapped:(id)sender {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
AViewController *aViewController = [sb instantiateViewControllerWithIdentifier:@"AViewController"];
aViewController.name = @"abc";
[self.navigationController pushViewController:aViewController animated:YES];
}
跳转到xib页面
BViewController *bController = [[BViewController alloc] initWithNibName:@"BViewController" bundle:nil];
[self.navigationController pushViewController:bController animated:YES];
2.5.2 模态
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
DViewController *dViewController = [sb instantiateViewControllerWithIdentifier:@"DViewController"];
[self presentViewController:dViewController animated:true completion:nil];
2.6 数据存储
- Keychain 存储一些用户账号密码之类敏感信息,只能存储键值对。
- plist文件(属性列表)
- NSKeyedArchiver(归档)
- SQLite 3 (FMDB github.com/ccgus/fmdb)
- CoreData
- 手动存放沙盒,字典和数组都可以直接写入文件。
2.7 多线程
NSThread,GCD,NSOperation
1. NSOperation相对于GCD:
NSOperation拥有更多的函数可用,具体查看api。NSOperationQueue是在GCD基础上实现的,只不过是GCD更高一层的抽象。
在NSOperationQueue中,可以建立各个NSOperation之间的依赖关系。
NSOperationQueue支持KVO。可以监测operation是否正在执行(isExecuted)、是否结束(isFinished),是否取消(isCanceld)
GCD只支持FIFO的队列(即先进先出的队列),而NSOperationQueue可以调整队列的执行顺序(通过调整权重)。NSOperationQueue可以方便的管理并发、NSOperation之间的优先级。
2. 使用NSOperation的情况:
各个操作之间有依赖关系、操作需要取消暂停、并发管理、控制操作之间优先级,限制同时能执行的线程数量.让线程在某时刻停止/继续等。
3. 使用GCD的情况:
一般需求很简单的多线程操作,用GCD都可以了,简单高效。
tbfungeek.github.io/2019/08/12/…
GCD简单示例:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//TODO: 耗时操作
dispatch_async(dispatch_get_main_queue(), ^{
//主线程更新UI
});
});
2.8 网络请求
AFNetworking