iPadawan
iPadawan

Reputation: 1110

How to create an enum function that returns a tuple of Ints?

Good day everybody. I'm creating an enum to get the screen resolution when I supply an iDevice type ( the screen size numbers are fake here ). I have code working when I use no enum function, but would rather like to use an enum function to keep things clean and together. The code that I have so far, for using an enum function is the following...

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)

The last two lines of code is the way I would like to call the enum function and get the result back.

What am I missing? I did try self, at the question marks, to get the current iDeviceType, but can't get it to work.

Any suggestions to make it more clear? I am using Swift 5.

Upvotes: 1

Views: 628

Answers (1)

vacawama
vacawama

Reputation: 154691

This works with a few modifications. The key modification is to specify the return type of screenSize() as (width: Int, height: Int) so that you can unpack the result.

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


Making screenSize a computed property:

Since you're not passing anything to screenSize(), consider making it a computed property:

change:

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

to:

var screenSize: (width: Int, height: Int) {

And then access it like this:

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

Upvotes: 1

Related Questions