本站的主題是商業,創業,美食,葡萄酒,閱讀,網路科技。
這是我的 FB粉專 以及 IG,我比較常使用 Threads,歡迎大家追蹤互動~
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID105
其實 Swift 與 Python 的 data structures 是類似的,Swift 有: Array, Set, and Dictionary.
(以下 sample code 都是出自於上方蘋果教程文件)
Array
initializer syntax:
var someInts = [Int]() var threeDoubles = [Double](count: 3, repeatedValue: 0.0)
array literal (shorthand):
var shoppingList: [String] = ["Eggs", "Milk"] var shoppingList = ["Eggs", "Milk"]
用 [0], [1], … subscript syntax (蘋果官方文件取的名詞) 來存取 member.
Set
initializer syntax:
var letters = Set<Character>()
array literal (對,他不是叫 set literal):
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"] var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
(沒有指定 type Set 就會被當作 Arrary)
Set 不能用 subscript syntax 來存取 member,合理,因為 Set 沒有順序性。用 mySet.contains(item) 來判斷 item 是否存在於 mySet.
Dictionary
initializer syntax:
var namesOfIntegers = [Int: String]()
dictionary literal:
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"] var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
存取用 subscript syntax。
跟 Python 比起來,Swift collections 都是中括號。Swift 也有 Tuple (小括號),用於 loop 的參數值,function 的 input/output 值等等,但不屬於 collection.
蘋果官方 Swift 的教程 (go through an example)
https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/
蘋果官方的 App Distribution Guide
https://developer.apple.com/library/prerelease/ios/documentation/IDEs/Conceptual/AppDistributionGuide/Introduction/Introduction.html
本站的主題是商業,創業,美食,葡萄酒,閱讀,網路科技。