swift 协议要求类型为“类”,但希望它在类的哪些属性之间灵活

wfypjpf4  于 2023-03-07  发布在  Swift
关注(0)|答案(1)|浏览(102)

我正在为我正在制作的一个应用程序制作一个转换脚本,并希望为我制作的所有不同的转换结构体制定一个协议。我希望它们都有一个名为unitType的属性,一个名为amount的属性,以及一个名为convert(to otherType) -> Double的变异函数。以下是代码:

public class Units {
    enum TemperatureUnits {
        case fahrenheit, rankine
        case celsius, kelvin
    }
    enum DistanceUnits {
        case inch, foot, yard, mile
        case millimeter, centimeter, meter, kilometer
    }
    //Plus any others I decide to do
}

//Want to create a protocol here
protocol Conversion {
    var unitType
    var amount: Double 
    func convert(to otherType) -> Double
}

//This struct works, but i want to implement the protocol
public struct Temperature {

    var unitType: Units.TemperatureUnits
    var amount: Double
    
    mutating func convert(to otherType: Units.TemperatureUnits) -> Double {
        
        var returnAmount: Double
        
        switch otherType {
            
        case .fahrenheit:
            switch unitType {
                
            case .fahrenheit:
              returnAmount = amount
            case .celsius:
              returnAmount = (amount * (9/5)) + 32
            case .kelvin:
              returnAmount = (amount - 273.15) * (9/5) + 32
            case .rankine:
              returnAmount = amount - 459.67
            }
        case .celsius:
            switch unitType {
                
            case .celsius:
              returnAmount = amount
            case .kelvin:
              returnAmount = amount - 273.15
            case .fahrenheit:
              returnAmount = (amount - 32) * (5/9)
            case .rankine:
              returnAmount = (amount - 491.67) * (5/9)
            }
        case .kelvin:
            switch unitType {
                
            case .fahrenheit:
              returnAmount = (amount - 32) * (5/9) + 273.15
            case .celsius:
              returnAmount = amount + 273.15
            case .kelvin:
              returnAmount = amount
            case .rankine:
              returnAmount = amount * (5/9)
            }
        case .rankine:
            switch unitType {
                
            case .rankine:
              returnAmount = amount
            case .fahrenheit:
              returnAmount = amount + 459.67
            case .celsius:
              returnAmount = (amount * (9/5)) + 491.67
            case .kelvin:
              returnAmount = amount * (9/5)
            }
        }
        
        unitType = otherType
        amount = returnAmount
        return returnAmount
    }
}

现在,正如我所说的,当我试图制定一个多个结构体都能遵守的协议时,问题就来了。我将枚举封装在Units类中的全部原因是为了保持它们的组织性,并看看我是否能让协议像这样工作:

protocol Conversion {
    var unitType: Units { get set }
    var amount: Double { get set }
    func convert(to otherType: Units) -> Double
}

然后像这样实现它:

public struct Temperature: Conversion {
    var unitType: Units.TemperatureUnits
    var amount: Double

    mutating func convert(to otherType: Units.TemperatureUnits) -> Double {
        //code
    }
}

public struct Distance: Conversion {
    var unitType: Units.DistanceUnits
    var amount: Double

    mutating func convert(to otherType: Units.DistanceUnits) -> Double {
        //code
    }
}

但是它不起作用。我想知道我是否能够在swift中创建一个具有这种灵活性的协议,或者在这种情况下创建一个协议是否是必要的。(另外,嵌套switch语句是我能找到的应用转换的最有效的方法-我确信有一个更有效的方法,但我就是想不出来。)

ni65a41a

ni65a41a1#

我找到了一种使用泛型的方法,尽管我在发布这个问题之前尝试过,但我没有正确使用它。

protocol Conversion {
    associatedtype T
    
    var unitType: T { get set }
    var amount: Double { get set }
    mutating func convert(to otherType: T) -> Double
}

也感谢那些为我指出单位和度量API集合的人。你们为我节省了很多时间。

相关问题