客观c阻止迅速

时间:2015-12-25 18:53:41

标签: swift closures

我试图从Swift调用Objective-C类的方法。这是一个名为BoString的pod,这里是片段

- (BOStringAttribute *(^)(NSParagraphStyle *))paragraphStyle
{
    return ^BOStringAttribute *(NSParagraphStyle *paragraphStyle) {
        return self.attribute(NSParagraphStyleAttributeName, paragraphStyle);
    };
}

在Swift中,我尝试调用此方法,但编译器在尝试传递paragraphstyle

中的任何参数时会抱怨
let str = BOStringMaker()
str.paragraphStyle() // OK
//*** error
str.paragraphStyle({(ps) -> BOStringAttribute in
   // logic here
})

在Objective-C中,我使用了

BOStringMaker *make = [BOStringMaker alloc] init]
make.paragraphStyle(ps);

但是我无法在Swift中做同样的事情

1 个答案:

答案 0 :(得分:1)

在swift中你做同样的事情。你得到paragraphStyle这是块/函数,然后只需调用它并传入ps或你当前持有的NSParagraphStyle的任何实例作为参数:

formatText.paragraphStyle()(ps)