您的两个vehicles 是在开关的{ } 中声明的,因此它们只存在于该块中(即它们的“范围”)。它们不存在于它之外,所以你不能在那里引用它们,因此会出现错误。
对此的默认解决方案(其他答案正在给出)是将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 使用协议而不是基类,因为这样您就不必为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
}
}()