Heim  >  Fragen und Antworten  >  Hauptteil

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 Tage vor414

Antworte allen(1)Ich werde antworten

  • 天蓬老师

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

    mothod1和mothod2都是计算属性。也就是说每次调用都会运行一次闭包里的代码。所以可以把这种特性用来动态计算结果。
    var mothod1: 后面的都是参数 {
    意味着mothod1变量的类型是 传入元祖返回数组的闭包
    对了。单词写错了。 method 。。。。

    Antwort
    0
  • StornierenAntwort