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
730
Working at Cookpad UK
yuseinishiyama
2
3.5k
Building iOS apps at scale (Mobilization)
yuseinishiyama
4
1.8k
Working at Scale
yuseinishiyama
3
810
Building iOS apps at scale
yuseinishiyama
2
1.3k
Reduce Build Times and Get Home Eariler
yuseinishiyama
2
770
How to make your app international
yuseinishiyama
3
7.5k
Safer Networking Layer With Swift
yuseinishiyama
0
280
Making Your App Static
yuseinishiyama
13
4.2k
Other Decks in Technology
See All in Technology
[AWS JAPAN 生成AIハッカソン] Dialog の紹介
yoshimi0227
0
150
マネジメント視点でのre:Invent参加 ~もしCEOがre:Inventに行ったら~
kojiasai
0
460
IaC運用を楽にするためにCDK Pipelinesを導入したけど、思い通りにいかなかった話
smt7174
1
110
omakaseしないための.rubocop.yml のつくりかた / How to Build Your .rubocop.yml to Avoid Omakase #kaigionrails
linkers_tech
3
730
プロダクト成長に対応するプラットフォーム戦略:Authleteによる共通認証基盤の移行事例 / Building an authentication platform using Authlete and AWS
kakehashi
1
150
ネット広告に未来はあるか?「3rd Party Cookie廃止とPrivacy Sandboxの効果検証の裏側」 / third-party-cookie-privacy
cyberagentdevelopers
PRO
1
130
10分でわかるfreeeのQA
freee
1
3.4k
Nix入門パラダイム編
asa1984
2
200
Product Engineer Night #6プロダクトエンジニアを育む仕組み・施策
hacomono
PRO
1
470
Jr. Championsになって、強く連携しながらAWSをもっと使いたい!~AWSに対する期待と行動~
amixedcolor
0
190
AWS re:Inventを徹底的に楽しむためのTips / Tips for thoroughly enjoying AWS re:Invent
yuj1osm
1
560
使えそうで使われないCloudHSM
maikamibayashi
0
170
Featured
See All Featured
Embracing the Ebb and Flow
colly
84
4.4k
KATA
mclloyd
29
13k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Navigating Team Friction
lara
183
14k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
A designer walks into a library…
pauljervisheath
202
24k
Typedesign – Prime Four
hannesfritz
39
2.4k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
32
1.8k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Statistics for Hackers
jakevdp
796
220k
Designing for humans not robots
tammielis
249
25k
The Cost Of JavaScript in 2023
addyosmani
45
6.6k
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?