SlideShare a Scribd company logo
1 of 50
Download to read offline
Objective-CからSwiftへの移行講座
yuki ono
とりあえず Swiftとは
“Objective-C without the C” C(言語)無しのObjective-C
つまり、「全く新しい言語だよ」ってこと
“iOS” “OS X” 両方いける
Swiftとは
• Fast

• 速い

• Modern

• 現代的な

• Safe

• 安全な

• Interactive

• 双方向 対話型
(Fast) どんくらいはやいのか
プレゼンによると
Swift
Objective-C
Python
0 1 2 3 4
1
2.8
3.9
RC4 暗号化処理の速さは…
Swift
Objective-C
Python
0 100 200 300
1
127
220
(Modern) 新しいよ
↓ けど
ObjCで出来たことは全てできる 安心しろ
(Safe) 安全だよ → メモリ自動管理
(Interactive) インタラクティブ性
↓
直ぐ実行、可視化できる 手軽だね
突然ですが問題です
どちらがObjective-Cでしょうか
• @“TeenCodersさいこー”
• “TeenCodersさいてー”
どちらがObjective-Cでしょうか
• @“TeenCodersさいこー”
• “TeenCodersさいてー”
Objective-C
Swift
@が要らない
とある行の書き抜き。間違いは?
NSString *s = @“あはは”
Objective-C
とある行の書き抜き。間違いは?
NSString *s = @“あはは”;
Objective-C
;(セミコロン)
→Swiftではいらない!
あってもOK
ここから普通に解説
まず重要なのが
•let (定数)
•var (変数)
letとvar
NSString *s = @“さい”;
var s = “さい” let s = “さい”
s = “おひょひょ” s = “おひょひょ”
letとvar
配列(Array)の時
NSArray *a = @[@“きん”, @“どう”, @“?”];
let a = [“きん”, “どう”, “?”]
NSMutableArray *a = @[@“きん”, @“どう”, @“?”];
var a = [“きん”, “どう”, “?”]
ここまで書いたSwiftのコードを復習します
var s = “さい”
var a = [“きん”, “どう”, “?”]
なにか気づいた人
そう。
「文字列」と「配列」なのに、
その違いを表す記述がどこにもない
var s = “さい”
var a = [“きん”, “どう”, “?”]
つまり型推論 → 簡単
じゃあ逆に、
小数型(Double, Float)作りたいのに
整数型(Int)になっちゃうときどうすんの
型アノテーション
var num = 1
var num:Double = 1
double num = 1; (ObjC)
型アノテーション
こんなかんじで何でも出来る
var v:UIView
var v:Int
var v:Float
var v:UIImageView
しかーし
配列(Array)の時はちょっと違う!
型アノテーション 配列
文字の配列を作りたい時
var arr:Array<String> = [“あ”,”い”]
var arr:[String] = [“あ”,”い”]
じゃあちょうど配列の話してるから
配列について
Swiftの配列
なんてったって、
Int型とかも配列に出来る!
ほかはだいたいObjCと一緒
たぶん
Swiftの配列
• 基本
• var arr = [“は”,”に”]
• 空の配列を作る場合
• var arr:[String] = []
• var arr:Array<String> = []
• var arr = [String]()
• var arr = Array<String>()
• 全部同じ値の配列を作る
• var arr = [String](count: 3, repeatedValue: “ABC” ) → [“ABC”,”ABC”,”ABC”]
Swiftの配列
• 要素数
• arr.count →要素数Int
• arr.isEmpty → 空かどうか → Bool
• 要素追加
• arr.append(“ほ”) → 後ろに追加
• arr += [“ほ”] → 後ろに追加
• arr.insert(“A”, atIndex: 1) → 2番目に追加
• 要素削除
• arr.removeAll() → 全削除
• arr.removeAtIndex(2) → 3番目を削除
• arr.removeAtLast() → 最後を削除
Swiftの配列
• var arr = [“は”,”に”,”ほ”,”へ”]
• 取り出し
• arr[0] → は
• Array( arr[1…2] ) → [“に”, “ほ”]
• 置換
• arr[0] = “A” → [“A”,”に”,”ほ”,”へ”]
• arr[0…1] = [“あ”,”い”,”う”] → [“あ”,”い”,”う”,”ほ”,”へ”]
Swiftの連想配列
• var dic = [“電話”:”080”, 名前:”タナカ”]
• あとはArrayと大体一緒。自分で調べて
Swiftの面白いところ!
変数名がなんでもいい!
えもじでも日本語でも!
if とか for とか switchを見よう
制御構造 if
if (a==b) {
NSLog(“OKOKOKOKOK”)
}
if a==b {
println(“OKOKOKOKOK”)
}
制御構造 switch
var i = 20
switch i {
case 0: /*処理*/
case 1…5: /*範囲指定も可*/
case 2, 4, 9: /*カンマ区切りで複数指定も可*/
case 10: /*処理*/
fallthrough
default: break /*何も処理を書かない行はbreakを記述*/
}
変更点
・()がいらない
・breakがいらない
 (次の処理もしたい場合はfallthrough)
・default必須
制御構造 for
()がいらなくなった
制御構造 for-in
for a in 1…5 {
//aが1から5まで
}
for a in 1..<5 {
//aが1から5まで
}
制御構造 while
()がなくなっただけ
ねむい
メソッド
-(void)methodDayo{
//処理
}
func methodDayo() {
//処理
}
メソッド 引数
-(void)methodDayo(NSString*)str string2:(NSString*)str2{
//処理
}
func methodDayo(str:String, str2:String) {
//処理
}
メソッド 返り値
-(int)methodDayo(NSString*)str{
return 2;
}
func methodDayo(str:String) -> Int {
return 2
}
UIKit を使う
var label = UILabel()
label.text = “ABC”
label.textColor = UIColor.yellowColor()
self.addSubview(label)
UILabel *label = [UILabel new];
label.text = “ABC”;
label.textColor = [UIColor yellowColor];
[self addSubview:label];
もうこれで
薄く広くおしえたから
あとは自分でがんばって
おしまい
質問はいくらでもどんどんおいで
Twitter @yuki0n0
FB www.facebook.com/yuki0n0

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Objective-CからSwiftへの移行講座