Swift,如果让 .. as ?一些字符串
Swift, cast to type by a given string if let .. as ? someString
我正在尝试存储字典 var items : [String:(type:String,item:AnyObject)] = [:]
例如密钥是 "foo" 和 items["foo"]?.type = "UILabel"
我想将字符串中的给定类型转换为 AnyObject
。
是否可以做这样的事情?:
//This is a string
if let myConvertedItem = items["file"]!.item as? items["file"]!.type{
//myConvertedItem is UILabel here..
}
有更好的方法吗?
编辑: 我看到这个函数 _stdlib_getTypeName()
但 swift 不认识它。我怎样才能宣布它?它也适用于 AnyObject
吗?
我不寻找的解决方案:
做这样的事情:
if items["file"]!.item is UILabel{
//ok it's UILabel
}
if items["file"]!.item is SomeOtherClassName{
//ok it's some other class name
}
因为这个 if 列表可能很长
谢谢!
switch
表达式是否适合您?
if let item: AnyObject = items["file"]?.item {
switch item {
case let label as UILabel:
// do something with UILabel
case let someOtherClass as SomeOtherClassName:
// do something with SomeOtherClass
default:
break
}
}
is it possible to do something like this?:
//This is a string
if let myConvertedItem = items["file"]!.item as? items["file"]!.type{
//myConvertedItem is UILabel here..
}
没有。这是不可能的。 Swift 在 编译时 知道其所有变量的类型。您可以 选项 单击一个变量,然后 Swift 会告诉您它是什么。您不能在 运行 时间拥有假定类型的变量。
看看这个小例子:
let random = arc4random_uniform(2)
let myItem = (random == 0) ? 3 : "hello"
您希望 myItem
如果 random == 0
成为 Int
,如果 random == 1
成为 String
,但是 Swift 编译器使 myItem
成为 NSObject
因为它将 3
视为 NSNumber
并且将 "hello"
视为 NSString
以便它可以确定类型myItem
.
即使这行得通,你会用它做什么? //myConvertedItem is UILabel here..
Swift 会知道 myConvertedItem
是一个 UILabel
,但是你写的代码不知道。在对它做 UILabel
之前,你必须先做 某事 才能知道它是 UILabel
。
if items["file"]!.type == "UILabel" {
// ah, now I know myConvertedItem is a UILabel
myConvertedItem.text = "hello, world!"
}
这将是与您不希望的方式相同的代码量:
if myItem = items["file"]?.item as? UILabel {
// I know myItem is a UILabel
myItem.text = "hello, world!"
}
我正在尝试存储字典 var items : [String:(type:String,item:AnyObject)] = [:]
例如密钥是 "foo" 和 items["foo"]?.type = "UILabel"
我想将字符串中的给定类型转换为 AnyObject
。
是否可以做这样的事情?:
//This is a string
if let myConvertedItem = items["file"]!.item as? items["file"]!.type{
//myConvertedItem is UILabel here..
}
有更好的方法吗?
编辑: 我看到这个函数 _stdlib_getTypeName()
但 swift 不认识它。我怎样才能宣布它?它也适用于 AnyObject
吗?
我不寻找的解决方案:
做这样的事情:
if items["file"]!.item is UILabel{
//ok it's UILabel
}
if items["file"]!.item is SomeOtherClassName{
//ok it's some other class name
}
因为这个 if 列表可能很长
谢谢!
switch
表达式是否适合您?
if let item: AnyObject = items["file"]?.item {
switch item {
case let label as UILabel:
// do something with UILabel
case let someOtherClass as SomeOtherClassName:
// do something with SomeOtherClass
default:
break
}
}
is it possible to do something like this?:
//This is a string
if let myConvertedItem = items["file"]!.item as? items["file"]!.type{
//myConvertedItem is UILabel here..
}
没有。这是不可能的。 Swift 在 编译时 知道其所有变量的类型。您可以 选项 单击一个变量,然后 Swift 会告诉您它是什么。您不能在 运行 时间拥有假定类型的变量。
看看这个小例子:
let random = arc4random_uniform(2)
let myItem = (random == 0) ? 3 : "hello"
您希望 myItem
如果 random == 0
成为 Int
,如果 random == 1
成为 String
,但是 Swift 编译器使 myItem
成为 NSObject
因为它将 3
视为 NSNumber
并且将 "hello"
视为 NSString
以便它可以确定类型myItem
.
即使这行得通,你会用它做什么? //myConvertedItem is UILabel here..
Swift 会知道 myConvertedItem
是一个 UILabel
,但是你写的代码不知道。在对它做 UILabel
之前,你必须先做 某事 才能知道它是 UILabel
。
if items["file"]!.type == "UILabel" {
// ah, now I know myConvertedItem is a UILabel
myConvertedItem.text = "hello, world!"
}
这将是与您不希望的方式相同的代码量:
if myItem = items["file"]?.item as? UILabel {
// I know myItem is a UILabel
myItem.text = "hello, world!"
}