Slide 1

Slide 1 text

8SBQQJOH J04XJUI 3VCZ.PUJPO By @clayallsopp

Slide 2

Slide 2 text

4PMJLF XIZ

Slide 3

Slide 3 text

4PMJLF XIZ

Slide 4

Slide 4 text

4PMJLF XIZ

Slide 5

Slide 5 text

8FMM XIZOPU

Slide 6

Slide 6 text

8IBUNBLFT B<8>SBQQFS

Slide 7

Slide 7 text

8SBQQFS

Slide 8

Slide 8 text

8SBQQFS

Slide 9

Slide 9 text

#VUIPX

Slide 10

Slide 10 text

%FMFHBUFT

Slide 11

Slide 11 text

• UITableViewDelegate • tableView:heightForRowAtIndexPath: • UITableViewDataSource • UIImagePickerControllerDelegate • NSURLConnectionDelegate • CBCentralManagerDelegate • GKPeerPickerControllerDelegate • GOToHellDelegate

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

CLLocationManagerDelegate • locationManager:didUpdateToLocation :fromLocation:

Slide 14

Slide 14 text

CLLocationManagerDelegate Location.get do |location| p location[:to] p location[:from] end

Slide 15

Slide 15 text

module Location module_function def get(&callback) @callback = callback @location_manager = CLLocationManager.alloc.init @location_manager.delegate = self @location_manager.startUpdatingLocation end def locationManager(manager, didUpdateToLocation:new, fromLocation:old) @callback.call to: newLocation, from: oldLocation end end

Slide 16

Slide 16 text

• What happens with concurrent @callback? • NSNotificationCenter • Multiple delegator objects (PUDIBT

Slide 17

Slide 17 text

#MPDLT

Slide 18

Slide 18 text

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]); } failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON ) { NSLog(@"Error: %@", [error localizedDescription]); }]; AFNetworking

Slide 19

Slide 19 text

operation = AFJSONRequestOperation.JSONRequestOperationWithRequest(request, success: lambda { |request, response, json| p "IP Address: #{json.valueForKeyPath('origin')}" }, failure: lambda { |request, response, error, json| { p "Error: #{error.localizedDescription}" })

Slide 20

Slide 20 text

operation = AFJSONRequestOperation.JSONRequestOperationWithRequest(request, success: -> (request, response, json) { p "IP Address: #{json.valueForKeyPath('origin')}" }, failure: -> (request, response, error, json) { p "Error: #{error.localizedDescription}" })

Slide 21

Slide 21 text

AFJSONRequestOperation.JSONRequestOperationWithRequest (request) do |one_thing| if one_thing.success? # hooray elsif one_thing.failure? # :( end end end

Slide 22

Slide 22 text

module AFMotion module JSON def self.for_request(request, &callback) operation = AFJSONRequestOperation.JSONRequestOperationWithRequest(request, success: -> (request, response, json) { result = AFMotion::HTTPResult.new(operation, json, nil) callback.call(result) }, failure: -> (request, response, error, json) { result = AFMotion::HTTPResult.new(operation, json, error) callback.call(result) } ) end end end

Slide 23

Slide 23 text

AFMotion::Operation::JSON.get("users") do |result| if result.success? p result.object elsif result.failure? p result.error.localizedDescription end end

Slide 24

Slide 24 text

[AFJSONRequestOperation JSONRequestOperationWithRequest:request success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]); } failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON ) { NSLog(@"Error: %@", [error localizedDescription]); }];

Slide 25

Slide 25 text

$POTUBOUT &OVNT

Slide 26

Slide 26 text

picker = UIImagePickerController.alloc.init picker.sourceType = UIImagePickerControllerSourceTypeCamera view = UIView.alloc.initWithFrame(frame) view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingPleaseNoMoreNotAgain

Slide 27

Slide 27 text

class UIView def autoresizing_mask=(mask) self.autoresizingMask = Constants.get("UIViewAutoresizing", mask) end end picker.source_type = :camera view.autoresizing_mask = [:flexible_width, :flexible_height]

Slide 28

Slide 28 text

module Constants module_function def get(base, value) case value when Integer value when NSArray value.reduce { |i, j| const_int_get(base, i) | const_int_get(base, j) } else value = value.to_s.camelize Kernel.const_get("#{base}#{value}") end end end github.com/clayallsopp/inspect-2013

Slide 29

Slide 29 text

• Need to hard-code all possible constants as a method (PUDIBT class AppDelegate private def constants_hack [UIViewAutoresizingFlexibleHeight, UIViewAutoresizingFlexibleWidth, UIViewAutoresizingFlexibleTopMargin, etc] end end

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

• camelCase snake_case • NSNames Module::Names • isEnabled enabled? • setTitle #title= • .alloc.init .new • (Under the hood, still use designated initializer i.e. initWithFrame:, initWithNibNamed:bundle:)

Slide 32

Slide 32 text

0QFSBUPS 0WFSMPBEJOH

Slide 33

Slide 33 text

[]= HttpClient.headers["X-Authentication"] = "123123" class HttpClient class HeaderDSL def initialize(http_client) @http_client = http_client end def []=(header, value) @http_client.setDefaultHeader(header, value: value) end end def headers @header_dsl ||= HeaderDSL.new(self) end end

Slide 34

Slide 34 text

<< class NSOperationQueue def <<(operation) self.addOperation(operation) end end NSOperationQueue.currentQueue << my_operation

Slide 35

Slide 35 text

github.com/clayallsopp/geomotion size = CGSize.make(width: 50, height: 20) size + CGSize.make(width: 100, height: 50) => CGSize(150, 70)

Slide 36

Slide 36 text

alias_method

Slide 37

Slide 37 text

class UIControl alias_method :enabled?, :isEnabled end my_button = UIButton.buttonWithType UIButtonTypeRoundedRect my_button.enabled?

Slide 38

Slide 38 text

class NSOperationQueue class << self alias_method :current, :currentQueue end end OperationQueue = NSOperationQueue OperationQueue.current == NSOperationQueue.currentQueue

Slide 39

Slide 39 text

.FUB 1SPHSBNNJOH

Slide 40

Slide 40 text

8IBUOFYU github.com/clayallsopp/rubymotion-wrappers rubymotion-wrappers.com/ @RM_Wrappers