Mais conteúdo relacionado Semelhante a Swift事情2014夏 ~ Swift入門 beta6対応 (20) Mais de Tomoki Hasegawa (20) Swift事情2014夏 ~ Swift入門 beta6対応4. デジタルサーカス(株)
・ Webサイト受託開発(PHP, CakePHP, Drupal)
・ スマホアプリ受託開発(iOS, Android)
・ ⾃自社サービス (http://appbuilder.jp)
2000 Samui 2001 Cebu 2002 Phuket 2004 Ishigaki 2005 Vietnam 2006 Hǎinán Dǎo
2007 Bali 2008 Taiwan 2010 Seoul 2011 Macau 2012 Guam 2013 Phuket
14. 変わること
・ No .h, .m . Only .swift
・ ファイルを⾏行行き来しなくて良良いので地味にラク
・ Interface Builderから作成するIBOutlet, IBActionも .swift に書く
・ ⾔言語仕様による変化
・ 構造体のメンバへ直接アクセス可能
・ 変数 … Immutable / Mutable / 型推測
・ クラス定義 / メソッド定義
・ nilを意識識したコーディング
・ Dictionary, Arrayの取り扱い
17. 変わらないこと
・ Xcode Project, StoryBoard, Interface Builder
・ Provisioning Profiles
・ COCOAPODS含めObjective-Cベースのライブラリ混在可
・ Cocoa Touch (Foundation Framework, UIKit, … )
19. Controller class definition
// Super Class
@interface MasterViewController : UITableViewController {}
// Protocol(s)
@interface LoveBeerListViewController : UIViewController <UITableViewDataSource,
UITableViewDelegate> {}
Objective-C
// Super Class
class MasterViewController: UITableViewController {}
// Protocol(s)
class LoveBeerListViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {}
Swift
・ スーパークラス指定は : の後に。
・ プロトコル定義はスーパークラス定義の後に , 区切切りで。
20. Super class’s method
・ Super classのmethodは override で定義。
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{}
override func viewDidLoad() {}
override func viewWillAppear(animated: Bool){}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {}
Objective-C
Swift
21. Protocol method
・ Swiftではパラメタは(変数名: 型)または(外部名 変数名: 型)。
・ UIKitのプロトコルメソッドは後者で定義されている。
・ Objective-Cへの配慮っぽい。(けどちょっと…)
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {}
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}
Objective-C
Swift
24. AnyObject and Type Casting
LBShopTableViewCell* cell = [tableView
dequeueReusableCellWithIdentifier:@"ShopCell" forIndexPath:indexPath];
let cell: LBShopTableViewCell =
tableView.dequeueReusableCellWithIdentifier("ShopCell", forIndexPath: indexPath)
as LBShopTableViewCell
Objective-C
Swift
・ dequeueReusableCellWithIdentifier:forIndexPath:の返値は
Objective-Cではid型。SwiftではAnyObject型。
・ Swiftではas [型]で明⽰示的にキャストする必要あり。
25. 4. Swift TIPS
1. Xcode上でのSwiftの取り扱い
2. Objective-CからSwiftになって変わること変わらないこと
3. Objective-Cでのいつもの書き⽅方、Swiftでは?
4. Swift TIPS
5. 現時点でのおすすめ資料料
29. Optional型
・ Objective-C の NSString には nil が⼊入る。
・ Swift の String には nil は⼊入らない。
・ nil を⼊入れたい場合は Optional<String> 型を使う。
・ String 型として使いたい場合 Optional<String> 型のままでは使えな
いので String 型を取り出して使う。
・ これを「アンラップ」と⾔言う。
(String 型が Optional<String> 型に「ラップされている」)
・ アンラップにはいくつかの⽅方法がある。
32. Optional型 - アンラップ
if や while の中でアンラップする⽅方法は Optional Binding
・ let でも var でもOK
・ unwrappedStr は String 型
・ optionalStr が nil の場合は if の条件式の評価が false になる
34. Optional型 - シンタックスシュガー
・ シンタックスシュガー
・ Optional<String> → String?
・ Implicitly Unwrapped Optional<String> → String!
・ アンラップに使う ? や ! とは別物
37. ARC
// self がクロージャを強参照し、クロージャが self を強参照して循環参照に陥る
self.aProperty = {
self.doSomething()
}
!
// unowned self としてキャプチャすると、クロージャは self を所有しない
self.aProperty = {
[unowned self] in
self.doSomething()
}
43. COCOAPODSの使い⽅方
・ SwiftからもCOCOAPODSは使える。
・ ふつうに Podfile を書いて pod install する。
・ プロジェクト名-Bridging-Header.h を作る。
・ プロジェクトにObjective-Cの新規ファイルを追加すると「Bridging
Headerを作る?」というダイアログが出るのでそれで作るのがラク。
//
// Use this file to import your target's public headers that you would like to
expose to Swift.
//
#import <AFNetworking/AFNetworking.h>
48. ALAMOFIRE
・ Alamofire is an HTTP networking library written in Swift.
Think of it as AFNetworking, reimagined for the
conventions of this new language.
!
!
!
・ Xcode beta6の仕様変更更で動かなくなってる…
Alamofire.request(.GET, "https://qiita.com/api/v1/tags.json").response {
(request, response, data, error) in
let json: [JSONValue] = JSONValue(data!).array
for tag in tags {
println(tag["name"].string)
}
}