我想比较a
类型的两个变量(String
,例如b
和Any
),因此我首先想要将b
转换为a
类似let typedString = "string_value"
let a = typedString as Any
var b: Any = // irrelevant what actual value it has
let aType = type(of: a)
guard let castedB = b as? aType else { // this is where the compiler comp(i)lains
// can't cast b to type of a
return
}
这样的
b
但不幸的是,编译器不允许我使用消息
来做到这一点使用未声明的类型' aType'
有关如何将变量a
转换为(以前未知)变量类型dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
testCompile 'junit:junit:4.12'
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
//CardView
compile 'com.android.support:recyclerview-v7:25.1.0'
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.android.support:cardview-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.google.android.gms:play-services:10.0.1'
}
的任何建议吗?