Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Swift on the ObjC #shibuyaswift
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
ainame
June 08, 2016
Programming
940
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swift on the ObjC #shibuyaswift
ainame
June 08, 2016
More Decks by ainame
See All by ainame
Swift 5.7で変わる正規表現を試してみよう
ainame
4
8k
iOSDC 2021 - App Store用スクリーンショットの自動生成をアラビア語対応してSwiftUIで実装してみた
ainame
0
6.1k
DNN/GPU with Ruby #rubykaigi
ainame
2
3.3k
Server Side Swift実用性評価 2017 #iosdc #b
ainame
3
4.5k
Process tons of jobs with Swift
ainame
0
1.9k
家族アルバムみてね 開発風景 #realm_jp
ainame
4
4.2k
iOSで無限バックグラウンドアップロード(に挑戦してみた話)
ainame
2
5.4k
リファクタリングとtsort
ainame
1
1.8k
RubyMotionについて #mixiwwdc
ainame
2
470
Other Decks in Programming
See All in Programming
우리의 Nitro Modules는 당신의 Turbo Modules보다 아름답다
l2hyunwoo
0
190
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
1
1.8k
contenteditable と日本語入力に向き合う
colorful12
0
210
AI時代のTerminal環境を UNIX哲学で設計・運用する
yasunori0418
1
210
Seven New Principles of Software Engineering
yegor256
0
250
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
200
Groovy as a human and AI friendly programming language
paulk
0
110
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
380
Findy - エンジニア向け会社紹介/Findy Company Deck
findyinc
6
400k
golang-bob.pdf
iwatsukayura
0
140
ActionController meets Protocol Buffers
sorah
0
680
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
0
150
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
Everyday Curiosity
cassininazir
0
360
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
330
Mobile First: as difficult as doing things right
swwweet
225
10k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
RailsConf 2023
tenderlove
30
1.6k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Leo the Paperboy
mayatellez
10
2.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
22k
Transcript
Swift on the ObjC Shibuya.swift#4 • @ainame (Satoshi NAMAI) •
株式会社ミクシィ • 家族アルバムみてね作ってます
Question あなたのプロジェクトにはまだObjective-Cのコードがありますか?
Contents みてねチームにおいて、昨年10月頃からObjCで書かれたコードベースの上にSwift を書いてきた際に取り組んできたことを話します。
Policy ユーザーへの価値提供が第一 • 全コードを一気にSwiftに置き換えることはしない • 新規に書く箇所はSwift • 大きく仕様変更が入る際には画面単位でSwiftに置き換えつつ レガシーな設計から良い設計に合わせていく
Study Swift by WWDC videos 毎週水曜にランチタイムにみんなで動画を見てSwift2について学んだ • What's New in
Swift • Protocol-Oriented Programming in Swift • Building Better Apps with Value Types in Swift • Swift and Objective-C Interoperability see: 動画で学ぶモダンなiOS/Androidアプリ開発技術 - ainameの日記 井戸端iOS飯で過去に見た動画まとめ
Get started! • Bridging HeaderでObjCのコードをimportしてSwiftを書き始める • SwiftのStructやEnumはObjC側では使えないのでほとんど使わず、NSObject を継承 or @objcでClassを書いてく
• ObjCの巨大なViewControllerに機能追加する際には同じファイル名で extensionとして実装(⌥ + ↑ で切り替え可能) ◦ SomeViewController.{h,m,swift}という感じで3ファイルが存在している状態
Xcode’s auto-completion •Swift上でObjcのクラスの補完が効かなくなった Clean、Derived Data削除も効果なし importの仕方を修正したら復活 ✖ #import <GAI.h> ◯
#import <GoogleAnalytics/GAI.h> see: http://stackoverflow.com/a/35431971
Swifty? • さらに動画でSwiftっぽい書き方を学ぶ • "Feet in Both Worlds: From Objective-C
to Swift" by Andy Matuschak • Let's Play: Refactor the Mega Controller!
Example // enumを使ってUIの機能追加したい enum Icon { case Color(UIColor) case Image(UIImage)
} class IconView: UIView { init(icon: Icon) { … } } // ObjC用の箱を用意しておく class Box<T> { let value: T init(_ value: T) { self.value = value } } // ObjCのクラスには箱だけを id型としておいておく @interface LegacyViewController @property (readwrite, strong) id /* Box<SomeEnum> */ _icon; @end // 箱から取り出し用の propertyを宣言して extension LegacyViewController { var icon: Icon { get { return (_icon as! Box<Icon>).value } set { _icon = Box(newValue) } } // すっきりした実装ができる func showProfileWithIcon(icon: Icon) } どうしてもSwiftでしか使えないやつをObjCのViewControllerに もたせたい場合はBoxパターンを利用
Nullability & Lightweight Generics • SwiftでObjCのメソッドを呼ぶ際にNullabilityを考慮してなくて事故る • Bridging Headerでimportされているものには必ずNullability +
Lightweight Genericsを指定するルールに
Nullability & Lightweight Generics - Pitfall SwiftからObjCに公開するメソッドはnilが入り込みやすい (Swift側で参照すると落ちる) @objc func
doSomething(string: String) ← 普通のSwiftのメソッ ド---------------------------------------------------------- NSString *str = [self textField.text]; ← TextFieldからnullableなNSStringをとる [obj doSomething:str]; ← Swiftのメソッドにnullableなやつ渡す❌
Carthage & Dynamic Framework • SwiftベースのライブラリはDynamic Frameworkとして取り込んでいる • Cocoapodsはuse_frameworks!できなかった時ように残してある (当時GoogleAnalytics
SDKとか出来なかったけど今は対応している?) • carthage bootstrapは初回こそ時間かかるものの一度ビルドすると再ビルドせ ず済むので良い • CircleCI上でRxSwiftのビルド終わらない問題 ◦ CI上でビルドせず最初から s3にビルド済みキャッシュを自前で置く戦略で・・・ ◦ aws s3 sync s3://BUCKET/`md5 Cartfile.resolved | awk '{ print $4; }'` Carthage/Build/iOS
Introduce swifty libraries 置き換え系 • SDWebImageから拝借したdispatch_sync_main_safe -> duemunk/Async • DateTools
-> malcommac/SwiftDate • PromiseKit & KVOController -> ReactiveX/RxSwift 新規導入系 • antitypical/Result, lyft/mapper, ra1028/Former
NSCoding & Unit testing シリアライズ処理は辛い(怖い) • @objc(CLASS NAME)を利用してSwift化 • Module名が異なるとクラス名が一緒でもforce
downcastでクラッシュ 1. テスト対象クラスをTestのTargetにも追加 2. TestのtargetのModule名をアプリのTargetのModule名にして回避 • アプリのTargetをModule化して@testable importする方が一般的っぽい?
Example @interface MTNFoo: NSObject<NSCoding> @property(nonatomic) MTNBar *bar; .... @end @implementation
MTNFoo - (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [self init]; if (self) { _bar= [aDecoder ecodeObjectForKey:@"bar"]; } return self } @end @objc(MTNFoo) class MTNFoo: NSObjcet, NSCoding { var bar:MTNBar … required init?(coder aDecoder: NSCoder) { self.bar = aDecoder.decodeObjectForKey("bar") as! MTNBar } } MiteneTests -> Mitene Could not cast value of type MiteneTests.MTNBar' (0x13218604) to 'Mitene.MTNBar'
SwiftLint • SwiftLintをフォーマッター的に利用 • まだレビュー用途には利用できてない • チームメンバーがXcodePlugin書いてくれた ◦ ypresto/SwiftLintXcode: An
Xcode plug-in to format your code using SwiftLint.
Current Status まだまだこれから
まとめ • Swiftに慣れるのにみんなで動画を見るのはいいぞ • Nullability頑張って定義していこう • Xcodeの補完効かなくなると仕事出来なくなるので頑張って解決しよう • ObjCと混ぜたプロジェクトでテスト書くの大変っぽい?
Enjoy Swift! See you next week at WWDC!