简体   繁体   English

试图快速声明自定义对象的数组

[英]Trying to declare an array of custom objects in swift

Here is my code 这是我的代码

    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)

    }

and here is my class 这是我的课

class planet
{
   var size: CGFloat
   init(size: CGFloat)
   {
     self.size = size
   }
}

I am trying to figure out how I can make an array of 8 new "planet" objects 我试图弄清楚如何制作8个新的“行星”对象阵列

you can do it like this: 您可以这样做:

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)

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. 我为您的Planet类添加了一个名称变量,然后数组初始化为var planets:[planet],作为示例,我附加了一个行星供您查看其工作方式。

It looks like you already have an array of orbit markers.. do you have an array of sizes? 看来您已经有一个轨道标记数组。.您是否有一系列尺寸?

Also, name your class with UpperCamelCase 另外,使用UpperCamelCase为班级命名

class Planet {
    let name: String
    let size: CGFloat
}

let sizes: CGFloat = [1,2,3,....] // planet sizes
let names = ["mercury", "x", ...] // planet names

let planets = zip(names, sizes).map{Planet(name: $0, size: $1)}

Also.. size is a little bit nondescript. 还有..大小有点难以描述。 Consider changing it to radius, volume, diameter or whatever the value actually represents. 考虑将其更改为半径,体积,直径或实际值。

Quick explanation - zip combines two arrays of equal sizes into one array of tuples, attaching elements pairwise into a tuple. 快速说明-zip将相等大小的两个数组合并为一个元组数组,将元素成对地附加到一个元组中。 Element 1 from array 1 is paired with element 1 from array 2, etc. 数组1中的元素1与数组2中的元素1配对,依此类推。

Map performs an operation on every tuple in the array of tuples that resulted from the zipping, return a Planet object for each tuple. Map对由压缩产生的元组数组中的每个元组执行操作,并为每个元组返回一个Planet对象。

Custom class array example swift 3.0 自定义类数组示例Swift 3.0

class Planet {
    var name: String!
    var size: CGFloat!

    init(name:String,size:CGFloat) {
        self.name = name
        self.size = size
    }
}

var planets = [Planet]()
let planet = Planet(name: "Earth", size: 1.2)
planets.append(planet)

for Planet in planets {
    print(Planet.name)
}

This is how you would do it in Swift 3 这就是您在Swift 3中的做法

var planets = [planet]() // Initialize an array of type planet
var mercury = planet()   // Initialize an instance of type planet
mercury.size =           // set size on mercury
planets.append(mercury)  // Add mercury to the array planets

This is untested, but those are some basic statements working with an array of a custom type. 这未经测试,但是这些是一些使用自定义类型数组的基本语句。

EDIT: I just noticed you have an initializer set up which means you could make a planet like this. 编辑:我刚刚注意到您已经设置了一个初始化程序,这意味着您可以制作一个这样的星球。

var earth = planet(size: earthSizeHere) // Use initializer
planets.append(earth)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM