Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
え!? Swift使ってるのにそんな書きかたしてるのですか!?
Search
Yusei Nishiyama
November 25, 2014
Technology
28
8.9k
え!? Swift使ってるのにそんな書きかたしてるのですか!?
iOSアプリをSwiftらしいコードで記述するために
Yusei Nishiyama
November 25, 2014
Tweet
Share
More Decks by Yusei Nishiyama
See All by Yusei Nishiyama
Continuous Mobile App Delivery
yuseinishiyama
0
630
Working at Cookpad UK
yuseinishiyama
2
3.4k
Building iOS apps at scale (Mobilization)
yuseinishiyama
4
1.7k
Working at Scale
yuseinishiyama
3
750
Building iOS apps at scale
yuseinishiyama
2
1.2k
Reduce Build Times and Get Home Eariler
yuseinishiyama
2
700
How to make your app international
yuseinishiyama
3
7.4k
Safer Networking Layer With Swift
yuseinishiyama
0
260
Making Your App Static
yuseinishiyama
13
4k
Other Decks in Technology
See All in Technology
自己改善からチームを動かす! 「セルフエンジニアリングマネージャー」のすゝめ
shoota
6
770
反実仮想機械学習とは何か
usaito
PRO
11
4.7k
プロンプトエンジニアリングでがんばらない-Agentic Workflow へ-近藤憲児
kenjikondobai
3
840
地理空間データ可視化・解析・活用ソリューション Pacific Spatial Solutions (PSS)
pacificspatialsolutions
0
290
リテール金融(キャッシュレス・ネット銀行・ネット証券)の競争環境と経済圏
8maki
0
1.2k
いつか使うかも貯金してたらめちゃめちゃ機能が増えてた話
riyaamemiya
0
280
Gitlab本から学んだこと - そーだいなるプレイバック / gitlab-book
soudai
4
440
Azure犬駆動開発の記録/GlobalAzureFukuoka2024_20240420
nina01
1
220
チームでロジカルシンキングに改めて向き合っている話 〜学習環境と実践⽅法〜
sansantech
PRO
3
2.6k
開発生産性大幅アップ!Postman VS Code拡張機能
nagix
2
380
Kernel MemoryでAzure OpenAI Serviceとお手軽データソース連携
mitsuzono
1
260
Terraformあれやこれ/terraform-this-and-that
emiki
8
1.4k
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
243
12k
Build The Right Thing And Hit Your Dates
maggiecrowley
24
2k
A Tale of Four Properties
chriscoyier
151
22k
Clear Off the Table
cherdarchuk
84
310k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
274
13k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
The Straight Up "How To Draw Better" Workshop
denniskardys
227
130k
Stop Working from a Prison Cell
hatefulcrawdad
266
19k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
17
1.4k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
21
1.6k
Web development in the modern age
philhawksworth
202
10k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
322
20k
Transcript
͑!? SwiftͬͯΔͷʹͦΜͳ ॻ͖͔ͨͯ͠ΔͷͰ͔͢!? potatotips#11 @yuseinishiyama
Who am I? • Mobile app engineer at Cookpad Inc.
• Develop apps for iOS • Regulate “Auto Layout” day after day… :’-( • I’m interested in • Metal (new graphics API of Apple) • Functional programming (with Swift) • Reactive programming (Reactive Cocoa)
In Objective-C era… @interface SomeClass : NSObject @property (nonatomic, strong)
NSString *someString; @end ! @implementation SomeClass ! - (void)setSomeString:(NSString *)someString { _someString = someString; [self doSomething]; } ! - (void)doSomething { NSLog(@"I don't feel like doing anything..."); } ! @end ؆୯ʹೖ࣌ʹॲཧΛߦ͏͜ͱ͕Ͱ͖ͨ
With Swift class SomeClass { private var backingStore = ""
var value: String { get { return backingStore } set { backingStore = newValue doSomething() } } func doSomething() { println("I don't feel like doing anything.") } } 85'
Keep calm class SomeClass { var someString: String = ""
{ didSet { doSomething() } } func doSomething() { println("Now I’m sleeping. Please don’t disturb.") } } XJMM4FUEJE4FUΛ͑ಉ͜͡ͱ͕࣮ݱՄೳ
Coalesce fallback class Person { var name: String! } !
var quietPerson = Person() quietPerson.name = "Tom" ! var hisName: String! ! if quietPerson.name != nil { hisName = quietPerson.name } else { hisName = "I can't recognize what he says" }
Use `??` class Person { var name: String! } !
var quietPerson = Person() quietPerson.name = "Tom" ! var hisName = quietPerson.name ?? "I can't recognize what he says"
(Annoying) weakSelf pattern class SomeClass { var ivar: Int? func
methodA(function: ()->()) { function() } func methodB() { weak var weakSelf = self methodA { () -> () in weakSelf!.ivar = 3 } } }
use `[unowned self]` class SomeClass { var ivar: Int? func
methodA(function: ()->()) { function() } func methodB() { methodA { [unowned self] () -> () in self.ivar = 3 } } }
Static variables in function - (BOOL)toggleSwitch { static BOOL aSwitch
= true; aSwitch = !aSwitch; return aSwitch; }
4XJGUߏମͰ͔͠TUBUJDมΛαϙʔ τ͍ͯ͠ͳ͍ʜ
Use struct func toggleSwitch() -> Bool { struct Switch {
static var aSwitch = true } Switch.aSwitch = !Switch.aSwitch return Switch.aSwitch }
Cast var anyObject: AnyObject? = "WE ARE UPPERCASE" if anyObject
is String { let string = anyObject as String println(string.lowercaseString + "?") } ʁ
You can check, cast and bind in one line !
if let string = anyObject as? String { println(string.lowercaseString + "?") }
Trailing closure func methodX(i: Int, function: ()->(String)) { function() }
! methodX(1, { () -> (String) in return "Do nothing" } ) ! methodX(1) { () -> (String) in return "Do nothing" }
Swift array supports map, filter and reduce class Vehicle {
var numberOfTires: Int = 0 } class Car: Vehicle { override init() { super.init() numberOfTires = 4 } } class Bicycle: Vehicle { override init() { super.init() numberOfTires = 2 } } class Unicycle: Vehicle { override init() { super.init() numberOfTires = 1 } } let vehicles = [Car(), Car(), Unicycle(), Bicycle(), Car(), Unicycle()] var numberOfAllTires = vehicles.map{ $0.numberOfTires }.reduce(0, +)
Nested function func setUpViews() { func setupBarButton() { // set
up bar buttons } func setupRefreshControl() { // set up refresh control } setupBarButton() setupRefreshControl() } είʔϓΛอͬͨ··ɺॲཧΛߏԽ͢Δ͜ͱ͕Ͱ͖Δ
Enum with method enum MyTableViewSection: Int { case A =
0, B, C, D func heightForCell() -> CGFloat { switch self { case A: return 30 case B: return 44 case C: return 80 case D: return 44 } } } ! func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return MyTableViewSection(rawValue: indexPath.section)!.heightForCell() } ؔ࿈͢ΔॲཧΛ&OVNଆʹҠৡͨ͠΄͏͕Մಡੑ͕ߴ͍
Any Questions?