在Swift中重写方法或属性时的多态性(Polymorphism when overriding methods or properties in Swift)
我想通过编译器确保
CarViewController
仅在车辆属性中接收Car
。鉴于以下快速示例代码:
class Vehicle { func doSomething(){} } class Car: Vehicle { func doCarThings(){} } class VehicleViewController : UIViewController { var vehicle : Vehicle!; override func viewDidLoad() { super.viewDidLoad(); vehicle.doSomething(); } } class CarViewController:VehicleViewController { var vehicle: Car! override func viewDidLoad() { super.viewDidLoad(); vehicle.doCarThings(); } }
我收到以下错误:
Cannot override mutable property 'vehicle' of type 'Vehicle!' with covariant type 'Car!'
Cannot override mutable property 'vehicle' of type 'Vehicle!' with covariant type 'Car!'
我尝试了一种基于泛型的方法:
class Vehicle { func doSomething(){} } class Car: Vehicle { func doCarThings(){} } class VehicleViewController<T:Vehicle> : UIViewController { var vehicle : T!; override func viewDidLoad() { super.viewDidLoad(); vehicle.doSomething(); } } class CarViewController:VehicleViewController<Car> { override func viewDidLoad() { super.viewDidLoad(); vehicle.doCarThings(); } }
这是正确的,但在故事板类中使用泛型导致错误(因为它们被编译为objective-c)。
我怎样才能做到这一点,而不使用泛型?
谢谢!
I want to ensure by compiler that
CarViewController
only receives aCar
in the vehicle property.Given the following swift example code:
class Vehicle { func doSomething(){} } class Car: Vehicle { func doCarThings(){} } class VehicleViewController : UIViewController { var vehicle : Vehicle!; override func viewDidLoad() { super.viewDidLoad(); vehicle.doSomething(); } } class CarViewController:VehicleViewController { var vehicle: Car! override func viewDidLoad() { super.viewDidLoad(); vehicle.doCarThings(); } }
I get the following error:
Cannot override mutable property 'vehicle' of type 'Vehicle!' with covariant type 'Car!'
I tried with a generics-based approach:
class Vehicle { func doSomething(){} } class Car: Vehicle { func doCarThings(){} } class VehicleViewController<T:Vehicle> : UIViewController { var vehicle : T!; override func viewDidLoad() { super.viewDidLoad(); vehicle.doSomething(); } } class CarViewController:VehicleViewController<Car> { override func viewDidLoad() { super.viewDidLoad(); vehicle.doCarThings(); } }
It is correct but using generics in storyboard classes results in errors (since they get compiled to objective-c).
How can I do this without using generics?
Thanks!
原文:https://stackoverflow.com/questions/43937762
最满意答案
因此,根据我们的讨论,我们发现:
Cesium.Entity
是Cesium.Entity
的一个数组。和
Cesium.Entity
的position
字段是一个Cesium.PositionProperty
,它是一个接口,它有一个方法getValue
,接受一个Cesium.JulianDate
作为它的第一个参数。 所以,把所有这些放在一起,你需要的唯一改变是:peakPosition = peaks[i].position;
至
peakPosition = peaks[i].position.getValue(new Cesium.JulianDate());
So, per our discussion we have discovered:
peaks
is an array ofCesium.Entity
s.and
the
position
field of aCesium.Entity
is aCesium.PositionProperty
, which is an interface that has a methodgetValue
, accepting aCesium.JulianDate
as its first parameter. So, putting all that together, the only change you need is:peakPosition = peaks[i].position;
to
peakPosition = peaks[i].position.getValue(new Cesium.JulianDate());
相关问答
更多-
严格的答案 :因为JS规范这样说 : 如果Type(x)是Number,那么 如果x是NaN,则返回false。 如果y是NaN,则返回false。 有用的答案 :浮点数的IEEE 754规范(所有语言都使用浮点数)表示NaN永远不会相等。 Strict answer: Because the JS spec says so: If Type(x) is Number, then If x is NaN, return false. If y is NaN, return false. Useful ans ...
-
接受的答案是100%无疑是错误的 。 不是一半错误甚至略有错误。 当这个问题在搜索中出现时,我担心这个问题很长一段时间会混淆和误导程序员。 NaN旨在通过所有计算来传播,像病毒一样感染他们,所以如果你深入,复杂的计算中的某个地方遇到了NaN,那么你不会冒出一个看似合理的答案。 否则通过身份NaN / NaN应等于1,以及所有其他后果如(NaN / NaN)== 1,(NaN * 1)== NaN等。如果你想象你的计算在某个地方出错(舍入产生一个零分母,产生NaN)等,那么你可能会从你的计算中得到非常不正确( ...
-
你忘了在减法中包装绝对值。 负数的sqrt是NaN。 编辑:我很蠢。 你可能想要做欧氏距离,即sqrt((x2-x1)^ 2 +(y2-y1)^ 2),平方而不是abs。 (如果你打算做出租车距离,如果你只能水平和垂直移动,也就是距离,那就是abs(x2-x1)+ abs(y2-y1)。) You forgot to wrap absolute value around the subtractions. sqrt of a negative number is NaN. EDIT: I am dumb. ...
-
您不在e函数中返回值。 You don't return a value in your e function.
-
大熊猫合并返回NaN?(Pandas Merge returning NaN?)[2024-01-14]
如果使用左连接,如果df2.State中不存在df2.State值,则它将在代码中返回NaN,这表示不存在匹配。 我将检查状态列中的每个字符串之后是否有任何额外的空格,并确保所有值都是大写字母并转换为str 。 If you use a left join, if df2.State value does not exist in df1.State, it will return NaN in Code, meaning there is not a match. I will check if ther ... -
我认为应该在“城市”而不是“国家”字段上调用地图,如下所示: df['State'] = df['City'].map(state_dict) 但是,这有一个问题,它会覆盖任何不在您的字典中的城市的原始“国家”值,例如“芝加哥”。 解决这个问题的一个解决方案是下面的语法笨拙(但我相信是正确的)代码: df['State'] = df.apply(lambda x: state_dict[x['City']] if x['City'] in state_dict else x['State'], axis= ...
-
因此,根据我们的讨论,我们发现: Cesium.Entity是Cesium.Entity的一个数组。 和 Cesium.Entity的position字段是一个Cesium.PositionProperty ,它是一个接口,它有一个方法getValue ,接受一个Cesium.JulianDate作为它的第一个参数。 所以,把所有这些放在一起,你需要的唯一改变是: peakPosition = peaks[i].position; 至 peakPosition = peaks[i].position.ge ...
-
代码返回NAN(Code returning NAN)[2023-08-01]
getHours成员是一个函数而不是值。 因此,您将一个数字乘以一个方法并获得NaN 。 确保调用该方法 time1.setHours(time1.getHours() + (6)); The getHours member is a function not a value. Hence you're multiplying a number by a method and getting NaN. Make sure to invoke the method time1.setHours(time1. ... -
调用apply ,数据帧被强制转换为矩阵。 如果存在任何字符列,则将整个数据帧强制转换为字符矩阵,并将所有NaN转换为NA df_1 <- data.frame(a=c("a","b","c"), b=c(NaN, NaN,NaN)) df_2 <- data.frame(a=c(1,2,3), b=c(NaN, NaN,NaN)) as.matrix(df_1) a b [1,] "a" NA [2,] "b" NA [3,] "c" NA as.matrix(df_2) ...
-
你可以修改Float#/使用细化(请记住Ruby中的大多数操作符只是方法) module RaiseExceptionOnNaN refine Float do def /(*) super.tap {|result| raise ZeroDivisionError if result.nan?} end end end 然后在您的模块/类中,使用该模块 class MyClass using RaiseExceptionOnNaN # Have a try ...