Objective-C 学习笔记
2016-05-18 12:43
513 查看
1. Hello, World
#import <Foundation/Foundation.h>
int main()
{
/* my first program in Objective-C */
NSLog(@"Hello, World! \n");
return 0;
}
2. Block(本质是匿名函数,类似C的函数指针, JS closure, C++11的Lambda functions)
(1) example:
__block int sum = 0;
int (^myblocks3) (int a, int b) = ^(int a, int b) {
sum = a+b;
return 3;
};
myblocks3(20, 30);
NSLog(@"sum is %d", sum);
(2) example:
#import <Foundation/Foundation.h>
// 定义Blocks的typedef
typedef int (^SumBlockT) (int a, int b);
int main (int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
void (^myblocks) (void) = NULL;
myblocks = ^(void) {
NSLog(@"in blocks");
}; // 给myblocks 赋值
NSLog(@"before myblocks");
myblocks(); // 执行myblocks;
NSLog(@"after myblocks");
/*
before myblocks
in blocks
after myblocks
*/
int (^myblocks2) (int a, int b) = ^(int a, int b) {
int c = a+b;
return c;
};
NSLog(@"before blocks2");
int ret = myblocks2(10, 20);
NSLog(@"after blocks2 ret %d", ret);
__block int sum = 0;
int (^myblocks3) (int a, int b) = ^(int a, int b) {
sum = a+b;
return 3;
};
myblocks3(20, 30);
NSLog(@"sum is %d", sum);
SumBlockT myblocks4 = ^(int a, int b) {
NSLog(@"c is %d", a+b);
return 0;
};
myblocks4(50, 20);
}
return 0;
}
3.语法糖
4. 新关键字
5. 类和对象
(1)interface内部定义的是instance variables 是私有的,只能在类的implementation中访问。
(2)property定义了属性,编译器会为属性生成getter, setter方法,格式为: @property (参数1,参数2) 类型 名字;
-读写属性: (readwrite / readonly)
-setter语意:(assign / retain / copy)assign用于简单数据类型。 retain和copy用于对象。
分别对应弱引用,强引用,拷贝,类似Boost中的weak_ptr, shared_ptr
可用强引用、弱引用搭配来解除循环引用而导致的内存泄露。
-原子性: (atomicity / nonatomic)分别对应有同步锁无同步锁
(3)-修饰实例方法(非静态方法), + 修饰类方法(静态方法)
6.在OC中使用消息发送机制:[receiver message]
如果方法带有参数,那么就写作 [receiver param:value]
方法和名称和参数的名称是一体的,参数决定了方法是什么。
如果有多个参数,那么写作:[receiver param1:value1 param2:value2]
如果类的两个方法参数的类型或个数不同,那它们是两个不同的方法。OC 不支持默认参数的语法。
7. NSAutoreleasePool 与 autoreleasepool,语法:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Code benefitting from a local autorelease pool.
[pool release];
you would write:
@autoreleasepool {
// Code benefitting from a local autorelease pool.
}
NSAutoreleasePool被清空或是销毁时向池里所有的autorelease的对象发送一条release消息,从而实现对象的自动释放。
ARC中不能用NSAutoreleasePool,必须用autoreleasepool,目的是兼容MRC。
纯ARC编译的项目,无需要任何pool,编辑器会自动在合适的地方添加release.
pool release 和 pool drain (ios上二者完全相同, ios是reference-counted环境, GC在OS X 10.6后已经废弃)
在一个garbage collected环境里,release不做任何操作。 NSAutoreleasePool因此提供了一个 drain 方法,
它在reference-counted环境中的行为和调用release一样, 但是在一个garbage collected环境中则触发garbage collection动作。
8.(ARC)auto reference count(自动引用计数)
当ARC开启时,开发者不用写retain, release和autorelease,编译器将自动在代码合适的地方插入。
不用手动管理对象的引用计数
(深入)编译的时候插入引用计数的管理代码
(深入)不是GC考题:Block作为属性在ARC下应该使用的语义设置为?(D)
A. retain
B. weak
C. strong
D. copy
解释:
开发者使用 block 的时候苹果官方文档中说明推荐使用 copy,使用 copy 的原因就在于大家所熟知的把block从栈管理过渡到堆管理
在 ARC 下面苹果果帮助我们完成了 copy 的工作,在 ARC 下面即使使用的修饰符是 Strong,实际上效果和使用 copy 是一样的这一点在苹果的官方文档也有说明
9. Protocol (类似C++中的纯虚类)
定义与实现
10. iOS异步操作(多线程)一般都有哪些方式
gcd(首选)
NSOperation和NSOperation(可以设置并发数和依赖关系)
NSThread
posix thread(即pthread)
11. GCD(Grand Central Dispatch)
GCD存在于libdispatch.dylib这个库中,有三种,分别为:
串行(SerialDispatchQueue),并行(ConcurrentDispatchQueue)和主调度队列(MainDispatchQueue)。
(0) 定义两个block
UInt32 loopCount = 1000;
void (^taskFirst)(void) = ^{
NSLog(@"taskFirst 任务开始执行\r\n");
for (UInt32 i = 0; i < loopCount; i++) {
}
NSLog(@"taskFirst 任务结束\r\n");
};
void (^taskSecond)(void) = ^{
NSLog(@"taskSecond任务开始执行\r\n");
for (UInt32 i = 0; i < loopCount; i ++) {
}
NSLog(@"taskSecond 任务结束\r\n");
};
(1). 串行队列( 同步 和异步)
//同步(一个队列,单线程)
dispatch_queue_t serialQueue;
serialQueue = dispatch_queue_create("serialDemo", NULL);
dispatch_async(serialQueue, taskFirst);
NSLog(@"taskfirst 已经加入队列\r\n");
dispatch_async(serialQueue, taskSecond);
NSLog(@"tasksecond 已经加入队列\r\n");
//异步(多个队例,多线程)
dispatch_queue_t serialQueue;
serialQueue = dispatch_queue_create("serialDemo", NULL);
//创建第二个队列
dispatch_queue_t serialQueueSecond = dispatch_queue_create("serialSecondDemo", NULL);
dispatch_async(serialQueue, taskFirst);
NSLog(@"taskfirst 已经加入队列\r\n");
dispatch_async(serialQueueSecond, taskSecond);
NSLog(@"tasksecond 已经加入队列\r\n");
(2).并行队列
//效果与串行异步是一样的,不过分配多少个线程是由系统决定的
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, taskFirst);
NSLog(@"taskfirst 已经加入队列\r\n");
dispatch_async(concurrentQueue, taskSecond);
NSLog(@"tasksecond 已经加入队列\r\n");
(3).主队列(主调度队列中的任务运行在应用程序主线程上,所以,如果你要修改应用程序的界面,他是唯一的选择。)
dispatch_async(dispatch_get_main_queue(), ^{
.....//跟新界面的操作
});
12. 数据持久化
plist文件(属性列表)
preference(偏好设置)
NSKeyedArchiver(归档)
SQLite 3
CoreData
详见:http://www.cocoachina.com/ios/20150720/12610.html
#import <Foundation/Foundation.h>
int main()
{
/* my first program in Objective-C */
NSLog(@"Hello, World! \n");
return 0;
}
2. Block(本质是匿名函数,类似C的函数指针, JS closure, C++11的Lambda functions)
(1) example:
__block int sum = 0;
int (^myblocks3) (int a, int b) = ^(int a, int b) {
sum = a+b;
return 3;
};
myblocks3(20, 30);
NSLog(@"sum is %d", sum);
(2) example:
#import <Foundation/Foundation.h>
// 定义Blocks的typedef
typedef int (^SumBlockT) (int a, int b);
int main (int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
void (^myblocks) (void) = NULL;
myblocks = ^(void) {
NSLog(@"in blocks");
}; // 给myblocks 赋值
NSLog(@"before myblocks");
myblocks(); // 执行myblocks;
NSLog(@"after myblocks");
/*
before myblocks
in blocks
after myblocks
*/
int (^myblocks2) (int a, int b) = ^(int a, int b) {
int c = a+b;
return c;
};
NSLog(@"before blocks2");
int ret = myblocks2(10, 20);
NSLog(@"after blocks2 ret %d", ret);
__block int sum = 0;
int (^myblocks3) (int a, int b) = ^(int a, int b) {
sum = a+b;
return 3;
};
myblocks3(20, 30);
NSLog(@"sum is %d", sum);
SumBlockT myblocks4 = ^(int a, int b) {
NSLog(@"c is %d", a+b);
return 0;
};
myblocks4(50, 20);
}
return 0;
}
3.语法糖
4. 新关键字
_Packed | protocol | interface | implementation |
NSObject | NSInteger | NSNumber | CGFloat |
property | nonatomic; | retain | strong |
weak | unsafe_unretained; | readwrite | readonly |
(1)interface内部定义的是instance variables 是私有的,只能在类的implementation中访问。
(2)property定义了属性,编译器会为属性生成getter, setter方法,格式为: @property (参数1,参数2) 类型 名字;
-读写属性: (readwrite / readonly)
-setter语意:(assign / retain / copy)assign用于简单数据类型。 retain和copy用于对象。
分别对应弱引用,强引用,拷贝,类似Boost中的weak_ptr, shared_ptr
可用强引用、弱引用搭配来解除循环引用而导致的内存泄露。
-原子性: (atomicity / nonatomic)分别对应有同步锁无同步锁
(3)-修饰实例方法(非静态方法), + 修饰类方法(静态方法)
#import <Foundation/Foundation.h> @interface Box:NSObject { double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box } @property(nonatomic, readwrite) double height; // Property -(double) volume; @end @implementation Box @synthesize height; -(id)init { self = [super init]; length = 1.0; breadth = 1.0; return self; } -(double) volume { return length*breadth*height; } @end int main( ) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Box *box1 = [[Box alloc]init]; // Create box1 object of type Box Box *box2 = [[Box alloc]init]; // Create box2 object of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification box1.height = 5.0; // box 2 specification box2.height = 10.0; // volume of box 1 volume = [box1 volume]; NSLog(@"Volume of Box1 : %f", volume); // volume of box 2 volume = [box2 volume]; NSLog(@"Volume of Box2 : %f", volume); [pool drain]; return 0; }
6.在OC中使用消息发送机制:[receiver message]
如果方法带有参数,那么就写作 [receiver param:value]
方法和名称和参数的名称是一体的,参数决定了方法是什么。
如果有多个参数,那么写作:[receiver param1:value1 param2:value2]
如果类的两个方法参数的类型或个数不同,那它们是两个不同的方法。OC 不支持默认参数的语法。
7. NSAutoreleasePool 与 autoreleasepool,语法:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Code benefitting from a local autorelease pool.
[pool release];
you would write:
@autoreleasepool {
// Code benefitting from a local autorelease pool.
}
NSAutoreleasePool被清空或是销毁时向池里所有的autorelease的对象发送一条release消息,从而实现对象的自动释放。
ARC中不能用NSAutoreleasePool,必须用autoreleasepool,目的是兼容MRC。
纯ARC编译的项目,无需要任何pool,编辑器会自动在合适的地方添加release.
pool release 和 pool drain (ios上二者完全相同, ios是reference-counted环境, GC在OS X 10.6后已经废弃)
在一个garbage collected环境里,release不做任何操作。 NSAutoreleasePool因此提供了一个 drain 方法,
它在reference-counted环境中的行为和调用release一样, 但是在一个garbage collected环境中则触发garbage collection动作。
8.(ARC)auto reference count(自动引用计数)
当ARC开启时,开发者不用写retain, release和autorelease,编译器将自动在代码合适的地方插入。
不用手动管理对象的引用计数
(深入)编译的时候插入引用计数的管理代码
(深入)不是GC考题:Block作为属性在ARC下应该使用的语义设置为?(D)
A. retain
B. weak
C. strong
D. copy
解释:
开发者使用 block 的时候苹果官方文档中说明推荐使用 copy,使用 copy 的原因就在于大家所熟知的把block从栈管理过渡到堆管理
在 ARC 下面苹果果帮助我们完成了 copy 的工作,在 ARC 下面即使使用的修饰符是 Strong,实际上效果和使用 copy 是一样的这一点在苹果的官方文档也有说明
9. Protocol (类似C++中的纯虚类)
定义与实现
@protocol ProtocolName @required // list of required methods @optional // list of optional methods @end
@interface MyClass : NSObject <MyProtocol> ... @end
10. iOS异步操作(多线程)一般都有哪些方式
gcd(首选)
NSOperation和NSOperation(可以设置并发数和依赖关系)
NSThread
posix thread(即pthread)
11. GCD(Grand Central Dispatch)
GCD存在于libdispatch.dylib这个库中,有三种,分别为:
串行(SerialDispatchQueue),并行(ConcurrentDispatchQueue)和主调度队列(MainDispatchQueue)。
(0) 定义两个block
UInt32 loopCount = 1000;
void (^taskFirst)(void) = ^{
NSLog(@"taskFirst 任务开始执行\r\n");
for (UInt32 i = 0; i < loopCount; i++) {
}
NSLog(@"taskFirst 任务结束\r\n");
};
void (^taskSecond)(void) = ^{
NSLog(@"taskSecond任务开始执行\r\n");
for (UInt32 i = 0; i < loopCount; i ++) {
}
NSLog(@"taskSecond 任务结束\r\n");
};
(1). 串行队列( 同步 和异步)
//同步(一个队列,单线程)
dispatch_queue_t serialQueue;
serialQueue = dispatch_queue_create("serialDemo", NULL);
dispatch_async(serialQueue, taskFirst);
NSLog(@"taskfirst 已经加入队列\r\n");
dispatch_async(serialQueue, taskSecond);
NSLog(@"tasksecond 已经加入队列\r\n");
//异步(多个队例,多线程)
dispatch_queue_t serialQueue;
serialQueue = dispatch_queue_create("serialDemo", NULL);
//创建第二个队列
dispatch_queue_t serialQueueSecond = dispatch_queue_create("serialSecondDemo", NULL);
dispatch_async(serialQueue, taskFirst);
NSLog(@"taskfirst 已经加入队列\r\n");
dispatch_async(serialQueueSecond, taskSecond);
NSLog(@"tasksecond 已经加入队列\r\n");
(2).并行队列
//效果与串行异步是一样的,不过分配多少个线程是由系统决定的
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, taskFirst);
NSLog(@"taskfirst 已经加入队列\r\n");
dispatch_async(concurrentQueue, taskSecond);
NSLog(@"tasksecond 已经加入队列\r\n");
(3).主队列(主调度队列中的任务运行在应用程序主线程上,所以,如果你要修改应用程序的界面,他是唯一的选择。)
dispatch_async(dispatch_get_main_queue(), ^{
.....//跟新界面的操作
});
12. 数据持久化
plist文件(属性列表)
preference(偏好设置)
NSKeyedArchiver(归档)
SQLite 3
CoreData
详见:http://www.cocoachina.com/ios/20150720/12610.html
相关文章推荐
- 跟王老师学泛型(五): 自定义带泛型声明的方法
- IOS 开发 OC(Object_C)与前端页面JS(JavaScript)交互整理(一)
- 跟王老师学泛型(一)使用泛型
- Scriptable Object
- Cloneable接口和Object的clone()方法
- [IOS/翻译]GCD-4 Managing Dispatch Objects
- objective-c内存管理的实现机制,并简述什么时候由你负责释放对象,什么时候不由你释放?
- 在Objective-C声明Block的几种方式
- Java NIO框架Netty教程(三) – Object对象传递
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Object-C之继承
- Xcode7 安装VVDocumenter-Xcode步骤
- [PWA] 13. New db and object store
- Objective-C ---NSArray(梳理整理)
- effective objective-c 2.0 笔记 第五章 :内存管理
- 读书笔记《iPhone应用开发从入门到精通》——Objective-C的内存管理
- Object Attributes
- objective-c中的self和super关键字
- org.springframework.jdbc.object
- Java中的Object类介绍