简体   繁体   English

如何定义具有多个特定自定义类型的变量? (在斯威夫特中)

[英]How to define a variable with multiple specific custom types ? (in Swift)

Hi guys as you may know sometimes, it would be so handy and clean if we could define a variable which can store multiple variable types.大家好,您有时可能知道,如果我们可以定义一个可以存储多种变量类型的变量,那将会非常方便和干净。 this operation is easily possible if we use "Any" instead of our type name.如果我们使用“Any”而不是我们的类型名称,这个操作就很容易实现。 for example:例如:

var a : Any     // defines a variable with ability to store all types except a nil
var b : Any?    // defines a variable with ability to store all types including nil
var c : String  // a variable storing only and only a String value
var d : String? // a variable such as "c" , but also includes nil too

in the first two ones we can store all {Int, String, Float & etc.}.在前两个中,我们可以存储所有 {Int、String、Float 等}。 also in the third and fourth one we can store String values while we can't store others such as "Int" or "Float".同样在第三个和第四个中,我们可以存储字符串值,而不能存储其他值,例如“Int”或“Float”。 but what if in some cases we would like to have a variable which can store custom Types?但是如果在某些情况下我们想要一个可以存储自定义类型的变量怎么办? for example i want a variable which can store an "Int" value, can store a "String" one, but can not store "Float"s?例如我想要一个可以存储“Int”值的变量,可以存储一个“String”值,但不能存储“Float”?

var e : only(String and Int)
// some code like above, or even below :
var f : Any but not Float

is there any idea?有什么想法吗? is there any solution please?请问有什么解决办法吗?

thanks dudes

Currently there is no way to achieve this functionality. 当前尚无法实现此功能。 However, a closer, and in my opinion, a better way would be to use Any type and then use a guard or if let while casting the value to your desired type. 然而,近了,在我看来,一个更好的办法是使用Any类型,然后使用一个guard或者if let而铸造的价值,你需要的类型。 An example of such can be seen below: 下面是一个这样的示例:

Example

let a: Any    //e.g. you only want to store Int and String in this variable
if let _ = a as? Int {
    //a is of type Int
} else if let _ = a as? String {
    //a is of type String
}

Seems like you need to use protocols . 似乎您需要使用protocols

Something like: 就像是:

protocol CustomType {
    // your definitions ...
}

extension String: CustomType {}
extension Int: CustomType {}

let customType1: CustomType = "string"
print(customType1)
let customType2: CustomType = 0
print(customType2)

// Error: Value of type 'Double' does not conform to specified type 'CustomType'
// let customType3: CustomType = 0.0

In this case, values of CustomType type will only accept String and Int types (because of the protocol conformance). 在这种情况下, CustomType类型的值将仅接受StringInt类型(由于协议一致性)。

For most cases, when you want to store multiple types in a single variable or in a collection you would use an enum with associated values:在大多数情况下,当您想将多种类型存储在单个变量或集合中时,您将使用具有关联值的枚举:

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

var e: StringOrInt = .string("Hello World")
var f: StringOrInt = .int(42)
var g: [StringOrInt] = [e, f]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM