Objective-C 调用 Swift
Swift 类
Swift类定义前加入@objc关键字,并使类继承自NSObject
@objc class S : NSObject{
func m() {
print(__FUNCTION__)
}
}
在调用Swift代码的Objc文件中加入名类似为"xxx-Swift.h"的头文件
头文件具体的名字可在 设置选项:
{project} - {targets} - {build settings} - {swift compiler - code generation} - {Objective-C Generated Interface Header Name}

#import "objc_swift-Swift.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
S* s = [[S alloc]init];
[s m];
}
return 0;
}
Swift 调用 Objective-C
创建桥接文件

选择Create Bridging Header
桥接文件在如下设置位置
{project} - {targets} - {build settings} - {swift compiler - code generation} - {Objective-C Bridging Header}
将要调用的ObjC类头文件import至桥接文件中
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "O.h"
ObjC 代码
.h 文件
@interface O : NSObject
-(void)m;
@end
.m 文件
@implementation O
-(void)m {
NSLog(@"%s", __FUNCTION__);
}
@end
swift 文件
@objc class S : NSObject{
let o:O = O()
func m() {
o.m()
print(__FUNCTION__)
}
}
示例工程
objc-swift
日期: 2016-04-01 17:30:06, 8 years and 289 days ago
accessor method
accessor表示两个方法,一个叫做getter用于获取,另一个叫做setter用于设置。 getter形式为 - (type)name, setter形式为-(void)setName。
accessor method represents two method. one is getter and the other is setter.
the method of getter defaults as ‘-(type)name and setter defaults as -(void)setName
@property
@property 生成 instance variable 和 accessor。
@property makes instance variable (ivar) and accessor
@synthesize
@synthesize与@property配对,提供将@proerty与成员变量绑定的功能,即指定@property的inistance variable为特定的成员变量。
synthesize prepairs @property.in @synthesize the ivar can bind a class member, @syntax property-name=ivar-name
atomic
atomic的意思是读和写是原子操作,但是线程安全要保证同时读写数据不会出错,所以atomic无法保证。本质就是在getter和setter加入一个锁,赋值或取值前锁定,结束后解除。nonatomic/atomic 原子操作,但不能保证线程安全,默认为atomic,常用为nonatomic。
nonatomic/atomic is atomic operation. this does not gurantee the safe-thread. default value is atomic.atomic means the reading and writing are atomic each,but the safe-thread requests reading and writing in the same time do not case the problem. the atomic add the lock in getter and setter.
readonly/readwrite
读写或只读,本质就是生成getter和setter或者只生成getter
readwrite makes the getter and setter and the readonly makes the only getter.
weak/strong(assgin/retaine) copy
weak/strong in ARC, weak means assgin in setter, strong means reference count draining in setter(complite manages automatic reference count). assgin/reatin in MRC are deprecated now. copy means using copy in setter.
weak/strong作为ARC自动引用计数时代,weak本质是setter内直接赋值,strong本质是setter时增加引用计数(引用计数由编译器,编译时增加)(assign/retain 为MRC手动引用计数时代的标志,已经被淘汰)
copy 表示setter中使用copy方式
dotsyntax
dotsyntax本质是转化为 getter 和 setter
dotsyntax means sending getter or setter message.
日期: 2016-02-26 17:30:06, 8 years and 324 days ago
解决UITableVIewHeaderCell的布局和警告问题
in the custom UITableViewHeaderFooterCell, the width of the reuse view can not
be configed in a right way. And there is a warnning
'Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.'
during the running.
the reason that the view that is added to xib is UITableViewCell. instead of UICollectionReusableView, the problems are fixed.
在定制UITableView的section的Footer时,重用的模板view不能正确的布局,width总不能正确匹配并且
程序运行时,总是出现'Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.'的警告。
原因是,在向xib添加view时,使用的是UITableViewCell,用UICollectionReusableView替换即可可解决问题。
日期: 2015-10-23 17:30:06, 9 years and 85 days ago