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
9.1k
え!? 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
750
Working at Cookpad UK
yuseinishiyama
2
3.5k
Building iOS apps at scale (Mobilization)
yuseinishiyama
4
1.8k
Working at Scale
yuseinishiyama
3
820
Building iOS apps at scale
yuseinishiyama
2
1.4k
Reduce Build Times and Get Home Eariler
yuseinishiyama
2
780
How to make your app international
yuseinishiyama
3
7.5k
Safer Networking Layer With Swift
yuseinishiyama
0
290
Making Your App Static
yuseinishiyama
13
4.2k
Other Decks in Technology
See All in Technology
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
120
LINEヤフーのフロントエンド組織・体制の紹介【24年12月】
lycorp_recruit_jp
0
530
Password-less Journey - パスキーへの移行を見据えたユーザーの準備 @ AXIES 2024
ritou
3
1.4k
宇宙ベンチャーにおける最近の情シス取り組みについて
axelmizu
0
110
Oracle Cloudの生成AIサービスって実際どこまで使えるの? エンジニア目線で試してみた
minorun365
PRO
4
270
新機能VPCリソースエンドポイント機能検証から得られた考察
duelist2020jp
0
210
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
300
開発生産性向上! 育成を「改善」と捉えるエンジニア育成戦略
shoota
1
230
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
360
成果を出しながら成長する、アウトプット駆動のキャッチアップ術 / Output-driven catch-up techniques to grow while producing results
aiandrox
0
180
alecthomas/kong はいいぞ / kamakura.go#7
fujiwara3
1
300
C++26 エラー性動作
faithandbrave
2
640
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Statistics for Hackers
jakevdp
796
220k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
160
A Modern Web Designer's Workflow
chriscoyier
693
190k
Embracing the Ebb and Flow
colly
84
4.5k
Site-Speed That Sticks
csswizardry
2
190
Agile that works and the tools we love
rasmusluckow
328
21k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
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?