31
Xcode 6の新機能 2014.10.18 iOS 8/Swift エンジニア勉強会@ヤフー 佐藤 新悟 @gonsee

Xcode 6の新機能

Embed Size (px)

DESCRIPTION

Xcode 6の新機能 2014.10.18 iOS 8/Swift エンジニア勉強会@ヤフー

Citation preview

Page 1: Xcode 6の新機能

Xcode 6の新機能2014.10.18

iOS 8/Swift エンジニア勉強会@ヤフー

佐藤 新悟 @gonsee

Page 2: Xcode 6の新機能

自己紹介

• 佐藤 新悟

• iOSアプリ開発4年半ほど

• 現在は電子母子手帳 kazoc

Page 3: Xcode 6の新機能

アジェンダ

• Xcode 6の新機能概要

• Viewのデバッグ機能

• Interface Builderでライブレンダリング

• 非同期テスト

Page 4: Xcode 6の新機能

Xcode 6の新機能概要

Page 5: Xcode 6の新機能

• Swift対応 - 全部swfitでも、Obj-Cと同居でもOK

Playground - テスト、学習

• XCTest・

非同期テスト、パフォーマンステスト

• Interface Builder・

カスタムビュークラスのライブレンダリング、カスタムフォント

Page 6: Xcode 6の新機能

• Debugger・

viewの階層構造を3D表示、

queueに積まれたblockの表示

• SpriteKit、SceneKit向け改善

• Extension、Frameworkのサポート

Page 7: Xcode 6の新機能

詳しくは…

• What’s New in Xcodehttps://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/Introduction.html

Page 8: Xcode 6の新機能

本日取り上げるもの

1. Viewのデバッグ機能

2. Interface Builderでライブレンダリング

3. 非同期テスト

Page 9: Xcode 6の新機能

1. Viewのデバッグ機能

Page 10: Xcode 6の新機能

Viewの階層構造を3D表示

Debugエリア上部のツールバー

Debug View Hierarchy

Page 11: Xcode 6の新機能
Page 12: Xcode 6の新機能

階層構造やプロパティの表示Debug navigator Object inspector

Size inspector

Page 13: Xcode 6の新機能

Viewのデバッグ機能

Demo

Page 14: Xcode 6の新機能

2. Interface Builderで ライブレンダリング

Page 15: Xcode 6の新機能

• 自前viewクラスをIB上で描画できる

• 自前viewクラスのプロパティをIBから

設定できる

※ Framework化は必須ではない

Page 16: Xcode 6の新機能

Viewクラスの実装

// MyCustomView.h !IB_DESIGNABLE @interface MyCustomView : UIView !@end

// MyCustomView.m !- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 2.0f); [[UIColor blueColor] setStroke]; CGRect strokeRect = CGRectInset(self.bounds, self.lineWidth/2.0f, self.lineWidth/2.0f); CGContextStrokeEllipseInRect(context, strokeRect); }

Page 17: Xcode 6の新機能

IB上に配置Viewを配置してCustom Classに独自viewクラスを指定

IB上に独自viewが描画され コード修正も自動で反映される

Page 18: Xcode 6の新機能

IB上で設定可能なプロパティの追加

// MyCustomView.h !IB_DESIGNABLE @interface MyCustomView : UIView !@property (nonatomic, assign) IBInspectable CGFloat lineWidth; !@property (nonatomic, strong) IBInspectable UIColor *borderColor; !@end

// MyCustomView.m !- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, self.lineWidth); [self.borderColor setStroke]; CGRect strokeRect = CGRectInset(self.bounds, self.lineWidth/2.0f, self.lineWidth/2.0f); CGContextStrokeEllipseInRect(context, strokeRect); }

Page 19: Xcode 6の新機能

指定したプロパティをIBから設定可能になる

IB上で設定可能なプロパティの追加

Page 20: Xcode 6の新機能

• NSInteger • CGFloat • double • BOOL • NSString • CGPoint • CGSize • CGRect • UIColor • UIImage

IB上で設定可能なプロパティの追加

他にも様々なタイプで利用可能

Page 21: Xcode 6の新機能

Swiftの場合

@IBDesignable class SwiftCustomView: UIView { ! @IBInspectable var lineWidth: CGFloat = 1.0 @IBInspectable var borderColor: UIColor = UIColor.blueColor() override func drawRect(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextSetLineWidth(context, self.lineWidth) borderColor.setStroke() let strokeRect = CGRectInset(bounds, lineWidth/2.0, lineWidth/2.0); CGContextStrokeEllipseInRect(context, strokeRect); } !}

Page 22: Xcode 6の新機能

IBでライブレンダリング

Demo

Page 23: Xcode 6の新機能

3. 非同期テスト

Page 24: Xcode 6の新機能

非同期なAPIが当たり前

• Block呼び出し

• デリゲートコールバック

• ネットワークリクエスト

• バックグラウンド処理

Page 25: Xcode 6の新機能

• Xcode 5からユニットテストのフレー

ムワークとしてXCTestが導入された

• Xcode 6から標準で非同期テストが可

能になった

• 旧バージョン(iOS 6以降)をサポート

Page 26: Xcode 6の新機能

非同期メソッドのテスト

@interface ViewController : UIViewController !// a + b の結果を非同期で返す - (void)addA:(NSInteger)a toB:(NSInteger)b withCompletion:(void (^)(NSInteger result))completion; !@end

テスト対象のメソッド

Page 27: Xcode 6の新機能

非同期メソッドのテスト

// AsyncTestDemoTests.m - (void)testAsyncAddition { // descriptionはログに出力される XCTestExpectation *expectation = [self expectationWithDescription:@"Addition"]; // テスト対象の非同期メソッドを呼ぶ ViewController *vc = [[ViewController alloc] init]; [vc addA:1 toB:1 withCompletion:^(NSInteger result) { XCTAssertEqual(result, 2); // 計算結果の検証 [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:1.0 handler:^(NSError *error) { NSLog(@"Error: %@", error); }]; }

テストコード(XCTestCaseのサブクラス)

Page 28: Xcode 6の新機能

KVOのテスト// ViewController.h !@property (assign, readonly) ViewControllerState state; - (void)changeState; // ViewControllerState1 -> ViewControllerState2

テストコード- (void)testStateChangesFromState1ToState2 { ViewController *vc = [[ViewController alloc] init]; XCTAssertEqual(vc.state, ViewControllerState1); [self keyValueObservingExpectationForObject:vc keyPath:@"state" expectedValue:@(ViewControllerState2)]; [vc changeState]; [self waitForExpectationsWithTimeout:1.0 handler:nil]; }

Page 29: Xcode 6の新機能

Notificationのテスト

テストコード- (void)testNotification { [self expectationForNotification:ViewControllerSomeNotification object:nil handler:^BOOL(NSNotification *notification) { NSLog(@"%@", notification); // テストの成否をYES,NOで返す return YES; }]; ViewController *vc = [[ViewController alloc] init]; [vc notify]; // ViewControllerSomeNotificationをpostするメソッド [self waitForExpectationsWithTimeout:1.0 handler:nil]; }

Page 30: Xcode 6の新機能

非同期テスト

Demo

Page 31: Xcode 6の新機能

参考資料• WWDC 2014 Session Videos

• What’s New in Xcode 6

• Debugging in Xcode 6

• Testing in Xcode 6

• New Features in Xcode 6 https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_6_0.html

• Creating a Custom View that Renders in Interface Builder https://developer.apple.com/library/ios/recipes/xcode_help-IB_objects_media/chapters/CreatingaLiveViewofaCustomObject.html