Swift中建立Array和Dictionary的初始化

來源:互聯網
上載者:User

首先看下Swift中初始化Array的方法,下面的類型都以Int為例

var a1 = [1, 2, 3]

var a2: Int[] = [1, 2, 3]

var a3: Int[] = Int[]([1, 2, 3])

var a4: Int[] = Array<Int>([1,2,3])

var a5: Array<Int> = [1,2,3,]

var a6 = Array<Int>([1,2,3])

var a7: Array<Int> = Array<Int>([1,2,3])

接下來是Dictionary, 因為其內部的init方法的實現它的初始化方法少了很多,以String, String為例

var d1 = ["1":"2"]

var d2: Dictionary<String, String> = ["1":"2"]

var d3: Dictionary<String, String> =  Dictionary<String, String>()

var d4 =  Dictionary<String, String>()


以上列舉出來的方法,也是在學習The Swift Programming language的時候學習和自己測試出來的,如果還有其他方法,請朋友們留言

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.