---
# System prepended metadata

title: Object-C Migrate to Swift
tags: [Work On Note]

---

###### tags: `Work On Note`
# Object-C Migrate to Swift
> **Wranning**：There is nothing, just a shitpost.

**At my new job, We are refactoring in full swing.**
- Migrate to Swift
- MVC to MVVM
- Reactive programming (RxSwift)

**In a completed Application.**

### My nonsense journal
``(It will be fun and not big deal)`` what I think, when I get the offer and the task for me. 
After two weeks, I has refactored one view controller and refactoring another one, ``(It's really fun, but hard)``, I must stress this.

#### The Point to Solve
- Lots of delegate parttern of multi components.
     1. Refactor it 
     2. Build a middleware
     3. Or just leave it alone.
- Lots of reference containers those are not suitable migrated to reactive programming in full applection.
     1. Refactor it 
     2. Build a middleware
     3. Or just leave it alone.
- 3th AD SDK (We haven't figured out a solution yet)
     1. Just leave it alone.
- bla bla bla bla...
     1. Refactor it 
     2. Build a middleware
     3. Or just leave it alone.

There are many beautiful design of components in Object-c. As you see, those wheels we must to consider to refactor. But the more important are time required and scope of impact. And....the PM.

--- 

### What I do

```objectivec=
// Before: object-c
typedef void (^SomeCallback)(BOOL success, NSError *_Nullable error, NSArray *_Nullable whatever)) 
    
- (void)getSomeList:(nonnull SomeToken *)token 
                     callback:(nullable SomeCallback) callback;

// Swift 
// first middleware
struct FirstMiddleware: ApiMiddleware {
    func getSomeList(token: SomeToken, completion: @escaping Result<whatever, Error> {...}
}

// Swift
// second middleware for reactive
struct SecondMiddleware: ObserableMiddleware {
    func getSomeList(token: SomeToken)-> Obserable<whaterer> {...}
}
                                     
```

### delegate
```objectivec=
// Before: object-c
@protocol WhateverDelegate <NSObject>
- (void) foo:(SomeToken * _Nullable)token refreshToken:(NSString *_Nullable)refreshToken error:(NSError *_Nullable)error;
@end

@interface WhateverObject () <WhateverDelegate>

// create delegate middleware
class WhateverDelegateProxy: NSObject, ObservableType, WhateverDelegate {
typealias E = Event
private let publish: PublishRelay<FooResult> = .init()

func subscribe<O>(_ observer: O) -> Disposable where O: ObserverType, E == O.E {
        subject.bind(to: observer)
  }

func foo(_ token: SomeToken?, refreshToken: String?, error: Error?) { 
    publish.accept(FooResult(...))}
  }


// migrate swift
class WhateverObjectSwift {
   func bind() {
       let proxy = WhateverDelegateProxy()
       model.delegate = proxy
      proxy.bind(to:...).disposed(by: bag)
   }
}
```