本文介绍了尝试快速声明自定义对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
var planetNames = ["mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto"] //names of the planets
for currentRing in 0..<orbitMarkers.count
{
var planetNames[currentRing] = planet(size: 1.2)
}
这是我的课程
class planet
{
var size: CGFloat
init(size: CGFloat)
{
self.size = size
}
}
我试图弄清楚如何制作一个由8个新的行星"对象组成的数组
I am trying to figure out how I can make an array of 8 new "planet" objects
推荐答案
您可以这样做:
class planet
{
var name: String
var size: CGFloat
init(name: String, size: CGFloat)
{
self.size = size
self.name = name
}
}
var planets: [planet] = []
var mercury = planet(name: "Mercury", size: 20)
planets.append(mercury)
我为您的Planet类添加了一个名称变量,然后数组初始化为var planets:[planet],作为示例,我附加了一个行星供您查看其工作原理.
I added a name variable for your planet class, and then the array initialization is var planets: [planet] and as an example I have appended one planet for you to see how its done.
这篇关于尝试快速声明自定义对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!