1. Swiftãšã¯ïŒ
è¿å¹Žãã¢ããªéçºã®äžçã§æ³šç®ãéããŠããããã°ã©ãã³ã°èšèªãSwiftã§ãããã®ã»ã¯ã·ã§ã³ã§ã¯ãSwiftã®èµ·æºãèæ¯ããã®ç¹åŸŽãå©ç¹ããããŠObjective-Cãšã®éãã«ã€ããŠè©³ãã解説ããŸãã
1.1. Swiftã®èµ·æºãšèæ¯
Swiftã¯ãAppleã«ãã£ãŠ2014幎ã«çºè¡šãããããã°ã©ãã³ã°èšèªã§ãããã以åã®Appleã®äž»èŠãªããã°ã©ãã³ã°èšèªã§ãã£ãObjective-Cã«ä»£ãããã®ãšããŠéçºãããŸãããSwiftã®ç®çã¯ãéçºè ã«ãšã£ãŠãã䜿ãããããå¹ççãªèšèªãæäŸããããšã§ããããã®çµæãSwiftã¯é«éã§ãã¯ãã«ãªèšèªãšããŠèªçããŸããã
1.2. Swiftã®ç¹åŸŽãšå©ç¹
Swiftã®æ倧ã®ç¹åŸŽã¯ãå®å šæ§ãšå¹çæ§ã«ãããŸããSwiftã¯ãã³ãŒãã®ãšã©ãŒãæ©æã«æ€åºããä¿®æ£ã容æã«ããããã®æ©èœãå€æ°åããŠããŸãããŸããSwiftã¯èªã¿ããããæžããããã·ã³ã¿ãã¯ã¹ãæã£ãŠãããåå¿è ã«ãåŠã³ãããèšèªãšãªã£ãŠããŸããããã«ãSwiftã¯ãªãŒãã³ãœãŒã¹ã§ãããã³ãã¥ããã£ã®ãµããŒããéåžžã«å å®ããŠããŸãã
1.3. SwiftãšObjective-Cã®éã
SwiftãšObjective-Cã®æãé¡èãªéãã¯ãã·ã³ã¿ãã¯ã¹ã§ããObjective-Cã¯Cèšèªã«åºã¥ããŠããããã®ã·ã³ã¿ãã¯ã¹ã¯ç¬ç¹ã§ãããåŠç¿æ²ç·ãããæ¥ã§ãããäžæ¹ãSwiftã¯çŸä»£ã®ããã°ã©ãã³ã°èšèªã®ç¹æ§ãåãå ¥ããããã·ã³ãã«ã§çŽæçãªã·ã³ã¿ãã¯ã¹ãæã£ãŠããŸãããŸããSwiftã¯ã¡ã¢ãªç®¡çã®é¢ã§ã®æ¹åãããšã©ãŒãã³ããªã³ã°ã®åŒ·åãªã©ãå€ãã®æè¡çãªå©ç¹ãæã£ãŠããŸãã
2. Swiftã®åºæ¬æ§é
Swiftã®é åçãªéšåã®äžã€ã¯ããã®æçã§çŽæçãªåºæ¬æ§é ã§ãããã®ã»ã¯ã·ã§ã³ã§ã¯ãSwiftã®åºæ¬çãªèŠçŽ ã§ããå€æ°ãšå®æ°ãããŒã¿åããããŠåºæ¬çãªæŒç®åã«ã€ããŠè§£èª¬ããŸãã
2.1. å€æ°ãšå®æ°
ããã°ã©ãã³ã°ã«ãããŠãå€æ°ã¯ããŒã¿ãä¿åããããã®ååä»ãã®å ŽæãæããŸããSwiftã§ã¯ãvar
ããŒã¯ãŒãã䜿çšããŠå€æ°ã宣èšããŸããäžæ¹ãå®æ°ã¯äžåºŠèšå®ãããšå€æŽã§ããªãå€ãæã€å€æ°ã®ãããªãã®ã§ãlet
ããŒã¯ãŒãã䜿çšããŠå®£èšããŸããå®æ°ã¯ãå€æŽãããããšã®ãªãå€ãå®å
šã«ä¿æããããã«éåžžã«åœ¹ç«ã¡ãŸãã
2.2. ããŒã¿åãšãã®ç¹æ§
Swiftã¯åå®å
šãªèšèªã§ãããããããã®å€æ°ãå®æ°ã«ã¯ç¹å®ã®ããŒã¿åãé¢é£ä»ããããŠããŸããäž»ãªããŒã¿åã«ã¯ãInt
ïŒæŽæ°ïŒãDouble
ïŒå粟床浮åå°æ°ç¹æ°ïŒãString
ïŒæååïŒãªã©ããããŸããSwiftã®ããŒã¿åã¯ãæ確ã§äºæž¬å¯èœãªæ¹æ³ã§ããŒã¿ãæäœããããšãä¿èšŒããŸãã
2.3. åºæ¬çãªæŒç®åãšãã®äœ¿çšäŸ
Swiftã«ã¯ãæ°åŠçãªèšç®ãå€ã®æ¯èŒãè¡ãããã®å€ãã®æŒç®åãçšæãããŠããŸããåºæ¬çãªç®è¡æŒç®åã«ã¯ã+
ïŒå ç®ïŒã-
ïŒæžç®ïŒã*
ïŒä¹ç®ïŒã/
ïŒé€ç®ïŒãªã©ããããŸãããŸããæ¯èŒæŒç®åãšããŠã¯ã==
ïŒçããïŒã!=
ïŒçãããªãïŒã<
ïŒããå°ããïŒã>
ïŒãã倧ããïŒãªã©ããããŸãããããã®æŒç®åã䜿çšããŠãæ§ã
ãªèšç®ãæ¡ä»¶å€å®ãè¡ãããšãã§ããŸãã
3. Swiftã®å¶åŸ¡æ§é
ããã°ã©ãã³ã°ã«ãããå¶åŸ¡æ§é ã¯ãããã°ã©ã ã®æµãã管çããããã®éèŠãªèŠçŽ ã§ããSwiftãäŸå€ã§ã¯ãªããå€ãã®å¶åŸ¡æ§é ãæäŸããŠããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãSwiftã®äž»èŠãªå¶åŸ¡æ§é ã§ããæ¡ä»¶åå²ãšã«ãŒãã«ã€ããŠè©³ãã解説ããŸãã
3.1. æ¡ä»¶åå²ïŒifæãšswitchæ
ifæã¯ãç¹å®ã®æ¡ä»¶ãçã§ããå Žåã«ã®ã¿ã³ãŒããå®è¡ããããã®å¶åŸ¡æ§é ã§ããäŸãã°ãããæ°å€ã10ãã倧ãããã©ãããå€å®ããå Žåãifæã䜿çšããŠãã®æ¡ä»¶ããã¹ãã§ããŸããäžæ¹ãswitchæã¯ãå€ãã®æ¡ä»¶ãå¹ççã«ãã¹ãããããã®å¶åŸ¡æ§é ã§ããç¹å®ã®å€ã«å¿ããŠç°ãªãã³ãŒããå®è¡ããå Žåã«éåžžã«åœ¹ç«ã¡ãŸãã
3.2. ã«ãŒãïŒfor-inæãšwhileæ
ããã°ã©ã ã«ãããŠãåãã³ãŒããç¹°ãè¿ãå®è¡ããå¿
èŠãããå Žåãã«ãŒãã䜿çšããŸããSwiftã«ã¯ãfor-in
æãšwhile
æã®2ã€ã®äž»èŠãªã«ãŒãæ§é ããããŸããfor-in
æã¯ãã³ã¬ã¯ã·ã§ã³ã®åèŠçŽ ã«å¯ŸããŠã³ãŒããç¹°ãè¿ãå®è¡ããããã®ãã®ã§ãwhile
æã¯ãç¹å®ã®æ¡ä»¶ãçã§ããéãã³ãŒããç¹°ãè¿ãå®è¡ããããã®ãã®ã§ãã
4. Swiftã®é¢æ°ãšã¯ã©ã¹
Swiftã®é åã¯ããã®ã¢ãã³ãªæ©èœãšåŒ·åãªãªããžã§ã¯ãæåã®ç¹æ§ã«ãããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãSwiftã®é¢æ°ã®å®çŸ©ãšåŒã³åºãæ¹æ³ããããŠã¯ã©ã¹ã®åºæ¬ãç¶æ¿ã«ã€ããŠè©³ãã解説ããŸãã
4.1. é¢æ°ã®å®çŸ©ãšåŒã³åºã
é¢æ°ã¯ãç¹å®ã®ã¿ã¹ã¯ãå®è¡ããããã®ã³ãŒãã®ãŸãšãŸãã§ããSwiftã§ã¯ãfunc
ããŒã¯ãŒãã䜿çšããŠé¢æ°ãå®çŸ©ããŸããé¢æ°ã¯ããã©ã¡ãŒã¿ãåãããšãã§ããå¿
èŠã«å¿ããŠå€ãè¿ãããšãã§ããŸããé¢æ°ãåŒã³åºããšãã¯ãé¢æ°åã®åŸã«æ¬åŒ§ãã€ããŠãå¿
èŠãªãã©ã¡ãŒã¿ãæž¡ããŸããé¢æ°ã¯ã³ãŒãã®åå©çšæ§ãé«ããããã°ã©ã ã®æ§é ãæŽçããã®ã«åœ¹ç«ã¡ãŸãã
4.2. ã¯ã©ã¹ã®åºæ¬ãšç¶æ¿
ã¯ã©ã¹ã¯ããªããžã§ã¯ãæåããã°ã©ãã³ã°ã®äžå¿çãªæŠå¿µã§ãããSwiftãäŸå€ã§ã¯ãããŸãããã¯ã©ã¹ã¯ãé¢é£ããããŒã¿ãšã¡ãœããããŸãšããããã®èšèšå³ã®ãããªãã®ã§ããSwiftã®ã¯ã©ã¹ã¯ãããããã£ãšã¡ãœãããæã€ããšãã§ããŸãããŸããç¶æ¿ã䜿çšããŠãæ¢åã®ã¯ã©ã¹ããæ°ããã¯ã©ã¹ã掟çãããããšãã§ããŸããããã«ãããæ¢åã®ã³ãŒãã®åå©çšãæ¡åŒµã容æã«ãªããŸãã
5. Swiftã®ã³ã¬ã¯ã·ã§ã³
ããŒã¿ã®éåãå¹ççã«ç®¡çããããã«ãSwiftã¯ããã€ãã®åŒ·åãªã³ã¬ã¯ã·ã§ã³åãæäŸããŠããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãSwiftã®äž»èŠãªã³ã¬ã¯ã·ã§ã³ã§ããé åãšèŸæžã«ã€ããŠè©³ãã解説ããŸãã
5.1. é åãšãã®æäœ
é
åã¯ãåãåã®è€æ°ã®èŠçŽ ãé åºä»ããŠæ ŒçŽããããã®ã³ã¬ã¯ã·ã§ã³ã§ããSwiftã®é
åã¯ãé åºãä¿æãããåãå€ãè€æ°åå«ãããšãã§ããŸããé
åã®èŠçŽ ã«ã¯ãã€ã³ããã¯ã¹ã䜿çšããŠã¢ã¯ã»ã¹ããŸãããŸããé
åã«ã¯èŠçŽ ã®è¿œå ãåé€ãæ€çŽ¢ãªã©ã®å€ãã®æäœããµããŒããããŠããŸããäŸãã°ãappend()
ã¡ãœããã䜿çšããŠèŠçŽ ãè¿œå ããããremove(at:)
ã¡ãœããã䜿çšããŠç¹å®ã®äœçœ®ã®èŠçŽ ãåé€ããããšãã§ããŸãã
5.2. èŸæžãšãã®ç¹æ§
èŸæžã¯ãããŒãšå€ã®ãã¢ãæ ŒçŽããããã®ã³ã¬ã¯ã·ã§ã³ã§ããåããŒã¯äžæã§ãããããã«é¢é£ä»ããããå€ãæã¡ãŸããSwiftã®èŸæžã¯ãé«éãªããŒã®æ€çŽ¢ãšå€ã®ååŸããµããŒãããŠããŸããèŸæžã®èŠçŽ ã«ã¯ãããŒã䜿çšããŠã¢ã¯ã»ã¹ããŸãããŸããæ°ããããŒãšå€ã®ãã¢ãè¿œå ããããæ¢åã®ããŒã®å€ãæŽæ°ãããããããšãã§ããŸããèŸæžã¯ãæ å ±ãããŒãšå€ã®ãã¢ãšããŠå¹ççã«ç®¡çããå¿ èŠãããå Žåã«éåžžã«åœ¹ç«ã¡ãŸãã
6. Swiftã®é«åºŠãªãããã¯
Swiftã¯ãåå¿è ããäžçŽè ãŸã§ã®éçºè ãå¹ççã«ã³ãŒããæžãããã®å€ãã®é«åºŠãªæ©èœãæäŸããŠããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãSwiftã®é«åºŠãªãããã¯ã®äžã§ãç¹ã«éèŠãªããªãã·ã§ãã«ããšãããããã£ãšåæåãã«ã€ããŠè©³ãã解説ããŸãã
6.1. ãªãã·ã§ãã«ãšnilã®åãæ±ã
ãªãã·ã§ãã«ã¯ãSwiftã®ç¹åŸŽçãªæ©èœã®äžã€ã§ãããå€ãååšããããååšããªãïŒnilïŒãã®2ã€ã®ç¶æ
ãæã€ããšãã§ããåã§ãããªãã·ã§ãã«ã䜿çšããããšã§ãå€ãååšããªãå Žåã®ãšã©ãŒãå¹æçã«åãæ±ãããšãã§ããŸããSwiftã§ã¯ããªãã·ã§ãã«ã®å€ãå®å
šã«åãåºãããã®æ§ã
ãªæ¹æ³ãäŸãã°if let
ãguard let
ãæäŸãããŠããŸãã
6.2. ããããã£ãšåæå
Swiftã®ã¯ã©ã¹ãæ§é äœã«ã¯ãããããã£ãšããå€æ°ãå®æ°ãæã€ããšãã§ããŸãããããã®ããããã£ã¯ããªããžã§ã¯ãã®ç¹æ§ãç¶æ
ãè¡šãããã«äœ¿çšãããŸãããŸãããªããžã§ã¯ããäœæããããšãã«ããããã®ããããã£ã«åæå€ãèšå®ããããã®åæå
ã¡ãœãããå¿
èŠã§ããSwiftã§ã¯ãããããã£ã®åæåãç°¡åãã€å®å
šã«è¡ãããã®æ§ã
ãªæ©èœãæäŸãããŠããŸãã
7. Swiftã§ã®ã¢ããªéçº
Swiftã¯ãiOSãmacOSãwatchOSãtvOSãªã©ã®Appleã®ãã©ãããã©ãŒã åãã®ã¢ããªã±ãŒã·ã§ã³éçºã«åºã䜿çšãããŠããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãSwiftã䜿çšããã¢ããªéçºã®åºæ¬ãšãå ·äœçãªã¢ããªã®äŸã«ã€ããŠè©³ãã解説ããŸãã
7.1. UIKitã®åºæ¬
UIKitã¯ãiOSããã³tvOSã¢ããªã±ãŒã·ã§ã³ã®ãŠãŒã¶ã€ã³ã¿ãŒãã§ãŒã¹ãæ§ç¯ããããã®ãã¬ãŒã ã¯ãŒã¯ã§ããUIKitã«ã¯ããã¿ã³ãã©ãã«ãããŒãã«ãã¥ãŒãªã©ã®å€ãã®UIã³ã³ããŒãã³ããå«ãŸããŠããŸãããããã®ã³ã³ããŒãã³ãã䜿çšããŠããŠãŒã¶ã«é åçã§çŽæçãªã€ã³ã¿ãŒãã§ãŒã¹ãæäŸããã¢ããªã±ãŒã·ã§ã³ãäœæããããšãã§ããŸãããŸããUIKitã¯ãã¿ããã€ãã³ãã®åŠçãã¢ãã¡ãŒã·ã§ã³ã®ãµããŒããªã©ãã¢ãã€ã«ã¢ããªã±ãŒã·ã§ã³éçºã«å¿ èŠãªå€ãã®æ©èœãæäŸããŠããŸãã
7.2. Swiftã䜿çšããã¢ããªã®äŸ
Swiftã䜿çšããŠéçºãããã¢ããªã±ãŒã·ã§ã³ã¯æ°å€ããããŸããäŸãšããŠãã¡ã¢ã¢ããªã倩æ°äºå ±ã¢ããªãã«ã¬ã³ããŒã¢ããªãªã©ãæããããŸãããããã®ã¢ããªã±ãŒã·ã§ã³ã¯ãSwiftã®åŒ·åãªæ©èœã掻çšããŠããŠãŒã¶ã«é«åºŠãªæ©èœãšåªãããŠãŒã¶äœéšãæäŸããŠããŸããç¹ã«ãSwiftã®å®å šæ§ãšããã©ãŒãã³ã¹ã®é«ãã¯ãã¢ããªã±ãŒã·ã§ã³ã®å質ãåäžãããäžã§å€§ããªåœ¹å²ãæãããŠããŸãã
8. ãŸãšã
ãã®èšäºãéããŠãSwiftã®åºæ¬ããé«åºŠãªãããã¯ãŸã§å¹ åºãåŠã¶ããšãã§ããŸãããSwiftã¯çŸä»£ã®ã¢ããªã±ãŒã·ã§ã³éçºã«ãããŠéåžžã«éèŠãªåœ¹å²ãæãããŠããããã®åŠç¿ã¯ä»åŸã®ããã°ã©ãã³ã°ãã£ãªã¢ã«ãããŠå€§ããªã¢ããã³ããŒãžãšãªãã§ãããã
8.1. Swiftã®åŒ·ã¿ãšä»åŸã®å±æ
Swiftã®åŒ·ã¿ãšããŠã¯ãé«éãªå®è¡é床ãå®å šæ§ããããŠçŽæçãªææ³ãæããããŸããããã«ãããéçºè ã¯å¹ççã«ã³ãŒããæžãããšãã§ãããã°ã®å°ãªãã¢ããªã±ãŒã·ã§ã³ãäœæããããšãå¯èœãšãªããŸãããŸããAppleãããã¯ã¢ããããŠãããããiOSãmacOSãªã©ã®Appleã®ãã©ãããã©ãŒã ã§ã®éçºã«ãããŠãSwiftã¯ä»åŸãäžå¿çãªåœ¹å²ãæããããšãæåŸ ãããŸãã
8.2. SwiftåŠç¿ã®ããã®ãªãœãŒã¹
SwiftãåŠã¶ããã®ãªãœãŒã¹ã¯æ°å€ãååšããŸããå ¬åŒã®Swift.orgãAppleã®Developer Documentationã¯ãåå¿è ããäžçŽè ãŸã§ã®åŠç¿è ã«ãšã£ãŠéåžžã«æçšãªæ å ±ãæäŸããŠããŸãããŸãããªã³ã©ã€ã³ã®ãã¥ãŒããªã¢ã«ãæžç±ãã³ãã¥ããã£ãSwiftã®åŠç¿ããµããŒãããŠããŸããç¹ã«ãå®éã®ãããžã§ã¯ããæãããªããåŠã¶ããšã§ãããæ·±ãç解ãšå®è·µçãªã¹ãã«ã身ã«ã€ããããšãã§ããŸãã
åèæç®
-
The Swift Handbook â Learn Swift for Beginners - freeCodeCamp.org
ãã®èšäºã¯ãSwiftã®ããã°ã©ãã³ã°èšèªã«ã€ããŠã®çŽ¹ä»ãããŠããŸããAppleã2014幎ã«äœæããSwiftã¯ãAppleã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã å šäœã§åäœããå ¬åŒã®èšèªã§ãã
-
The Definitive Swift Tutorial for Beginners (2021) : CodeWithChris
ãã®ãã¥ãŒããªã¢ã«ã¯ãSwiftããã°ã©ãã³ã°ãåŠã³ããåå¿è åãã«äœæãããŠããŸããèšäºã«ã¯ãSwiftã®åºæ¬çãªææ³ãæ§æã«é¢ããæ å ±ãå«ãŸããŠãããåå¿è ãSwiftãåŠã¶éã®åèè³æãšããŠåœ¹ç«ã¡ãŸãã
-
The Basics - Swift.org - Documentation
ãã®ããã¥ã¡ã³ãã¯ãSwiftã®å ¬åŒããã¥ã¡ã³ãã®äžéšã§ãããSwiftã®åºæ¬ã«é¢ããæ å ±ãæäŸããŠããŸãã