More Related Content Similar to Xcode 6の新機能 (20) Xcode 6の新機能2. 自己紹介
• 佐藤 新悟
• iOSアプリ開発4年半ほど
• 現在は電子母子手帳 kazoc
3. アジェンダ
• Xcode 6の新機能概要
• Viewのデバッグ機能
• Interface Builderでライブレンダリング
• 非同期テスト
5. • Swift対応 - 全部swfitでも、Obj-Cと同居でもOK
Playground - テスト、学習
• XCTest・
非同期テスト、パフォーマンステスト
• Interface Builder・
カスタムビュークラスのライブレンダリング、
カスタムフォント
7. 詳しくは…
• What’s New in Xcode
https://developer.apple.com/library/ios/documentation/DeveloperTools/
Conceptual/WhatsNewXcode/Articles/Introduction.html
16. 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);
}
18. 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);
}
21. 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);
}
!
}
26. 非同期メソッドのテスト
テスト対象のメソッド
@interface ViewController : UIViewController
!
// a + b の結果を非同期で返す
- (void)addA:(NSInteger)a toB:(NSInteger)b
withCompletion:(void (^)(NSInteger result))completion;
!
@end
27. 非同期メソッドのテスト
// 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のサブクラス)
28. 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];
}
29. 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];
}
31. 参考資料
• 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