Swift:如何让泛型代码更简洁

问题描述

我正在寻找使我的代码更简洁高效的方法我有一个“BaseInfo”,它有一些属性,而且它是“SomeInfo”和“AnotherInfo”类的父类,它们有自己的附加属性。我制作了一个通用函数,可以从 UserDefaults 保存和获取这些对象。我有使用这些函数保存和加载信息的 ViewController,考虑到它应该使用的信息类型。我想知道有什么方法可以使我的代码更清晰并摆脱 ViewContoller 中的那些类型转换。这是我的信息类:

public class  BaseInfo: Codable{
    var name: String?
    
    init(_ dict: [String: Any]){
        
        name = dict["name"] as? String
    }
}

class AnotherInfo: BaseInfo {
    var secondName: String?
    
    override init(_ dict: [String : Any]) {
        super.init(dict)
        secondName = dict["secondName"] as? String
    }
    
    required init(from decoder: Decoder) throws {
        fatalError("init(from:) has not been implemented")
    }
}

class SomeInfo: BaseInfo {
    var middleName: String?
    
    
    override init(_ dict: [String : Any]) {
        super.init(dict)
        middleName = dict["middleName"]
    }
    
    required init(from decoder: Decoder) throws {
        fatalError("init(from:) has not been implemented")
    }
}

这是我管理信息的班级

protocol InfoManagerProtocol {
    static func getInfo(with type: InfoType) -> BaseInfo?
    static func saveInfo <T: BaseInfo>(with type: InfoType,info: T)
}

class InfoManager: InfoManagerProtocol {
    
    static func getInfo<T: BaseInfo>(with type: InfoType) -> T? {
        if let data = UserDefaults.standard.object(forKey: type.toString()) as? Data,let info = try? JSONDecoder().decode(type.typeOfInfo() as! T.Type,from: data){
            return info
        }
        return nil
    }
    
    
    static func saveInfo<T>(with type: InfoType,info: T) where T : BaseInfo {
        if let encodedData = try? JSONEncoder().encode(info){
            UserDefaults.standard.setValue(encodedData,forKey: type.toString())
        }
    }
}

InfoType 枚举:

enum InfoType: Int{
    case base = 1
    case another = 2
    case some = 3
}

extension InfoType{

    func toString() -> String{
      switch self{
       case .base: 
           return "base"
       case .another: 
           return "another"
       case .some:
           return "some"
      }
    } 
    func typeOfInfo() -> BaseInfo.Type{
        switch self{
        case .base:
            return BaseInfo.self
        case .another:
            return AnotherInfo.self
        case .some:
            return SomeInfo.self
        }
    }
}

和一些控制器

class ViewCV: UIViewController{
    ......
    // some outlets
    var infoType: InfoType? // info 

    // some func that updates UI
    func updateUI(){
       
       let genericInfo = InfoManager.getInfo(info: .infoType)
       self.someNameOutletLabel.text = genericInfo.name
       self.secondNameOutletLabel?.text = (genericInfo as? AnotherInfo).secondName // here I don't like it
       if let someInfo = genericInfo as? SomeInfo{
          self.secondNameOutletLabel?.text = someInfo.thirdName // or like here I also don't like
       }
   }
}

期待其他评论和建议

解决方法

您可以通过跳过 InfoType 类型来简化代码,而是从给定的参数或返回值定义所使用的类型。

所以协议变成了

protocol InfoManagerProtocol {
    static func getInfo<T: BaseInfo>() -> T?
    static func saveInfo <T: BaseInfo>(info: T)
}

然后是实现。请注意,我现在使用类名本身而不是枚举中的 toString 作为键

class InfoManager: InfoManagerProtocol {
    static func getInfo<T: BaseInfo>() -> T? {
        if let data = UserDefaults.standard.object(forKey: "\(T.self)") as? Data,let info = try? JSONDecoder().decode(T.self,from: data){
            return info
        }
        return nil
    }

    static func saveInfo<T>(info: T) where T : BaseInfo {
        if let encodedData = try? JSONEncoder().encode(info){
            print(encodedData)
            UserDefaults.standard.set(encodedData,forKey: "\(T.self)")
        }
    }
}

这是一个如何使用它的例子(假设 init(from:) 已经正确实现)

let dict: [String: String] = ["name": "Joe","secondName": "Doe","middleName": "Jr"]
let another = AnotherInfo(dict)
let some = SomeInfo(dict)

//Here the declaration of the argument tells the generic function what T is
InfoManager.saveInfo(info: another)
InfoManager.saveInfo(info: some)

//And here the declaration of the return value tells the generic function what T is
if let stored:AnotherInfo = InfoManager.getInfo() {
    print(stored,type(of: stored))
}
if let stored:SomeInfo = InfoManager.getInfo() {
    print(stored,type(of: stored))
}