I have 2 Types, first is Car
with property feature
and it of the second type Feature
.
我有2种类型,第一种是具有属性功能的Car,第二种是Feature。
I have a property cars: [Car]
, when I append new car to it, it returns nil
.
我有一辆物业车:[汽车],当我把新车添加到它时,它返回零。
I have created a sample in the snippet below:
我在下面的代码段中创建了一个示例:
class Car {
let make: String
var features: Feature?
let id: String
init(make: String, features: Feature?, id: String) {
self.make = make
self.features = features
self.id = id
}
}
class Feature {
let convertible: String
init(convertible: String) {
self.convertible = convertible
}
}
var cars: [Car]?
var feature: Feature?
let featureElement = Feature(convertible: "yes")
feature = featureElement
let car = Car(make: "SomeMaked", features: feature, id: "ID")
cars?.append(car)
print(cars)
My question is: Shouldn't the array increase its count after appening to it? What am I missing?
我的问题是:数组在加到它之后不应该增加它的数量吗?我错过了什么?
Please note that this code is just sample, so ignore typos and coding convention.
请注意,此代码只是示例,因此请忽略拼写错误和编码约定。
1 个解决方案
#1
2
You haven't initialized your array.
您尚未初始化阵列。
Replace your var cars: [Car]?
with var cars: [Car]? = []
更换你的变种车:[汽车]?与var车:[汽车]? = []
#1
2
You haven't initialized your array.
您尚未初始化阵列。
Replace your var cars: [Car]?
with var cars: [Car]? = []
更换你的变种车:[汽车]?与var车:[汽车]? = []