小编cjw*_*rth的帖子

当被称为Objective-C块时,Swift闭包崩溃

在我的项目中,我有Objective-C和Swift代码.我有一些对象具有包含块的属性来清理一些UITableView配置.使用它在Objective-C中工作,但在使用Swift时崩溃.

我已经将问题减少到尽可能小,同时仍然可以重现.

// in Objective-C
@interface MyClass : NSObject
@property (copy, nonatomic) NSString* (^block)();
- (NSString *)callTheBlock;
@end

@implementation MyClass
- (NSString *)callTheBlock {
    if (self.block) {
        return self.block();
    } else {
        return @"There is no spoon";
    }
}
@end

// In Swift
// I actually have this in my AppDelegate to run when the app starts, but that shouldn't matter

class AppDelegate: UIResponder, UIApplicationDelegate {
    var myClass: MyClass?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    self.myClass = …
Run Code Online (Sandbox Code Playgroud)

closures objective-c ios objective-c-blocks swift

12
推荐指数
1
解决办法
9219
查看次数

标签 统计

closures ×1

ios ×1

objective-c ×1

objective-c-blocks ×1

swift ×1