简体   繁体   English

如何用Swift语法编写这个Objective-C块?

[英]How to write this objective-c block in swift syntax?

I recently switched from working with Objective-C to Swift and now I am facing the following objective-c block. 我最近从使用Objective-C切换到Swift,现在我面对以下的Objective-C模块。 I cannot figure out how to write it in Swift (1.2) syntax . 我不知道如何用Swift(1.2)语法编写它。 This is how the block is defined in objective-c: 这是在objective-c中定义块的方式:

formatter = ^(CGFloat value){

            // do something with value

            return "stringrepresentation";
        };

Could someone be kind and show me the correct syntax? 有人能和kind并告诉我正确的语法吗?

ADDITIONAL Info: the "formatter" value needs to be of type ValueFormatter 其他信息: “格式器”值必须为ValueFormatter类型

How to achieve this? 如何实现呢?

That would look like this: 看起来像这样:

let formatter = { (value: CGFloat) -> String in
    // do something with value
    return "the formatted thing"
}

The type of formatter will be (CGFloat) -> String . formatter的类型为(CGFloat) -> String

Look at closures . 看一下闭包

{(/*Parameters*/) -> /*ReturnType*/ in
    // Statements
}

So this block would be: 因此,该块将是:

{(value : CGFloat) -> String in
    // Do cool stuff
    retun "aString"
}

SWIFT 2 SWIFT 2

Block Declarations 块声明

var myCallBack:((Bool)->Void)? = nil

Block sending back 阻止发回

  self.myCallBack!(true)

Block Receiving back 阻止收货

myCallBack = { (isSuccess: Bool)-> () in
          //perform action you want to      
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM