Share
Embed
Objective-Cのプロジェクトをリニューアルを機にSwift移行し始めた話
AKIBA.swift×Swift愛好会
http://love-swift.connpass.com/event/37019/
#akibaswift #love_swift
SAHistoryNavigationViewController
https://github.com/marty-suzuki/SAHistoryNavigationViewController
URLEmbeddedView
https://github.com/marty-suzuki/URLEmbeddedView
MisterFusion
https://github.com/marty-suzuki/MisterFusion
Taiki Suzuki
August 29, 2016
Transcript
58 slides
-
TARGETS -> Build Setting -> Swift Compiler - Code Generation
-> Objective-C Bridging Header -
Objective-C Bridging Header // // Use this file to import
your target's public headers that // you would like to expose to Swift. // #import "TNPost.h" #import "TNBodyElement.h" -
enum // Definition in Objective-C typedef enum : int32_t {
TNPostType_Text, TNPostType_Image, TNPostType_Text_Image, TNPostType_Video, TNPostType_Text_Video, } TNPostType; // Use in Swift let postType: TNPostType = TNPostType_Image TNPostType_Image.rawValue -
NS_ENUM // Definition in Objective-C typedef NS_ENUM(NSInteger, TNPostType) { TNPostType_Text,
TNPostType_Image, TNPostType_Text_Image, TNPostType_Video, TNPostType_Text_Video, }; // Use in Swift let postType: TNPostType = .Image TNPostType.Image.rawValue -
Variable arguments (ՄมҾ) // In Objective-C header file - (id)initWithPathString:(NSString
*)pathString parameter:(NSString *)firstParameter, ...; -
Swift readable definition // In Objective-C header file - (id)initWithPathString:(NSString
*)pathString parameters:(NSArray *)parameters; -
Implicitly Unwrapped Optional (҉తΞϯϥοϓܕ) and AnyObject // In Objective-C header
file - (id)initWithPathString:(NSString *)pathString parameters:(NSArray *)parameters; -
Original definition // In Objective-C header file - (id)initWithPathString:(NSString *)pathString
parameters:(NSArray *)parameters; Improved definition // In Objective-C header file - (instancetype __nullable)initWithPathString:(NSString * __nonnull)pathString parameters:(NSArray<NSString *> * __nullable)parameters; -
Optional and Correct type // In Objective-C header file -
(instancetype __nullable)initWithPathString:(NSString * __nonnull)pathString parameters:(NSArray<NSString *> * __nullable)parameters; -
TARGETS -> Build Setting -> Swift Compiler - Code Generation
-> Objective-C Generated Interface Header Name -
TARGETS -> Build Setting -> Packaging -> Product Module Name
// In Objective-C files (*.h or *.m) #import "ObjcSwiftSample-Swift.h" -
Change Product Module Name // In Objective-C files (*.h or
*.m) #import "ObjcSwiftSampleHoge-Swift.h" -
Circular reference ObjcSwiftSample-Bridging-Header.h // // Use this file to import
your target's public headers that // you would like to expose to Swift. // #import "TNPost.h" TNPost.h #import "ObjcSwiftSample-Swift.h" //Error @interface TNPost: NSObject @end -
TNPost.h typedef NS_ENUM(int32_t, TNPostType) { TNPostType_Text, TNPostType_Image, TNPostType_Text_Image, TNPostType_Video, TNPostType_Text_Video,
}; @class TNBodyElement; @interface TNPost : NSObject @property (assign, nonatomic) NSInteger postId; @property (assign, nonatomic) NSInteger senderId; @property (assign, nonatomic) TNPostType postType; @property (copy, nonatomic) NSArray<TNBodyElement *> *elements; @property (copy, nonatomic) NSDate *createdDate; @property (copy, nonatomic) NSDate *updatedDate; @end -
TNBodyElement.h typedef NS_ENUM(NSInteger, TNBodyType) { TNBodyType_Text, TNBodyType_Image, TNBodyType_Video }; @interface
TNBodyElement : NSObject @property (assign, nonatomic) TNBodyType bodyType; //type text @property (copy, nonatomic) NSString *text; //type image & video @property (copy, nonatomic) NSString *thumbnailUrl; @property (assign, nonatomic) CGFloat width; @property (assign, nonatomic) CGFloat height; //type image @property (copy, nonatomic) NSString *rawImageUrl; //type video @property (copy, nonatomic) NSString *videoUrl; @end -
Swift Enum enum PostType { case Text case Image case
Text_Image case Video case Text_Video } -
Objective-C readable enum @objc enum PostType: Int { case Text
case Image case Text_Image case Video case Text_Video } -
Swift class class Post { let postId: Int let senderId:
Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate } -
class Post: NSObject { let postId: Int let senderId: Int?
let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate init(postId: Int, senderId: Int?, postType: PostType, elements: [BodyElement], createdDate: NSDate, updatedDate: NSDate) { //... } } -
Associated Values enum BodyElement { case Text (text : String)
case Image(thumbnail: String, width: CGFloat, height: CGFloat, rawImageUrl: String) case Video(thumbnail: String, width: CGFloat, height: CGFloat, videoUrl: String) } -
class Post: NSObject { let postId: Int let senderId: Int?
let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate init(postId: Int, senderId: Int?, // can not recognize from Objective-C postType: PostType, elements: [BodyElement], // can not recognize from Objective-C createdDate: NSDate, updatedDate: NSDate) { //... } } -
class Post: NSObject { let postId: Int let senderId: Int?
let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate init(postId: Int, senderId: NSNumber?, postType: PostType, elements: [TNBodyElement], createdDate: NSDate, updatedDate: NSDate) { //... } } -
extension TNBodyElement { func toBodyElement() -> BodyElement? { switch bodyType
{ case .Text: guard let text = text else { return nil } return .Text(text: text) case .Image: guard let thumbnailUrl = thumbnailUrl, rawImageUrl = rawImageUrl else { return nil } return .Image(thumbnail: thumbnailUrl, width: width, height: height, rawImageUrl: rawImageUrl) case .Video: guard let thumbnailUrl = thumbnailUrl, videoUrl = videoUrl else { return nil } return .Video(thumbnail: thumbnailUrl, width: width, height: height, videoUrl: videoUrl) } } } // rawElements: [TNBodyElement] let elements: [BodyElement] = rawElements.flatMap { $0.toBodyElement() } -
Unreadable from Objective-C struct Post { let postId: Int let
senderId: Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate } -
Wrapper class BodyElementWrapper: NSObject { class func text(text: String) ->
BodyElementWrapper { return BodyElementWrapper(bodyElement: .Text(text: text)) } //... let bodyElement: BodyElement init(bodyElement: BodyElement) { self.bodyElement = bodyElement super.init() } } -
Wrapper class Post: NSObject { let postId: Int let senderId:
Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate init(postId: Int, senderId: NSNumber?, postType: PostType, elements: [BodyElementWrapper], createdDate: NSDate, updatedDate: NSDate) { //... self.elements = elements.map { $0.bodyElement } //... } } -
Ordinary Code for Swift let view = UIView() self.view.addSubview(view) view.translatesAutoresizingMaskIntoConstraints
= false self.view.addConstraints([ NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 10), NSLayoutConstraint(item: view, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1, constant: -10), NSLayoutConstraint(item: view, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1, constant: 10), NSLayoutConstraint(item: view, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: -10), ]) Too long... -
Ordinary Code for Swift NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal,
toItem: self.view, attribute: .Top, multiplier: 1, constant: 10) -
MisterFusion Code for Swift let view = UIView() self.view.addLayoutSubview(view, andConstraints:
view.Top |+| 10, view.Right |-| 10, view.Left |+| 10, view.Bottom |-| 10 ) -
Ordinary Code for Objective-C UIView *view = [UIView new]; view.translatesAutoresizingMaskIntoConstraints
= NO; [self.view addSubview: view]; [self.view addConstraints:@[ [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:10.0f], [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0f constant:-10.0f], [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:10.0f], [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.5f constant:-15.0f] ]]; I can't see... too long... -
MisterFusion Code for Objective-C #import <MisterFusion/MisterFusion-Swift.h> UIView *view = [UIView
new]; [self.view addLayoutSubview:view andConstraints:@[ view.Top .Constant(10.0f), view.Right .Constant(-10.0f), view.Left .Constant(10.0f), view.Bottom.Constant(-10.0f) ]]; -
let hogeView = UIView() let misterFusions: [MisterFusion] if let imageView
= imageView { misterFusions = [ hogeView.Right |==| imageView.Left |+| 4, hogeView.Left |-| 4, ] } else { misterFusions = [ hogeView.Right |+| 8, hogeView.Left |-| 8, ] } let commonMisterFusion = [ hogeView.Top, hogeView.Bottom ] let combinedMisterFusions = Array([misterFusions, commonMisterFusion].flatten()) self.view.addLayoutSubview(hogeView, andConstraints: combinedMisterFusions)