ios - swift 元组 ,闭包 的一些问题?
ringa_lee
ringa_lee 2017-04-18 09:51:24
0
1
420
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_lee
ringa_lee

ringa_lee

全部回覆(1)
Peter_Zhu

mothod1和mothod2都是計算屬性。也就是說每次呼叫都會執行一次閉包裡的程式碼。所以可以把這種特性用來動態計算結果。
var mothod1: 後面的都是參數 {
表示mothod1變數的型別是 傳入元祖回傳數組的閉包
對了。單字寫錯了。 method 。 。 。 。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!