xcode 6の新機能

Post on 28-Nov-2014

11.424 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Xcode 6の新機能2014.10.18

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

佐藤 新悟 @gonsee

自己紹介

• 佐藤 新悟

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

• 現在は電子母子手帳 kazoc

アジェンダ

• Xcode 6の新機能概要

• Viewのデバッグ機能

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

• 非同期テスト

Xcode 6の新機能概要

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

Playground - テスト、学習

• XCTest・

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

• Interface Builder・

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

• Debugger・

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

queueに積まれたblockの表示

• SpriteKit、SceneKit向け改善

• Extension、Frameworkのサポート

詳しくは…

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

本日取り上げるもの

1. Viewのデバッグ機能

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

3. 非同期テスト

1. Viewのデバッグ機能

Viewの階層構造を3D表示

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

Debug View Hierarchy

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

Size inspector

Viewのデバッグ機能

Demo

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

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

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

設定できる

※ Framework化は必須ではない

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); }

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

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

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); }

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

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

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

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

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

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); } !}

IBでライブレンダリング

Demo

3. 非同期テスト

非同期なAPIが当たり前

• Block呼び出し

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

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

• バックグラウンド処理

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

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

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

能になった

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

非同期メソッドのテスト

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

テスト対象のメソッド

非同期メソッドのテスト

// 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のサブクラス)

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]; }

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]; }

非同期テスト

Demo

参考資料• 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

top related