Moustafa Elgamel
Moustafa Elgamel

Reputation: 23

objective c block to swift

I'm trying to invoke a method on an Objective-C class from Swift. It's for a pod called BoString, and here's the snippet

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

In Swift, I'm trying to invoke this method but the compiler complains if I try to pass any parameter in paragraphstyle

let str = BOStringMaker()
str.paragraphStyle() // OK
//*** error
str.paragraphStyle({(ps) -> BOStringAttribute in
   // logic here
})

In Objective-C, I used

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

But i'm unable to do the same in Swift

Upvotes: 0

Views: 86

Answers (1)

luk2302
luk2302

Reputation: 57114

In swift you do the exact same thing. You get the paragraphStyle which is the block / function and then simply invoke it and pass in ps or whatever instance of NSParagraphStyle you currently hold as parameter:

formatText.paragraphStyle()(ps)

Upvotes: 1

Related Questions