Have a variable with multiple types in Swift

2020-04-05 06:44发布

I would like to have a variable, which can have multiple types (only ones, I defined), like:

var example: String, Int = 0
example = "hi"

This variable should be able to hold only values of type Int and String.

Is this possible?

Thanks for your help ;)

标签: ios swift types
4条回答
Deceive 欺骗
2楼-- · 2020-04-05 07:18

An “enumeration with associated value” might be what you are looking for:

enum StringOrInt {
    case string(String)
    case int(Int)
}

You can either assign a string or an integer:

var value: StringOrInt
value = .string("Hello")
// ...
value = .int(123)

Retrieving the contents is done with a switch-statement:

switch value {
case .string(let s): print("String:", s)
case .int(let n): print("Int:", n)
}

If you declare conformance to the Equatable protocol then you can also check values for equality:

enum StringOrInt: Equatable {
    case string(String)
    case int(Int)
}

let v = StringOrInt.string("Hi")
let w = StringOrInt.int(0)
if v == w { ... }
查看更多
Emotional °昔
3楼-- · 2020-04-05 07:18

No, this is not possible for classes, structs, etc.

But it is possible for protocols.

You can this:

protocol Walker {
func go()
}
protocol Sleeper {
func sleep()
}

var ab = Walker & Sleeper

or even

    struct Person {
    var name: String
    }


var ab = Person & Walker & Sleeper

But I don't recomment use this way.

More useful this:

struct Person: Walker, Sleeper {
/// code
}
var ab = Person
查看更多
我命由我不由天
4楼-- · 2020-04-05 07:35

Here is how you can achieve it. Works exactly how you'd expect.

protocol StringOrInt { }

extension Int: StringOrInt { }
extension String: StringOrInt { }

var a: StringOrInt = "10"
a = 10 //> 10
a = "q" //> "q"
a = 0.8 //> Error

NB! I would not suggest you to use it in production code. It might be confusing for your teammates.

UPD: as @Martin R mentioned: Note that this restricts the possible types only “by convention.” Any module (or source file) can add a extension MyType: StringOrInt { } conformance.

查看更多
手持菜刀,她持情操
5楼-- · 2020-04-05 07:41

You can use Tuple.

Example:

let example: (String, Int) = ("hi", 0)

And access each data by index:

let stringFromExampleTuple = example.0 // "hi"
let intFromtExampleTuple = example.1 // 0
查看更多
登录 后发表回答