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

From Obj-C to Swift

From Obj-C to Swift

Madhavi Solanki shares her experience of Migrating from Obj-C to Swift.

Swift India

May 27, 2017
Tweet

More Decks by Swift India

Other Decks in Programming

Transcript

  1. Transition //2010 - Objective-C Person *mPerson = [[Person alloc] init];

    Person *mPerson = [[[Person alloc] init] autorelease]; -(void)dealloc{ [mPerson release] [super dealloc] } //2017 - Swift3 var person = Person() var person:Person? = nil var person: Person = Person()
  2. Transition Objective-c pointer is a variable whose value is the

    address of another variable - its direct address of memory location. Person *mPerson Swift You can use pointers in swift as well but since swift is memory safe so it prevents direct access of memory. A pointer encapsulates a memory address. Types that involve direct memory access get an “unsafe” prefix so the pointer type is called UnsafePointer. let pointer = UnsafeMutablePointer<Int>.allocate(capacity: count)
  3. //ENUM enum Example { case A case B case C

    case D } var example = Example.A example = .B enum Types { case Str(String) case Num(Double) } var a = Types.Str("hello") a = .Num(1.0) switch a { case .Str(let val): print(val) case .Num(let val): print(val) } Enum //Class class Example2 { var a = 0 var b = 0 init(_ a: Int, _ b: Int) { self.a = a self.b = b } } let example2 = Example2(1, 2) print(example2.a) print(example2.b) //Tuples var letters = ("a", "b") let (first, second) = letters print(first) // a print(second) // b var (a, _) = letters print(a) // a print(letters.0) // a print(letters.1) // b var letters2 = (first: "a", second: "b") print(letters2.first) // a print(letters2.second) // b
  4. //Struct struct Bucket<Bucketable> { var items = [Bucketable]() mutating func

    add(item: Bucketable) { items.append(item) } mutating func remove() { items = [] } } Enum //Generics var bucket = Bucket<Int>() bucket.add(item: 1) var bucket2 = Bucket<String>() bucket2.add(item:"special") class Basket<Basketable> { var items = [Basketable]() func add(_ item: Basketable) { items.append(item) } func remove() { items = [] } } var basket = Basket<Int>() basket.add(1) var basket2 = Basket<String>() basket2.add("control")
  5. How to Migrate Do one thing at a time. Try

    using tools Add Bridging headers Do class forward Declaration for using class from swift file to Objective-c User @objc keyword Check for Swift type Compatibility
  6. How to Migrate //Swift optional are annotated as __nonnull -

    (nonnull instancetype)initWithSongs:(NSArray<NSString *> * __nonnull)songs OBJC_DESIGNATED_INITIALIZER; //Declaring swift protocol that can be used in Objective-c Class @objc public protocol MySwiftProtocol { func requiredMethod() @objc optional func optionalMethod() } //Adopting swift Protocol in Objective-C implementation @interface MyObjcClass () <MySwiftProtocol> // ... @end @implementation MyObjcClass // ... @end
  7. Please share your feedback and ideas. We are keen to

    improve and decide topics for next meet up. Become speaker and share your knowledge and experience. We are keen on improvising, its possible with your feedback https://goo.gl/Zm9YdP