Slide 1

Slide 1 text

͑!? Swift࢖ͬͯΔͷʹͦΜͳ ॻ͖͔ͨͯ͠ΔͷͰ͔͢!? potatotips#11 @yuseinishiyama

Slide 2

Slide 2 text

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)

Slide 3

Slide 3 text

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 ؆୯ʹ୅ೖ࣌ʹॲཧΛߦ͏͜ͱ͕Ͱ͖ͨ

Slide 4

Slide 4 text

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'

Slide 5

Slide 5 text

Keep calm class SomeClass { var someString: String = "" { didSet { doSomething() } } func doSomething() { println("Now I’m sleeping. Please don’t disturb.") } } XJMM4FU΍EJE4FUΛ࢖͑͹ಉ͜͡ͱ͕࣮ݱՄೳ

Slide 6

Slide 6 text

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" }

Slide 7

Slide 7 text

Use `??` class Person { var name: String! } ! var quietPerson = Person() quietPerson.name = "Tom" ! var hisName = quietPerson.name ?? "I can't recognize what he says"

Slide 8

Slide 8 text

(Annoying) weakSelf pattern class SomeClass { var ivar: Int? func methodA(function: ()->()) { function() } func methodB() { weak var weakSelf = self methodA { () -> () in weakSelf!.ivar = 3 } } }

Slide 9

Slide 9 text

use `[unowned self]` class SomeClass { var ivar: Int? func methodA(function: ()->()) { function() } func methodB() { methodA { [unowned self] () -> () in self.ivar = 3 } } }

Slide 10

Slide 10 text

Static variables in function - (BOOL)toggleSwitch { static BOOL aSwitch = true; aSwitch = !aSwitch; return aSwitch; }

Slide 11

Slide 11 text

4XJGU͸ߏ଄ମͰ͔͠TUBUJDม਺Λαϙʔ τ͍ͯ͠ͳ͍ʜ

Slide 12

Slide 12 text

Use struct func toggleSwitch() -> Bool { struct Switch { static var aSwitch = true } Switch.aSwitch = !Switch.aSwitch return Switch.aSwitch }

Slide 13

Slide 13 text

Cast var anyObject: AnyObject? = "WE ARE UPPERCASE" if anyObject is String { let string = anyObject as String println(string.lowercaseString + "?") } ৑௕ʁ

Slide 14

Slide 14 text

You can check, cast and bind in one line ! if let string = anyObject as? String { println(string.lowercaseString + "?") }

Slide 15

Slide 15 text

Trailing closure func methodX(i: Int, function: ()->(String)) { function() } ! methodX(1, { () -> (String) in return "Do nothing" } ) ! methodX(1) { () -> (String) in return "Do nothing" }

Slide 16

Slide 16 text

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, +)

Slide 17

Slide 17 text

Nested function func setUpViews() { func setupBarButton() { // set up bar buttons } func setupRefreshControl() { // set up refresh control } setupBarButton() setupRefreshControl() } είʔϓΛอͬͨ··ɺॲཧΛߏ଄Խ͢Δ͜ͱ͕Ͱ͖Δ

Slide 18

Slide 18 text

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ଆʹҠৡͨ͠΄͏͕Մಡੑ͕ߴ͍

Slide 19

Slide 19 text

Any Questions?