Home  >  Q&A  >  body text

ios - swift 元组 ,闭包 的一些问题?

class TTTTT {
    
    var mothod1: (String, String) -> Array<String> {
        
        return {
            [$0.0,$0.1]
        }
    }
    
    var mothod2: (String) -> Array<String> {
        
        return {
            [$0]
        }
    }
}
let test = TTTTT()
test.mothod1("s", "sss")
test.mothod2("ssa")

这段代码如何理解,

  1. mothod1是方法么?

  2. mothod1的参数是元组?

  3. mothod2的参数是一个字符串?

  4. 这种写法有什么优势?

  5. 这是方法作为属性么?

下面这样写为啥不行

研究了一会,

  1. 是闭包类型的变量

  2. mothod1的参数是元组
    原因:

  1. mothod1, mothod2应该是闭包类型

ringa_leeringa_lee2714 days ago411

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 09:53:24

    Mothod1 and method2 are both computed properties. That is to say, the code in the closure will be run every time it is called. So this feature can be used to dynamically calculate results.
    var mothod1: The following are all parameters {
    means that the type of mothod1 variable is pass in the closure of the tuple and return the array
    Right. The word is spelled wrong. method . . . .

    reply
    0
  • Cancelreply