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

Refactoring an Ugly Objective-C with Swift

Konstantin
February 11, 2016

Refactoring an Ugly Objective-C with Swift

Refactoring an Ugly Objective-C with Swift

Konstantin

February 11, 2016
Tweet

More Decks by Konstantin

Other Decks in Programming

Transcript

  1. I am - !" ! - Swift ! - Programming

    ! - Swift Package Manager http://swifthighperformance.com ! Swift Hight Performance
  2. Amazing Feature № 1 KKOStorage *storage = [KKOStorage new]; [storage

    saveItem:@"New item"]; KKONetwork *network = [KKONetwork new]; [network uploadItem:@"Some stuff"];
  3. Amazing Feature № 1 KKOStorage *storage = [KKOStorage new]; [storage

    saveItem:@"New item"]; KKONetwork *network = [KKONetwork new]; [network uploadItem:@"Some stuff"]; [network saveItem:@"Some stuff"]; ❗Error❗
  4. BUT

  5. [network performSelector: @selector(saveItem:)]; id network = [[KKONetwork alloc] init]; [network

    saveItem:@"!"]; [network arrayByAddingObjectsFromArray: @[ @10 ]]; [network URLByAppendingPathComponent:@"/me.com"];
  6. ! Does! // @interface NSObject <NSObject> // - (id)copy; NSMutableArray

    *ar = [NSMutableArray arrayWithArray:@[@1, @1]]; NSMutableArray *aCopy = [ar copy]; // it's a NSArray [aCopy addObject: @2]; //Crash here!!
  7. VS Swift let storage = Storage() storage.saveItem("Apple") let network =

    Network() network.uploadItem("Item") // network.saveItem("i") // Error! Always!
  8. VS Swift let any: Any = Network() // any.saveItem("i") //

    Can't!! if let any = any as? Storage { any.saveItem("i") }
  9. Objc id Can do Anything. Whatever you Want! id some;

    [some URLCache]; [some stringByAppendingString:@":("];
  10. Swift Any Can't do Nothing let any: Any = "String"

    any. //Any is empty. It has 0 methods print(any)
  11. - (instancetype)init { self = [super init]; if (self) {

    // Init ivars! // Don't access properties!!! } return self; }
  12. Go Crazy - (instancetype)init { return 0; // -1, etc

    } - (instancetype)init { return [self init]; } - (instancetype)init { return nil; } - (instancetype)init { // Do whatever You Want }
  13. @interface KKOArticle : NSObject @property (nonatomic, readonly, strong) NSString *title;

    @property (nonatomic, readonly, strong) NSString *text; @property (nonatomic, readonly, strong) NSDate *date; - (instancetype)initWith:(NSString *)title text:(NSString *)text date:(NSDate *)date; @end @implementation KKOArticle - (instancetype)initWith:(NSString *)title text:(NSString *)text date:(NSDate *)date { self = [super init]; if (self) { _title = title; _text = text; _date = date; } return self; } @end KKOArticle *article = [[KKOArticle alloc] initWith:@"title" text:@"text" date:[NSDate date]];
  14. Can't go Crazy :( init () { // - must

    initialize all properties // - super.init is required when has a superclass // - can't access properties and methods until filly initialized // - very safe }
  15. Swift struct Article { let title: String let text: String

    let date: NSDate } Article(title: "title", text: "Text", date: NSDate())
  16. NSString *a; [a stringByAppendingString:@"b"]; // Nothing [@"b" stringByAppendingString:a]; // Crash!

    [nil stringByAppendingString:@"b"]; [@"b" stringByAppendingString: nil];
  17. NSString *a; [a stringByAppendingString:@"b"]; // Nothing [@"b" stringByAppendingString:a]; // Crash!

    [nil stringByAppendingString:@"b"]; // Error [@"b" stringByAppendingString: nil];
  18. NSString *a; [a stringByAppendingString:@"b"]; // Nothing [@"b" stringByAppendingString:a]; // Crash!

    [nil stringByAppendingString:@"b"]; // Error [@"b" stringByAppendingString: nil]; // Warning //stringByAppendingString:(nonnull NSString *)
  19. NSString *a; [a stringByAppendingString:@"b"]; // Nothing [@"b" stringByAppendingString:a]; // Crash!

    [nil stringByAppendingString:@"b"]; // Error [@"b" stringByAppendingString: nil]; // Warning //stringByAppendingString:(nonnull NSString *) [(id)nil stringByAppendingString:a]; // !
  20. !"

  21. let a = "a" a + "b" /* ! */

    "b" + a /* ! */
  22. let a = "a" let a: String? a + "b"

    /* ! */ a + "b" // Error "b" + a /* ! */ "b" + a // Error
  23. let a: String? a + "b" // Error "b" +

    a // Error if let a = a { a + "b" // ! }
  24. let a: String? a + "b" // Error "b" +

    a // Error a.stringByAppendingString("b") // Error if let a = a { a + "b" // ! } a?.stringByAppendingString("b") // !
  25. typedef NS_ENUM(NSInteger, Action) { ActionDelete, ActionCreate, ActionEdit, ActionCopy }; //+

    (void)runAction:(Action)action; [Runner runAction:ActionCreate];
  26. typedef NS_ENUM(NSInteger, Action) { ActionDelete, ActionCreate, ActionEdit, ActionCopy }; //+

    (void)runAction:(Action)action; [Runner runAction:ActionCreate]; [Runner runAction:20]; // WAT ?!
  27. typedef NS_ENUM(NSInteger, Action) { ActionDelete, ActionCreate, ActionEdit, ActionCopy }; //+

    (void)runAction:(Action)action; [Runner runAction:ActionCreate]; [Runner runAction:20]; // WAT ?! // !"# Action action = ActionEdit; action += ActionCopy; action /= 56; [Runner runAction:action];
  28. + (NSString *)actionString:(Action)action { switch (action) { case ActionDelete: return

    @"Delete"; case ActionCreate: return @"Create"; case ActionEdit: return @"Edit"; case ActionCopy: return @"Copy"; } }
  29. enum Action { case Delete case Create case Edit case

    Copy } runAction(.Delete) //runAction(10) // Error!!
  30. enum Action: String { case Delete case Create case Edit

    case Copy } let action = Action.Delete.rawValue // Delete
  31. enum Action: String { case Delete case Create case Edit

    case Copy var isDangerous: Bool { return self == .Delete } } let danger = Action.Delete.isDangerous // true
  32. - (void)setup { [self setupWithName:[App name]]; } - (void)setupWithName:(NSString *)name

    { [self setupWithName:name mode:ModeSqlite]; } - (void)setupWithName:(NSString *)name mode:(Mode)mode { [self setupWithName:name mode:ModeSqlite logeLevel:LogLevelVerbose]; } - (void)setupWithName:(NSString *)name mode:(Mode)mode logeLevel:(LogLevel)logLevel { ... } CoreDataStack *stack; [stack setup]; [stack setupWithName:@"Data" mode:ModeInMemory]; [stack setupWithName:@"Data" mode:ModeInMemory logeLevel:LogLevelNone]; //[stack logeLevel:LogLevelNone]; Error
  33. Swift - Parameters default values func setup(name: String = App.name,

    mode: Mode = .Sqlite, logLevel: LogLevel = .Verbose) { ... } let stack = CoreDataStack() stack.setup() stack.setup(logLevel: .None) stack.setup(mode: .InMemory, logLevel: .None) stack.setup("DB", mode: .InMemory, logLevel: .None)