<!-- generator=" CodePongo (Base on Kukkaisvoima version 15b3TA)" -->
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
>
<channel><title>CodePongo: swift</title><link>http://codepongo.com/blog</link><description></description><pubDate>Fri, 06 May 2016 17:30:06 +0200</pubDate><lastBuildDate>Fri, 06 May 2016 17:30:06 +0200</lastBuildDate><generator>http://codepongo.com/blog</generator><language>zh-cn</language><item><title>闭包的内存管理
</title><link>http://codepongo.com/blog/3uH5ah</link><comments>http://codepongo.com/blog/3uH5ah#comments</comments><pubDate>Fri, 06 May 2016 17:30:06 +0200</pubDate><dc:creator>zuohaitao</dc:creator><category>swift</category><category>closure</category><guid isPermaLink="false">http://codepongo.com/blog/3uH5ah/</guid><description><![CDATA[ 
 [...]]]></description><content:encoded><![CDATA[

  do {
        class Holder {
Void)?
            deinit {
                print("\(self.dynamicType) \(#function)")
            }
        }
        {
            let h = Holder()
            h.closure = {
                [weak h] in // weak
                guard let h = h else { return } // here comes the weak–strong dance
                print(h) // strong
            }
        }()
    }
}


循环引用是两个对象实例作为彼此的属性成员互相持有导致的。

对于在实例的方法中对其他实例进行持有（引用计数加一）是不会造成循环引用的，因为当方法执行完毕后，其局部变量自动摧毁时，对其引用对象的引用计数自动减一。

闭包的循环引用是因为闭包的镜像表（相当于闭包对象实例的成员）持有了闭包拥有者实例，而其拥有者又持有了闭包实例，即如上所述，两个对象实例作为彼此的属性成员互相持有造成了循环引用。所以在镜像表中做弱引用或无主引用声明后（如实例代码注释weak的部分），再在闭包内做强弱转换（示例代码注释weak-strong dance部分），这相当于在闭包镜象实例的方法中持有其他对象，于是就避免了闭包循环引用。

]]></content:encoded><wfw:commentRss>http://codepongo.com/blog/3uH5ah/feed/</wfw:commentRss></item><item><title>Swift 和 Objective-C 互相调用
</title><link>http://codepongo.com/blog/Z6bDt</link><comments>http://codepongo.com/blog/Z6bDt#comments</comments><pubDate>Fri, 01 Apr 2016 17:30:06 +0200</pubDate><dc:creator>zuohaitao</dc:creator><category>objc</category><category>swift</category><category>cocoa</category><category>xos</category><guid isPermaLink="false">http://codepongo.com/blog/Z6bDt/</guid><description><![CDATA[ 
 [...]]]></description><content:encoded><![CDATA[
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

]]></content:encoded><wfw:commentRss>http://codepongo.com/blog/Z6bDt/feed/</wfw:commentRss></item></channel></rss>