110 Views
June 12, 16
スライド概要
カジュアル Swift 勉強会 #9 で話したスライドです。勉強会中では 3 つまで紹介してたっぷり楽しみましたけど、そんな資料にも続きがあるので、せっかくなのでアップしておきますね。
※ Docswell での公開に移行する直前の Slideshare での閲覧数は 3,832 でした。
正統派趣味人プログラマー。プログラミングとは幼馴染です。
4XJGU ͷ৽ػೳ ͱΓ͋͑ͣͳʹؾΔͱ͜Ζ͔Β ΧδϡΞϧ4XJGUษڧձ &;/&5۽୩༑ 4XJGUEFW-0$"- IUUQF[OFUKQ
۽୩༑ !FT@LVNBHBJ &;/&5IUUQF[OFUKQ ษڧձ ԣJ1IPOF։ൃऀษڧձ ΧδϡΞϧ4XJGUษڧձ ZJEFW ˏԣɾഅंಓ DTXJGU !ԣɾ੨༿ Θ͍Θ͍ɾΏΔ͘ɺJ1IPOF։ൃऀͷ ΈΜͳͰָ͘͠ա͢͝ͷ͕తͷձ ΏΔ͘ΈΜͳͰ4XJGUʹ͍ͭͯ ޠΒ͑Δ͕ཉͯ࢝͘͠Ίͨձ ୈ̎̐ճΛʹ։࠵༧ఆ ୈ̍̌ճ݄ޙ͔ʁ
۽୩༑ !FT@LVNBHBJ &;/&5IUUQF[OFUKQ ॻ੶ొஃ 9DPEFపఈղઆ .04" 9DPEFͷશػೳΛ పఈతʹղઆͨ͠ຊ 049J04ܥͷྺ࢙ਂ͍ ༗ྉձһ੍ͷษڧձ 9DPEFͰཱͭͣ ๏ਓձһଟ 紙版は絶版、電⼦書籍は販売中
۽୩༑ !FT@LVNBHBJ &;/&5IUUQF[OFUKQ J04 049 "QQMF8BUDIΞϓϦ $PEF1JFDF ͍ͭͷి ιʔείʔυΛ5XJUUFSͱ (JTUʹಉ࣌ߘͰ͖Δɻ ࣜࢉܭ͑ݟΔిΞϓϦɻ XBUDI04ରԠ ԻͰ࠶ୡΰου &;/&5*11IPOF ؆୯ૢ࡞Ͱ ࠶ୡͷਃ͠ࠐΈɻ J1IPOFͰͻ͔ΓిΛ͏ɻ ࣗ-"/͔Βͷར༻ઐ༻
$PEF1JFDFGPS04 9 ษڧձΛָ͠ΉΞϓϦ ιʔείʔυΛ5XJUUFSͱ(JTUʹಉ࣌ߘͰ͖Δ ษڧձͰݟΛΈΜͳͱڞ༗͍ͨ࣌͠ͱ͔ʹศརʂ Ͱ͖Δ͜ͱ #cswift
4XJGU XJMMCFSFMFBTFETPNFUJNFJOMBUF
88%$ ʙ
88%$ ͰͳʹؾΔηογϣϯ 8IBU`T/FXJO4XJGU
0QFO4PVSDFʹͳ͍ Ӆ͕͠ۄདྷΔ͔ʁ
4XJGUΛ ༧श͓͔ͯ͠ͶͳΒͳ͍
લճ 4XJGU Λ༧श։࢝ 4& IUUQXXXTMJEFTIBSFOFUUPNPIJSPLVNBHBJTXJGUDTXJGU
ͦͯ͠ɺࠓճ
4XJGU ͷ৽ػೳ ͦͷ͏ͪͷ͍͔ͭ͘ΛோΊͯΈΔ
͏Θͬʜ4XJGUͷมߋɺଟ͗͢ʜʁ
4XJGU ͷ৽ػೳ ຊͷࢿྉ֓ཁ શཏͻͱ·ͣఘΊΔʜ ७ਮ4XJGUΛத৺ʹϐοΫΞοϓ ͦΕͰɺͯ͢ޠΓ͖Εͳ͍ 0CKFDUJWF$Λҙࣝͨ͠ػೳޙ ·͔͞ͷௐ͖Εͳ͍ͱ͔ແ೦͗͢Δ
ॻࣜͷݟ͠
4& ଐੑҾͷ&RVBMΛ$PMPOʹมߋ
ଐੑҾͷ&RVBMΛ$PMPOʹมߋ ֓ཁ ⾣ ଐੑҾͷΛ:Ͱද͢ݱΔ ⾣ ೖͱҧ͏Ͱ͠ΐ͏ʁͱ͍͏Ձ؍ @available(*, unavailable, renamed: "NewProtocol") typealias PreviousProtocol = NewProtocol 4XJGU
༨ஊ !TXJGU@NJHSBUJPOଐੑ
!TXJGU@NJHSBUJPOଐੑʁ ֓ཁ ⾣ ϚΠάϨʔγϣϯͷͨΊͷใΛ͖ͰࡌهΔʁ ⾣ %FWFMPQFS1SFWJFXͰࢼͤͳ͍
!TXJGU@NJHSBUJPOଐੑʁ جຊػೳ ⾣ !TXJGU@NJDSBUJPOΛܕؔͳͲʹࢦఆ ⾣ Ҿͱͯ͠renamedmessageΛࢦఆՄೳ @swift3_migration(message: "MESSAGE") func doSomething() { … } @swift3_migration(renamed: "NewProtocol") typealias PreviousProtocol = NewProtocol 4XJGU
4&ଐੑҾͷ&RVBMΛ$PMPOʹมߋ ײ ม͑Δ΄ͲͷͷͰͳ͍Α͏ͳҹ ͦΕΑΓ!TXJGU@NJHSBUJPOʹࠦΒΕΔ
4& ԋࢉࢠηΫγϣϯͰͷίϝϯτͷѻ͍มߋ
ԋࢉࢠηΫγϣϯͰͷίϝϯτͷѻ͍มߋ ֓ཁ ⾣ ίϝϯτͷҐஔͰԋࢉࢠͷղऍ͕มΘ͍ͬͯͨ ⾣ ίϝϯτΛۭനจࣈʹͯ͠ɺײతͳղऍʹ if /* comment */!isValid { ... } !͕ɺ྆ଆΛѻ͏தஔԋࢉࢠͱͯ͠ѻΘΕ͍ͯͨΒ͍͠ value +/* comment */coefficient +/*͕ɺ྆ଆΛѻ͏தஔԋࢉࢠͱͯ͠ѻΘΕ͍ͯͨΒ͍͠ 4XJGUY
ԋࢉࢠηΫγϣϯͰͷίϝϯτͷѻ͍มߋ ղऍͯ͠Β͑ͳ͘ͳΔॻࣜ ⾣ ৽ͨʹ4XJGUͰղऍͰ͖ͳ͘ͳΔͷ͕͋Δ ⾣ ίϝϯτ͕ۭനจࣈѻ͍ʹͳΔͷ͕Өڹ optionalArray/* Comment */?.count ?͕༧͍ͳ͠ظจࣈͱͯ͠ೝࣝ͞ΕΔΑ͏ʹͳΔ value+/* comment */coefficient +͕ɺޙஔԋࢉࢠͱͯ͠ѻΘΕΔΑ͏ʹͳΔ 4XJGU
4&ԋࢉࢠηΫγϣϯͰͷίϝϯτͷѻ͍มߋ ײ ୈҰҹྑ͍͕ɺޭΛ͔͢ෆ໌ झຯͷྖҬͳΑ͏ͳ
4& *NQMJDJUMZ6OXSBQQFE0QUJPOBMͷഇࢭ
*NQMJDJUMZ6OXSBQQFE0QUJPOBMͷഇࢭ ֓ཁ ⾣ *NQMJDJUMZ6OXSBQQFE0QUJPOBMܕͷഇࢭ ⾣ ҉ΞϯϥοϓΛࣔ͢!ଐੑͱͯ͠ѻ͏ ਜҮᢅәӥӦᒇᦸӓҴӴ let optionalValue: Int! = 100 4XJGU
*NQMJDJUMZ6OXSBQQFE0QUJPOBMͷഇࢭ ࣮ࡍʹͬͯΈΔ ⾣ *NQMJDJUMZ6OXSBQQFE0QUJPOBMͱͯ͠ೝࣝ ⾣ جຊతͳ͍ํಉ͡ var a: Int! var b: Int? // なぜか ImplicitlyUnwrappedOptional<Int> として認識 print(a.dynamicType) // こちらは Optional<Int> 型として認識 print(b.dynamicType) 4XJGU
4&*NQMJDJUMZ6OXSBQQFE0QUJPOBMͷഇࢭ ײ ෆࢥٞʹࢥ͑ͨڍಈ࣮ʹྑ্͍͕Γ ࣮ࡍʹΈΜͳͱࢼͯ͠༡ΜͰΈͨΒ͍Ζ͍Ζ͔ͬͨʂ
4& Ҿϥϕϧ໊ͷنଇΛ౷Ұ
Ҿϥϕϧ໊ͷنଇΛ౷Ұ ֓ཁ ⾣ ୈ̍ҾͷಛྫΛഇࢭɺશͯͰಉ͡نଇʹ ⾣ ΨΠυϥΠϯͷϝιου໋໊نଇ͕ಈػ func function(first: Int, second: Int, third: Int) { } ◌㮫ᐠᡔҶᢡᇰӑ⍋∪ҿӶӔҹӔӍӉ function(first: 1, second: 10, third: 3) ؔɺϝιουɺΠχγϟϥΠβʔͷ શͯͷҾϥϕϧ͕ಉ͡ϧʔϧʹ౷Ұ 4XJGU
Ҿϥϕϧ໊ͷنଇΛ౷Ұ ୈ̍Ҿͷϥϕϧ໊มߋՄೳ ⾣ ֎෦Ҿ໊Λఴ͑ΕมߋՄೳ ⾣ Ͱ_߸هҾϥϕϧΛলུՄೳ // ラベル名を label にする func function(label value: Int) { } ◌㮫ᐠᡔӘՑՁՓൻӼ⍋∪Ӄӵ func function(_ label value: Int) { } 4XJGU
4&Ҿϥϕϧ໊ͷنଇΛ౷Ұ ײ ۠ผ͢Δඞཁ͕ͳ͘ͳ͍ͬͯ͠خ ΨΠυϥΠϯ९कʹߩ͏ͦ͠ݙ
4& !OPFTDBQFͱ!BVUPDMPTVSFΛҠಈ
!OPFTDBQFͱ!BVUPDMPTVSFΛҠಈ ֓ཁ ⾣ มଐੑ͔Βܕଐੑมߋ ⾣ ܕͷੑ࣭Λత֬ʹઆ໌Ͱ͖ΔΑ͏ʹ͢Δ func function(@noescape f: () -> Int) { } XBSOJOH!OPFTDBQFJTOPXBOBUUSJCVUFPOBQBSBNFUFSUZQF JOTUFBEPGPOUIFQBSBNFUFSJUTFMG func function(@autoclosure f: () -> Int) { } 4XJGUY
!OPFTDBQFͱ!BVUPDMPTVSFΛҠಈ ৽͍͠ॻࣜ ⾣ !ʹܕOPFTDBQF!BVUPDMPTVSFΛఴ͑Δ ⾣ !OPFTDBQFͳΫϩʔδϟʔΈ͍ͨʹಡΊΔ func function(f: @noescape () -> Int) { } func function(f: @autoclosure () -> Int) { } 4XJGU
!OPFTDBQF
ʹܕରͯ͠!OPFTDBQFΛఆٛ ߴ֊ؔͰҖྗΛൃش ⾣ !OPFTDBQFͳؔܕΛฦͤΔ ⾣ ෆඞཁʹΤεέʔϓ͢Δඞཁ͕ͳ͘ͳΔ func function(f: () -> Int) -> (@noescape (Int) -> Int) -> Int { return { g in g(f()) } } 4XJGU
ʹܕରͯ͠!OPFTDBQFΛఆٛ ϩʔΧϧมͷؔͰܕ͑Δ ⾣ !OPFTDBQFͳΫϩʔδϟʔΛ࡞ΕΔ ⾣ είʔϓͰ͍ऴΘΔΫϩʔδϟʔʹ࠷ద func method() -> Int { let g: @noescape (Int) -> Int = { $0 + action() * 2 } return function(g) } ͨͩ͠!FTDBQFͳؔܕΛ औΔؔʹͤͳ͍ͷͰҙ 4XJGU
ʹܕରͯ͠!OPFTDBQFΛఆٛ ैલͷTFMGΛলུ͢Δํ๏ ⾣ ҎલؔΛೖΕࢠʹͯ͠TFMGΛলུՄೳ ⾣ Τεέʔϓͳؔͤͯ͠ͱܕΔ func method() -> Int { func g(_ value: Int) -> Int { return value + action() * 2 } return function(g) } ͜ͷ߹!FTDBQFͳؔܕΛ औΔؔʹͤΔ 4XJGU
ʹܕରͯ͠!OPFTDBQFΛఆٛ !OPFTDBQFΛ!OPFTDBQFͰแΜͰฦͤͳ͍ʁ ⾣ Γͷؔ!͕ܕOPFTDBQFͰͳ͍ ⾣ !FTDBQFͳؔܕ!OPFTDBQFΛ࣋ͯͳ͍ func function(@noescape f: () -> Int) -> (@noescape (Int) -> Int) -> Int { return { g in g(f()) } } FSSPSDMPTVSFVTFPG!OPFTDBQFQBSBNFUFSGNBZ BMMPXJUUPFTDBQF 4XJGU
ʹܕରͯ͠!OPFTDBQFΛఆٛ ΓͷؔࣗܕମΛ!OPFTDBQFʹͰ͖Δ ⾣ Γͷؔ!ʹܕOPFTDBQFΛࢦఆ͢Δ ⾣ !OPFTDBQFΛ࣋ͯΔΑ͏ʹͳΔ func function(@noescape f: () -> Int) -> @noescape (@noescape (Int) -> Int) -> Int { return { g in g(f()) } } 4XJGU
!BVUPDMPTVSF
ʹܕରͯ͠!BVUPDMPTVSFΛఆٛ ߴ֊ؔͰҖྗΛൃش ⾣ !BVUPDMPTVSFͳؔܕΛฦͤΔ ⾣ ͼݺग़͠ઌͰͷར༻ΛγϯϓϧʹͰ͖Δ func function() -> (@autoclosure () -> Int) -> Int { return { (f: @autoclosure () -> Int) in f() } } Ͱ࣌ݱ࠷ݶɺҾϦετͷఆ͕ٛඞཁͳ༷ࢠ // 関数で得た、関数を取る関数に、値をそのまま渡せる let f = function() let result = f(100) 4XJGU
ʹܕରͯ͠!BVUPDMPTVSFΛఆٛ ؔܕͷมͰ͑Δ ⾣ !BVUPDMPTVSFΛҾʹऔΔؔܕΛ࡞ΕΔ ⾣ ͼݺग़͠ઌͰͷར༻ΛγϯϓϧʹͰ͖Δ let f: () -> (@autoclosure () -> Int) -> Int = { return { (f: @autoclosure () -> Int) in f() } } // 関数を取る関数に、値をそのまま渡せる let result = f(100) 4XJGU
ʹܕରͯ͠!BVUPDMPTVSFΛఆٛ มΛ!BVUPDMPTVSFʹܕͰ͖ͳ͍ ⾣ !BVUPDMPTVSFͳؔܕͷม࡞Εͳ͍ ⾣ ࡞ΕͨΒָͦ͠͏ͩͬͨͷʹ let value: @autoclosure () -> Int = { return 100 } FSSPS!BVUPDMPTVSFNBZPOMZCFVTFEPOQBSBNFUFST 4XJGU
4&!OPFTDBQFͱ!BVUPDMPTVSFΛҠಈ ײ Ϋϩʔδϟʔ͕ૉΒ͘͠ਐԽͨ͠ҹ !OPFTDBQFΫϩʔδϟʔ͕ಛʹΈʂ
4& ʹܕରͯ͠JOPVUΛఆٛ
ʹܕରͯ͠JOPVUΛఆٛ ֓ཁ ⾣ ϥϕϧʹఴ͍͑ͯͨJOPVUΛܕ ⾣ ϥϕϧͱJOPVU͕۠ผ͠ʹ͍ͨ͘Ί func method(inout result: Int) { } FSSPSJOPVUCFGPSFBQBSBNFUFSOBNFJTOPUBMMPXFE QMBDFJUCFGPSFUIFQBSBNFUFSUZQFJOTUFBE ՑՁՓҶ⋭ӃӵӘӒൺӂᙟ func method(inOut result: Int) { } 4XJGUY
ʹܕରͯ͠JOPVUΛఆٛ ৽͍͠ॻࣜ ⾣ ʹܕఴ͑Δ͜ͱͰϥϕϧͱ໌֬ʹ۠ผՄೳ ⾣ ͰJOPVUͰܕແ͍Α͏ͳؾ͢Δʁ func method(result: inout Int) { } ㋚ႁᡔൻӘፗҶՑՁՓൻӑҬӵҽӒҶᣀ⎰ func method(inOut result: inout Int) { } 4XJGU
ʹܕରͯ͠JOPVUΛఆٛ ϥϕϧ໊ʹJOPVUͦͷ··͑ͳ͍ ⾣ JOPVUϥϕϧ໊ͷՕॴͰ༧ޠ ⾣ ϥϕϧ໊ʹ͍͍ͨͳΒΤεέʔϓ͕ඞཁ func method(inout result: inout Int) { } FSSPSJOPVUCFGPSFBQBSBNFUFSOBNFJTOPUBMMPXFE QMBDFJUCFGPSFUIFQBSBNFUFSUZQFJOTUFBE ԐԡԙդԿӃӶӚਜҲӵѭහӝ௲ӁᣪӬԐԡԙդԿҶᒨ⺙ func method(`inout` result: inout Int) { } 4XJGU
ʹܕରͯ͠JOPVUΛఆٛ ৽͍͠ॻࣜ ⾣ ʹܕఴ͑Δ͜ͱͰϥϕϧͱ໌֬ʹ۠ผՄೳ ⾣ ͰJOPVUͰܕແ͍Α͏ͳؾ͢Δʁ func method(result: inout Int) { } ㋚ႁᡔൻӘፗҶՑՁՓൻӑҬӵҽӒҶᣀ⎰ func method(inOut result: inout Int) { } 4XJGU
ʹܕରͯ͠JOPVUΛఆٛ ߴ֊ؔͰҖྗΛൃش ⾣ JOPVUͳؔܕΛฦͤΔ ⾣ ͪΌΜͱJOPVUͱͯ͠ػೳ͢Δ func function() -> (result: inout Int) -> Void { return { (result: inout Int) -> Void in result = 700 } } 4XJGU
ʹܕରͯ͠JOPVUΛఆٛ ܕਪಇ͘ ⾣ ҉ఆٛ͞ΕΔมͰJOPVUՄೳ ⾣ ׳Εͳ͍͔Β͔ɺίʔυΛோΊ͍ͯΔͱෆ҆ func function() -> (result: inout Int) -> Void { return { $0 = 700 } } 4XJGU
ʹܕରͯ͠JOPVUΛఆٛ ؔܕͷมͰ͑Δ ⾣ ؔܕͷมͰJOPVU͕Մೳ ⾣ ͍ํීஈͷͱ͓Γ let f: (inout Int) -> Void = { return { $0 = 5000 } } 4XJGU
4&ʹܕରͯ͠JOPVUΛఆٛ ײ Ϋϩʔδϟʔͱಉ͘͡Մೳੑ͕͕Δҹ ͦͦJOPVUཱͭͷ͔͠Β
ػೳͷআ
4& ΧϦʔԽؔߏจͷআ
ΧϦʔԽؔߏจͷআ ֓ཁ ⾣ ΧϦʔԽߏจΛআ͢Δ ⾣ ͦͦେͯ͠ʹཱͨͳ͍Ͱ͠ΐ͏ʁ func freeFall(context: Context)(time: Double) -> Double { return -1 / 2 * time * time * context.gravitationalAcceleration } FSSPSDVSSJFEGVODUJPOEFDMBSBUJPOTZOUBYIBTCFFOSFNPWFE VTFBTJOHMFQBSBNFUFSMJTU 4XJGUY
ΧϦʔԽؔߏจͷআ ߴ֊ؔΛ͏ํ๏ʹҰຊԽ ⾣ ༷ޠݴͷෳࡶ͞Λੋਖ਼ ⾣ ಈ࡞ΧϦʔԽߏจͱ·ͬͨ͘ಉ͡ func freeFall(context: Context) -> (time: Double) -> Double { return { time in -1 / 2 * time * time * context.gravitationalAcceleration } } 4XJGU
ΧϦʔԽؔߏจͷআ ΧϦʔԽؔͷ༻ྫ // コンテキストを固定し、その環境を前提に動く関数を定義 let earth = Context(identifier: "Earth", gravitationalAcceleration: 9.80665) let freeFallOnEarth: (time: Double) -> Double = freeFall(context: earth) // 汎用関数でスマートに捌く let timeInterval = stride(from: 0.0, through: 100.0, by: 1.0) let locations = timeInterval.map(freeFallOnEarth) 4XJGU
ΧϦʔԽؔߏจͷআ YΧϦʔԽߏจͷཹҙ ⾣ ॊೈͳείʔϓ੍ޚΛߦ͑ͳ͍ ⾣ ࠷Ͱ·ޙΠϯελϯε͕อ࣋͞ΕΔ func function(object: Object)(multiple: Int) -> Int { let value = object.value return value * multiple } do { let f = function(Object(100)) print(f(multiple: 5)) είʔϓऴྃ·Ͱ 0CKFDUอ࣋ } 4XJGUY
ΧϦʔԽؔߏจͷআ ߴ֊ؔͰࣗͰ੍ޚՄೳ ⾣ ҙࣝతͳείʔϓ੍͕ޚՄೳ ⾣ ฦؔ͢ʹ͚ͳ·ؚΕอ࣋͞Εͳ͍ func function(object:Object) -> (multiple:Int) -> Int { let value = object.value return { multiple in value * multiple } } ؔΛฦͨ࣌͠Ͱ 0CKFDU͕ϑϦʔʹͳΔ do { let f = function(Object(100)) print(f(multiple: 5)) } 4XJGU
4&ΧϦʔԽؔߏจͷআ ײ ैདྷ௨Γͷߴ֊ؔͰସՄೳ ΫϩʔδϟʔͷਐԽͱ૬·ͬͯૉΒ͍͠
4& 4& ҾϦετ͔ΒWBSͱMFUΛআ
ҾϦετ͔ΒWBSͱMFUΛআ
֓ཁ
⾣ ߏจͷՁͷׂʹࠞཚ͢Δ͔Βআ
⾣ JOPVUͱWBSͷҙຯΛࠞಉ͍͢͠ʁ
func function(var initial value: Int, count: Int) -> Int {
FSSPSQBSBNFUFSTNBZOPUIBWFUIFWBSTQFDJpFS
(1 ..< count).forEach { _ in
value += value
}
return value
}
4XJGUY
ҾϦετ͔ΒWBSͱMFUΛআ
WBSͷಈ͖Λ͓͞Β͍
ՄมมWBS͕࡞ΒΕɺෳͨ͠Λอ࣋
func function(var initial value: Int, count: Int) -> Int {
(1 ..< count).forEach { _ in
value += value
}
ՄมมͳͷͰॻ͖͑Մೳ
return value
}
ͼݺग़͠ઌͷWBSม͕ॻ͖͑ΒΕͯ
ݩͷมWBMVFӨڹΛड͚ͳ͍
var value = 10
let answer = function(initial: value, count: 5)
ͦͦॻ͖͍ͳ͑ͷͰMFUͰྑ͍
4XJGUY
ҾϦετ͔ΒWBSͱMFUΛআ JOPVUͷಈ͖Λ͓͞Β͍ ՄมมWBS͕࡞ΒΕɺෳͨ͠Λอ࣋ func function(initial value: inout Int, count: Int) { (1 ..< count).forEach { _ in value += value } } ՄมมͳͷͰɺมࣗମ͕ॻ͖͑Մೳ είʔϓΛൈ͚ΔͱɺՄมมΛݩͷมʹॻ͖͢ JOPVUʹ͢มɺॻ͖ม͑ʢॻ͖͠ʣΛ͏ͷͰWBSͰએݴ var value = 10 ͼݺग़࣌͠lzΛ໌ͯ͢͠هʢࢀর͠Ͱͳ͍ʣ function(initial: &value, count: 5) 4XJGU
ҾϦετ͔ΒWBSͱMFUΛআ
ҾϦετͰWBS͕͍͑ͯͨࠒͷར
⾣ ͞ΕͨΛʹݩଈ࠲ʹฤूͰ͖Δ
⾣ มWBSʹ࠶ೖ͢Δख͕ؒল͚Δ
func function(var initial value: Int, count: Int) -> Int {
// 即座に value に変更を仕掛けていける
(1 ..< count).forEach { _ in
value += value
}
return value
}
4XJGUY
ҾϦετ͔ΒWBSͱMFUΛআ
ҾϦετͰWBS͕͑ͳ͘ͳͬͨ߹
⾣ ͞ΕͨΛWBSͰड͚͢
⾣ ͋ͱࠓ·Ͱ௨ΓͷॲཧͰ0,
func function(initial value: Int, count: Int) -> Int {
// 編集できるように新しい可変値変数で受け直す
var value = value
(1 ..< count).forEach { _ in
value += value
}
return value
}
4XJGU
ҾϦετ͔ΒWBSͱMFUΛআ
ͦͦWBSΛΘͳ͍खΛ୳͠·ͤΜ͔ʁ
⾣ ͷʹࢉܭWBSΛ࣋ͪग़͞ͳ͍εεϝ
⾣ ༡ΜͰΈΔͱ4XJGUΒ͕ͯ͑͘͠͞ݟΔʁ
func function(initial value: Int, count: Int) -> Int {
return (1 ..< count).reduce(value) { value, _ in
value + value
}
}
4XJGU
ҾϦετ͔ΒWBSͱMFUΛআ
ҾϦετͰͷMFU໌ࣔࢦఆআ
⾣ ҾϦετͰWBS͕ͳ͍ͳΒMFU໌ࣔෆཁ
⾣ ͯ͑MFUΛॻ͘ػձͳ͔ͬͨͣ
func function(let initial value: Int, count: Int) -> Int {
FSSPSMFUBTBQBSBNFUFSBUUSJCVUFJTOPUBMMPXFE
return (1 ..< count).reduce(value) { value, _ in
value + value
}
}
4XJGU
ҾϦετ͔ΒWBSͱMFUΛআ ҾϦετҎ֎ͳΒWBSͰड͚ΒΕΔ // if var 構文で使用可能 if var value = optionalValue { … } // while 構文で使用可能 while var value = iterator.next() { … } // for 構文で使用可能 for var value in values { … } // switch 構文で使用可能 switch optionalValue { case .some(var value): … case .none: … } 4XJGU
ҾϦετ͔ΒWBSͱMFUΛআ HVBSEͰWBSͰड͚ΒΕΔ ⾣ HVBSEͭͭ͠WBSมΛఆٛͰ͖Δ ⾣ ൺֱతεϚʔτ͔ʁʢΦεεϝ͠ͳ͍ʣ // guard var 構文が使用可能 guard var value = optionalValue else { fatalError() } // これ以降で可変値変数 value が使える value += 200 4XJGU
ͻͱ·ͣɺ͜͜·Ͱ
4XJGU ͷ৽ػೳ ͻͱ·ͣɺ͓͠·͍ 4&ଐੑҾͷ&RVBMΛ$PMPOʹมߋ 4&ԋࢉࢠηΫγϣϯͰͷίϝϯτͷѻ͍ 4&*NQMJDJUMZ6OXSBQQFE0QUJPOBMͷഇࢭ 4&Ҿϥϕϧ໊ͷنଇΛ౷Ұ 4&!OPFTDBQFͱ!BVUPDMPTVSFΛҠಈ 4&ʹܕରͯ͠JOPVUΛఆٛ 4&ΧϦʔԽߏจͷআ 4&ҾϦετ͔ΒWBSΛআ 4&ҾϦετ͔ΒMFUΛআ