想知道为什么 Swift switch 语句不允许像其他语言那样实例化类,以及如何解决这个问题。如果有人可以帮助解决这个问题,我们会很高兴。 在示例中,我创建了一个简单的 Vehicle 类,并尝试根据 classSetter 值通过开关实例化其子类。但是,如果在 switch(或任何其他类型的条件)语句中实例化,打印语句的最后一行不能打印任何类的名称属性。
import UIKit
class Vehicle {
var name: String {return ""}
}
class Car: Vehicle {
override var name: String {return "Car"}
}
class Bike: Vehicle {
override var name: String {return "Bike"}
}
var classSetter:Int = 1
switch classSetter {
case 1:
println("Initializing Car")
var vehicle = Car()
case 2:
println("Initialized Bike")
let vehicle = Bike()
default:
println("just defaulted")
}
println("Name property from initialization is \(vehicle.name)")
最佳答案
您的两个 vehicle
是在开关的 { }
中声明的,因此它们仅存在于该 block 中(即它们的“范围”)。它们不存在于它之外,因此您不能在那里引用它们,因此会出现错误。
对此(其他答案给出的)的默认解决方案是将 vehicle
声明为开关外部的 var
,但这里有一个替代方案:将开关包裹在一个闭包表达式并从中返回一个值。为什么要这样做?因为这样你就可以使用 let
而不是 var
来声明 vehicle
:
let vehicle: Vehicle = { // start of a closure expression that returns a Vehicle
switch classSetter {
case 1:
println("Initializing Car")
return Car()
case 2:
println("Initialized Bike")
return Bike()
default:
println("just defaulted")
return Vehicle()
}
}() // note the trailing () because you need to _call_ the expression
println("Name property from initialization is \(vehicle.name)")
如果 if
和 switch
是表达式(即求值结果)就好了,这样你就不需要这个闭包了,但现在这是一个合理的解决方法.
请注意,此处使用 var
方法的几个答案建议将 vehicle
设为可选值(即 Vehicle?
)。这不是必需的——只要保证代码在使用前为 vehicle
赋值(编译器会为您检查),它就不必是可选的。但我仍然认为闭包表达式版本是更好的方法。
顺便说一句,您可能需要考虑为 Vehicle
使用协议(protocol)而不是基类,因为这样您就不必为 Vehicle
提供默认值但是 name
的无效实现:
protocol Vehicle {
var name: String { get }
}
// one of the benefits of this is you could
// make Car and Bike structs if desired
struct Car: Vehicle {
var name: String {return "Car"}
}
struct Bike: Vehicle {
var name: String {return "Bike"}
}
尽管这意味着您无法从 Vehicle()
的 switch 语句中获得默认返回值。但无论如何,这很可能会很糟糕——一个可选的 Vehicle?
和 nil
代表失败可能是一个更好的选择:
let vehicle: Vehicle? = {
switch classSetter {
case 1:
println("Initializing Car")
return Car()
case 2:
println("Initialized Bike")
return Bike()
default:
println("no valid value")
return nil
}
}()
// btw since vehicle is a Vehicle? you need to unwrap the optional somehow,
// one way is with optional chaining (will print (nil) here)
println("Name property from initialization is \(vehicle?.name)")
如果您根本不希望出现这种情况,您可以考虑将不同类型车辆的指示器设为一个枚举,这样它就只能是一组有效车辆中的一个:
enum VehicleKind {
case Bike, Car
}
let classSetter: VehicleKind = .Car
let vehicle: Vehicle = {
switch classSetter {
case .Car:
println("Initializing Car")
return Car()
case .Bike:
println("Initialized Bike")
return Bike()
// no need for a default clause now, since
// those are the only two possibilities
}
}()
关于swift - 为什么不能在 Swift 的 Switch 语句中初始化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27589726/