2016-07-09 6 views
0

Ich habe eine Situation, in der ich ein Obj-c-Projekt in Swift konvertieren. Es ist wie folgtkonvertieren objective-c dynamic binding to swift 2

// few lazy property initializers as, 
    @property (nonatomic, strong) MyObject *property1; 
    @property (nonatomic, strong) MyObject *property2; 
    @property (nonatomic, strong) MyObject *property3; 
    ... 

    // I keep an index value to map these into a dictionary for reference 
    - (NSDictionary *)indexMap 
    { 
    if (!_indexMap) 
    { 
    _indexMap = @{ 
    @(index1) : [NSValue valueWithPointer:@selector(property1)], 
    @(index2) : [NSValue valueWithPointer:@selector(property2)], 
    ... 
    }; 
    } 
    return _indexMap; 
    } 

// other dictionary for index to class map 
- (NSDictionary *)classMap 
{ 
return @{ 

NSStringFromClass(@"MyClassA") : @(index1), 
NSStringFromClass(@"MyClassB") : @(index1), 
NSStringFromClass(@"MyClassC") : @(index1), 

NSStringFromClass(@"MyClassD") : @(index2), 
NSStringFromClass(@"MyClassE") : @(index2), 

NSStringFromClass(@"MyClassF") : @(index3), 
... 
}; 
} 

// finally i have method to pass in the class name & it will first find corresponding index, then use the index to return the property selector. 

Mein Anliegen ist, was ist die schnelle wie dies zu tun?

+0

Oh, Gott sei Dank wir dies in Swift nicht tun kann. – Sulthan

Antwort

0

Ein Weg, dies zu tun wäre, eine Liste von Callbacks zu führen, die die Initialisierung durchführen. Hier ist eine Lösung, die sehr flexibel ist, obwohl einfachere Alternativen sicherlich möglich sind:

class ComponentManager { 

    // Global singleton: ComponentManager.sharedManager 
    static let sharedManager = ComponentManager() 

    // Define a callback type which is used to create instances. 
    // This is the lazy initialiser. 
    typealias Constructor =() -> NSObject? 

    private var constructor = [String: Constructor]() 
    private var instances = [String: NSObject]() 

    func register(name: String, constructor: Constructor) { 
     self.constructor[name] = constructor 
    } 

    func instanceNamed(name: String) -> NSObject? { 

     if let instance = instances[name] { 
      return instance 
     } 

     guard let constructor = constructor[name] else { 
      return nil 
     } 

     guard let instance = constructor() else { 
      return nil 
     } 

     instances[name] = instance 

     return instance 
    } 
} 

es zu benutzen:

class A: NSObject { 
    let foo: String 
    init(foo: String) { 
     self.foo = foo 
    } 
} 

class B: NSObject { 
    let bar: Int 
    init(bar: Int) { 
     self.bar = bar 
    } 
} 

let manager = ComponentManager.sharedManager 

// Register our classes. 
// Note the callback functions which actually perform the 
// initialisation, these are only called when the class is requested the 
// first time. 
manager.register("A") { return A(foo: "fizz") } 
manager.register("B") { return B(bar: 47) } 

// Create some instances 
let a = manager.instanceNamed("A") as? A 
let b = manager.instanceNamed("B") as? B 
let a2 = manager.instanceNamed("A") as? A 

print("a = \(a?.foo)") 
print("a2 = \(a?.foo)") 
print("b = \(b?.bar)") 
Verwandte Themen