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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Yusei Nishiyama
November 25, 2014
Technology
9.3k
28
Share
え!? Swift使ってるのにそんな書きかたしてるのですか!?
iOSアプリをSwiftらしいコードで記述するために
Yusei Nishiyama
November 25, 2014
More Decks by Yusei Nishiyama
See All by Yusei Nishiyama
Continuous Mobile App Delivery
yuseinishiyama
0
960
Working at Cookpad UK
yuseinishiyama
1
3.7k
Building iOS apps at scale (Mobilization)
yuseinishiyama
4
2k
Working at Scale
yuseinishiyama
3
890
Building iOS apps at scale
yuseinishiyama
2
1.7k
Reduce Build Times and Get Home Eariler
yuseinishiyama
2
870
How to make your app international
yuseinishiyama
3
7.8k
Safer Networking Layer With Swift
yuseinishiyama
0
350
Making Your App Static
yuseinishiyama
13
4.4k
Other Decks in Technology
See All in Technology
音声言語モデル手法に関する発表の紹介
kzinmr
0
150
AIでAIをテストする - 音声AIエージェントの品質保証戦略
morix1500
1
150
M5Stack CoreS3とZephyr(RTOS)で Edge AIっぽいことしてみた
iotengineer22
0
390
GKE Agent SandboxでAIが生成したコードを 安全に実行してみた
lamaglama39
0
140
はじめての MagicPod生成AI機能 機能紹介から活用方法まで
magicpod
0
120
揺るがないAIを開発するためのアノテーション設計
sansantech
PRO
1
100
AI バイブコーティングでキーボード不要?!
samakada
0
660
ハーネスエンジニアリングの概要と設計思想
sergicalsix
9
6.4k
AzureのIaC管理からログ調査まで、随所に役立つSkillsとCustom-Instructions / Boosting IaC and Log Analysis with Skills
aeonpeople
0
340
UIライブラリに依存しすぎないReact Native設計を目指して
grandbig
0
160
色を視る
yuzneri
0
280
目的ファーストのハーネス設計 ~ハーネスの変更容易性を高めるための優先順位~
gotalab555
9
3.4k
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
350
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.4k
Designing for humans not robots
tammielis
254
26k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
110
Marketing to machines
jonoalderson
1
5.2k
KATA
mclloyd
PRO
35
15k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
Designing Powerful Visuals for Engaging Learning
tmiket
1
350
First, design no harm
axbom
PRO
2
1.2k
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?