Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Objective-CからSwift移行を始めて感じた事/ObjC-to-Swift

 Objective-CからSwift移行を始めて感じた事/ObjC-to-Swift

Taketo Yoshida

January 17, 2017
Tweet

More Decks by Taketo Yoshida

Other Decks in Programming

Transcript

  1. 29 TUSVDU׾⢪欽ׇ׆ծDMBTTך׫׾⢪欽 湱✼✼䳔䚍׾䠐陎׃׋剅ֹ倯 @objc class User: NSObject { let name:

    String var email: String? = nil init(name: String) { super.init() self.name = name } } /40CKFDU׾竰䪫ׅ׷䗳銲ָ֮׷
  2. 30 0CK$ך؎ٝة٦ؿؑ٦أח،ظذ٦ءّٝ׾אֽ׷ 湱✼✼䳔䚍׾䠐陎׃׋剅ֹ倯 @interface User : NSObject @property (nonnull, nonatomic,

    readonly, copy) NSString *name; @property (nullable, nonatomic, copy) NSString *email; - (nonnull instancetype)initWithName:(nonnull NSString *)name; - (nonnull instancetype)init; @end let user = User(name: "hogehoge") user.email = "[email protected]" print("\(user.name) \(user.email ?? "no email")") // hogehoge [email protected]
  3. 31 0CK$ךًاحسךㄏせ鋉⵱׾㸚׷ 湱✼✼䳔䚍׾䠐陎׃׋剅ֹ倯 - (void)authenticateWithPassword:(nonnull NSString *)password; func authenticate(password: String)

    农럀涸חًاحسせָ㢌刿ׁ׸׷ #FUUFS5SBOTMBUJPOPG0CKFDUJWF$"1*T*OUP4XJGU IUUQTHJUIVCDPNBQQMFTXJGUFWPMVUJPOCMPCNBTUFSQSPQPTBMTPCKFDUJWFDOBNFUSBOTMBUJPONE
  4. 33 #PYػة٦ٝ 湱✼✼䳔䚍׾䠐陎׃׋剅ֹ倯 @interface LegacyViewController : UIViewController @property (readwrite, strong)

    id /* Box<User> */ _user; @end extension LegacyViewController { var user: User { get { return (self._user as! Box<User>).value } set { self._user = Box<User>(newValue) } } } struct User {}
  5. 35 5ZQF&BTVSF 湱✼✼䳔䚍׾䠐陎׃׋剅ֹ倯 struct Section<Item> { let items: [Item] }

    class UserCollectionViewController: UIViewController { typealias Item = User var section: Section<Item> init(section: Section<Item>) { self.section = section super.init(nibName: nil, bundle: nil) } } 0CK$⩎ַ׵כTFDUJPOח،ؙإأדֹזְ
  6. 36 5ZQF&BTVSF 湱✼✼䳔䚍׾䠐陎׃׋剅ֹ倯 extension Section where Item: Any { var

    anySection: AnySection { return AnySection(self) } } @objc class AnySection: NSObject { var items: [Any] init<Item: Any>(_ section: Section<Item>) { self.items = section.items } } "OZ4FDUJPOהְֲDMBTT׾欰䧭ׅ׷
  7. 37 5ZQF&BTVSF 湱✼✼䳔䚍׾䠐陎׃׋剅ֹ倯 class UserCollectionViewController: UIViewController { typealias Item =

    User var section: Section<Item> @objc var _section: AnySection { return self.section.anySection } init(section: Section<Item>) { self.section = section super.init(nibName: nil, bundle: nil) } } 0CK$⩎ַ׵כWD@TFDUJPOד،ؙإأדֹ׷
  8. 41 9DPEFך酡㸣堣腉ח蕱׃׬湱✼⿫撑ָ鳞ְ 4XJGUד剅ְ׋؝٦س׾0CK$⩎ד⢪ֲ儗鷞׮搫׶ח酡㸣堣腉ד蕱׃׬ 4XJGU̞0CKFDUJWF$ػة٦ٝךرًٔحز SWIFT_CLASS("_TtC11ProductName4User") @interface User : NSObject @property

    (nonatomic, readonly, copy) NSString * _Nonnull name; @property (nonatomic, copy) NSString * _Nullable email; - (nonnull instancetype)initWithName:(NSString * _Nonnull)name OBJC_DESIGNATED_INIT… - (void)authenticateWithPassword:(NSString * _Nonnull)password; - (nonnull instancetype)init SWIFT_UNAVAILABLE; @end ProductName-Swift.h ؎ٝة٦ؿؑ٦أָ刿倜ׁ׸זְה酡㸣ָ⸬ַזְ
  9. 49 ⣛㶷ꟼ⤘׾罋ִזְהؽٕس儗꟦ח蕱׃׬ 4XJGU̞0CKFDUJWF$ػة٦ٝךرًٔحز C Class 0CKFDUJWF$ 4XJGU A Class B

    Class ProductName-Swift.h K Class F Class C Class K Class F Class C Class K Class C Class ׮׃׋ֻׁ׿֮׏׋׵˘