- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这里有一些设置代码来解释正在发生的事情:
protocol CanJump{
func jump()
}
struct Dog: CanJump{
func jump(){
print("yay jump")
}
}
struct Cat: CanJump{
func jump(){
print("nothx")
}
}
let d = [Dog()]
let c = Cat()
这不起作用:
let thingsThatCanJump: [CanJump] = d
Cannot convert value of type [Dog] to specified type [CanJump]
这确实有效:
let thingsThatCanJump: [CanJump] = [c]
我想到的一个令人作呕的解决方法是:
let thingsThatCanJump: [CanJump] = d.map({$0 as CanJump})
谁能解释为什么会这样?
我猜这与编译器没有完全评估类型和一致性有关。
最佳答案
这是因为您 let d
实际上是 [Dog] 的类型,而 swift 对类型安全非常严格。
要解决此问题,您可以执行以下操作:
let d: [CanJump] = [Dog()]
希望对你有帮助
关于ios - 无法将类型 '[SomeStruct]' 的值转换为指定类型 '[SomeProtocol]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37205631/
我定义了 3 个结构和两种类型 type OnMoveEvent = fn(Board) -> (); type OnGameOverEvent = fn(Player) -> (); #[wasm_
假设 SomeStruct 定义为: struct SomeStruct { int member; }; 这些是什么意思? &SomeStruct::member int SomeStruc
const Vector4 colorBlack = new Vector4(0,0,0,1);//Vector4 is struct public static void example(Vecto
考虑以下几点: struct SomeStruct {} var foo: Any! let bar: SomeStruct = SomeStruct() foo = bar // Compiles
考虑以下几点: struct SomeStruct {} var foo: Any! let bar: SomeStruct = SomeStruct() foo = bar // Compiles
我试图理解 ORM 库的这段代码,但我无法理解 (*User)(nil) 的含义?第一个括号是指向 User 结构的指针,那么第二个括号代表什么? type User struct { Id
我想获取结构中特定成员的大小。 sizeof(((SomeStruct *) 0)->some_member) 对我有用,但我觉得可能有更好的方法。 我可以#define SIZEOF_ELEM(ST
我不小心输入了一个默认的可初始化对象 struct至 std::numeric_limits::infinity() .我得到的是一个默认结构。 为什么标准允许它编译并返回这样一个意想不到的值? #i
我有一个或多或少像这样的结构: [Serializable] [XmlRoot("Customer")] public struct TCustomer { string CustomerNo;
假设我有很多带有接收器的函数或方法,每个函数或方法都有不同类型的参数。我想使用表驱动方法来调度函数或方法调用。所以我将构建一个这样的表: type command struct { name s
我正在查看一些使用的代码 Rc> 所以我出去阅读了 Rc 和 RefCell 之间的差异: 以下是选择 Box、Rc 或 RefCell 的原因的概述: Rc enables multiple own
我在这里做了一些比较: https://github.com/itchingpixels/structs-vs-classes似乎将结构插入结构数组比将类插入类数组(具有相同数据)慢 10 倍。 我的
例如: type name struct { name string age int } func main() { c := make(chan name)
给定 SomeStruct 如下: struct SomeStruct { int i; char c; }; 下面不编译。 int main() { std::array a
谁能解释一下这个错误是什么意思?这实际上是相同的结构。它是否无法以某种方式推断出 R 或 F 并以这种方式报告? src/demo.rs:113:51: 113:65 error: mismatche
主题。用例是:假设我有一个非托管缓冲区,用于存储 SomeStruct 的 N 个实例。 .所以在这个缓冲区中有一个地址,有没有办法把这个地址转换成ref SomeStruct -- 例如能够直接更新
这里有一些设置代码来解释正在发生的事情: protocol CanJump{ func jump() } struct Dog: CanJump{ func jump(){
我有一个结构 type mapKey string var key1 mapKey = "someKey" var key2 mapKey = "anotherKey" type SampleMap
当我尝试使用用户定义的转换运算符从接口(interface)类型转换为通用结构类型时,出现编译错误,指出类型无法转换: public interface J { } public struct S {
我是一名优秀的程序员,十分优秀!