countͬΆ͍͚ΕͲ w ʮ࠷ޙͷΠϯσοΫε ʯΛࣔ͢ w count0 O ɺendIndex0 7 let list = ["DeNA", "STORES", "Rakuten Rakuma”] print(list.count) // 3 print(list.endIndex) // 3
for-inΛճॻ͘ඞཁ͕ͳ͘ͳΔ 13 let years = [2021, 2022, 2023] let months = ["January", "February", "March", /* ... */] for year in years { for month in months { print(year, month) /* 2021 January 2021 February 2021 March ... 2022 January 2022 February 2022 March ... */ } }
for-inΛճॻ͘ඞཁ͕ͳ͘ͳΔ w 4XJGU"MHPSJUINTͷproduct(_:_:)ͰωετΛݮΒͤΔ w Ұํ͕ۭͷ߹ɺແବͳϧʔϓॲཧ͕ൃੜ͠ͳ͍ 14 import Algorithms for (year, month) in product(years, months) { print(year, month) }
forEach(_:)ࣗମfor-inͰ࣮͞Ε͍ͯΔ͚Ͳ 15 func printWithLowercased(_ value: String) { print(value.lowercased()) } let list = ["DeNA", "STORES", "Rakuten Rakuma"] for name in list { printWithLowercased(name) } // ಉ͡ग़ྗ list.forEach { name in printWithLowercased(name) } // ಉ͡ग़ྗ
arc4random()arc4random_uniform(_:)Θͳ͍ w 4XJGU͔Β͑ΔΑ͏ʹͳͬͨ random()ɾrandom(in:)ɾrandomElement()Λ͏ 18 let flag = Bool.random() let num = Int.random(in: 0..<10) let area = [1: "Shibuya", 2: "Shinjuku"].randomElement() let player = ["uhooi", "treastrain"].randomElement()
ʮHFU͢Δͱ͖·ͰʹඞͣTFU͞Ε͍ͯΔʯϓϩύςΟͰ 20 enum Area { case osaka, hiroshima } let area: Area = // ... var okonomiyaki: String? switch area { case .osaka: okonomiyaki = "ؔ෩" case .hiroshima: okonomiyaki = "ౡ෩" } print(okonomiyaki)
ʮHFU͢Δͱ͖·ͰʹඞͣTFU͞Ε͍ͯΔʯϓϩύςΟͰ 21 enum Area { case osaka, hiroshima } let area: Area = // ... let okonomiyaki: String switch area { case .osaka: okonomiyaki = "ؔ෩" case .hiroshima: okonomiyaki = "ౡ෩" } print(okonomiyaki)
͕࣌ؒΓͳ͔ͬͨʜʜ w switchจͰͪΌΜͱཏͯ͠ɺ ݟམͱ͠Λແͨ͘͠Γޙͷมߋʹڧͨ͘͠Γ͢Δ w Resultͱεϩʔؔʢ5ISPXJOH'VODUJPOTʣͷ૬ޓม w %FMFHBUFɾΫϩʔδϟΛBTZODͳؔʹ͢Δ w NotificationCenterΛ4XJGU$PODVSSFODZ͔Β͏ w $PNCJOFɾ3Y4XJGUΛ4XJGU$PODVSSFODZ͔Β͏ 29