Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何创建返回整数元组的枚举函数?_Swift_Enums_Tuples_Func - Fatal编程技术网

Swift 如何创建返回整数元组的枚举函数?

Swift 如何创建返回整数元组的枚举函数?,swift,enums,tuples,func,Swift,Enums,Tuples,Func,大家好。我正在创建一个enum,以便在提供iDevice类型时获得屏幕分辨率(这里的屏幕大小数字是假的)。 当我使用noenum函数时,代码可以正常工作,但我更希望使用enum函数来保持事物的整洁和统一。 到目前为止,我使用enum函数的代码如下 enum iDeviceType { case iPhone(String) case iPad(String) ... func screenSize()->(Int,Int){ var myM

大家好。我正在创建一个
enum
,以便在提供iDevice类型时获得屏幕分辨率(这里的屏幕大小数字是假的)。 当我使用no
enum
函数时,代码可以正常工作,但我更希望使用
enum
函数来保持事物的整洁和统一。 到目前为止,我使用
enum
函数的代码如下

enum iDeviceType {
    case iPhone(String)
    case iPad(String)
    ...

    func screenSize()->(Int,Int){
        var myModel: (Int, Int)

        switch ????? {
        case .iPhone(let model):
            switch model {
            case "XR" : myModel = (width: 400, height: 612)
            case "6" : myModel = (width: 465, height: 712)
            case "6Plus" : myModel = (width: 465, height: 912)
            ...
            default: myModel = (width: 365, height: 512)
            }

        case .iPad(let model):
            switch model {
            case "Air 1gen" : myModel = (width: 365, height: 512)
            case "Air 2gen" : myModel = (width: 405, height: 565)
            ...
            default: myModel = (width: 365, height: 512)
            }

        default:
            print("not an iOS device")
        }
        return myModel
    }

}

let myModel = iDeviceType.iPhone("XR").screenSize()
print(myModel.height)
最后两行代码是调用
enum
函数并返回结果的方式

我错过了什么?我在问号处尝试了
self
,以获取当前的
iDeviceType
,但无法使其工作


有什么建议让它更清楚吗?我正在使用Swift 5。

这只需稍作修改即可工作。关键的修改是将
screenSize()
的返回类型指定为
(width:Int,height:Int)
,以便您可以解压缩结果

enum iDeviceType {
    case iPhone(String)
    case iPad(String)
    case other

    func screenSize() -> (width: Int, height: Int) {
        var myModel = (width: 0, height: 0)

        switch self {
        case .iPhone(let model):
            switch model {
            case "XR" : myModel = (width: 400, height: 612)
            case "6" : myModel = (width: 465, height: 712)
            case "6Plus" : myModel = (width: 465, height: 912)
            default: myModel = (width: 365, height: 512)
            }

        case .iPad(let model):
            switch model {
            case "Air 1gen" : myModel = (width: 365, height: 512)
            case "Air 2gen" : myModel = (width: 405, height: 565)
            default: myModel = (width: 365, height: 512)
            }

        default:
            print("not an iOS device")
        }
        return myModel
    }
}

let myModel = iDeviceType.iPhone("XR").screenSize()
print(myModel.height)
612


使
屏幕大小成为计算属性:

因为您没有将任何东西传递给<代码> ScRunZe()/Cuth>,请考虑将其作为计算属性:

更改:

func screenSize() -> (width: Int, height: Int) {
致:

然后像这样访问它:

let myModel = iDeviceType.iPhone("XR").screenSize

谢谢,我还在学习,下一步是开始使用var而不是func。因此,现在我一次就学会了两件事。我接下来要做/学习的是,模型(子)案例也是一个枚举,让它始终作为一个有效输入,就像我对iDeviceType所做的那样。有什么建议吗?是的,您可以将
enum
s用于
iPhoneModel
iPadModel
中定义的
iDeviceType
。枚举案例不能以数字开头,因此您可以让它们以下划线开头:
Enum iPadModel{case\u Air\u 1gen case\u Air\u 2gen}
然后您可以这样使用它:
让myModel=iDeviceType.iPhone(.\u XR).screenSize
完成,但看起来有点难看。我将重做/重构原来的一个,只需要一个
enum
设置
字符串
,比如
“iphone6”
“iPad Air 1代”
等等。那么我只需要在一个地点/时间提供
设备模型
类型。谢谢vacawama。我接下来要做的是,模型(子)案例也是一个
enum
,它始终是一个有效的输入,就像我对
iDeviceType
所做的那样。有什么建议吗?
let myModel = iDeviceType.iPhone("XR").screenSize