如何在Swift中正确声明自定义对象数组? [英] How to Properly Declare Array of Custom Objects in Swift?
问题描述
这是我的自定义类...不确定我是否遗漏了任何内容...
import UIKit
class baseMakeUp {
var Image = UIImage()
var Brand:String
var颜色:String
var评级:Int = 0
init(品牌:字符串,颜色:字符串){
self.Brand =品牌
self.Color =颜色
}
}
我正试图在这里实例化...
import UIKit
class ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {
required init(coder aDecoder:NSCoder){
fatalError( init(编码器:)尚未实现)
}
let cellIdentifier =cellIdentifier
var foundation:[[baseMakeUp]]
var blush:[[baseMakeUp]]
var eyes:[[baseMakeUp]]
var lips:[[baseMakeUp]]
var nails:[[baseMakeUp]]
//进行一些测试d ata在化妆阵列这里......
奠基[0] .Brand =Revlon-------------> 预期声明错误。
foundation [0] .Color =Red
foundation [0] .Rating = 3
foundation [1] .Brand =MAC
foundation [1] .Color =蓝色
基金会[1]。累计= 4
我没有包含ViewController类的其余部分,因为我认为没有必要。
当我尝试为foundation [0] .Brand <赋值时发生错误/ p>
提前感谢您的帮助!
首先,我是假设你不想要2D阵列。如果你这样做了,我会从下面这个角度回答你的问题。
var foundation = [baseMakeUp]()
创建一个名为foundation的baseMakeUp的空数组。您不能使用下标向数组添加元素,您只能使用它来更改现有元素。由于你的数组是空的,你可以添加带附加的元素。
foundation.append(baseMakeUp(Brand:Brand,Color:颜色))
由于你没有baseMakeUp初始化程序,允许你传递一个等级元素的评级为0.但是,由于您将它附加到您的数组,您现在可以使用下标来更改它的评级变量,如下所示:
foundation [0] .Rating = 3
如果您打算使用2d数组。
var foundation = [[baseMakeUp]]()
创建数组
foundation.append([])
foundation [0] .append(baseMakeUp(品牌:品牌,颜色:颜色))
foundation [0] [0] .Rating = 3
第一行将空数组附加到顶级数组。第二行将baseMakeUp附加到第一行中添加的数组。第三行使用下标来更改二维数组第一个数组中第一个元素的等级。
希望这有助于解决您的问题。
另外
我打算在jday001s这里添加积分1和2,但你应该检查一下他们也给出了答案。
编辑
我刚认识到你'重新尝试在错误的范围内向数组中添加元素。
你必须移动你的
foundation.append(baseMakeUp(品牌:品牌,颜色:颜色)
在函数内部并自己调用它或将它们放在像viewDidLoad这样的东西
例如:
class ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {
var foundation = [baseMakeUp]()
override func viewDidLoad(){
super.viewDidLoad ()
foundation.append(baseMakeUp(品牌:品牌,颜色:颜色)
基础[0] .Rating = 3
}
}
希望这有用。
Here is my custom class...not sure if I'm missing anything in it...
import UIKit
class baseMakeUp {
var Image = UIImage()
var Brand: String
var Color: String
var Rating: Int = 0
init (Brand: String, Color: String) {
self.Brand = Brand
self.Color = Color
}
}
I'm trying to instantiate here...
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let cellIdentifier = "cellIdentifier"
var foundation: [[baseMakeUp]]
var blush: [[baseMakeUp]]
var eyes: [[baseMakeUp]]
var lips: [[baseMakeUp]]
var nails: [[baseMakeUp]]
// put some test data in makeup arrays here...
foundation[0].Brand = "Revlon" -------------> "Expected declaration" error.
foundation[0].Color = "Red"
foundation[0].Rating = 3
foundation[1].Brand = "MAC"
foundation[1].Color = "Blue"
foundation[1].Rating = 4
I didn't include the rest of the ViewController class, because I didn't think it was necessary.
The error occurs when I attempt to assign a value to foundation[0].Brand
Appreciate your help in advance!
First, I'm going to assume you didn't want 2d arrays. If you did, I'll answer your question from that perspective below.
var foundation = [baseMakeUp]()
Creates an empty array of baseMakeUp called foundation. You can't use subscripting to add elements to an array, you can only use it to change existing elements. Since your array is empty you add elements with append.
foundation.append(baseMakeUp(Brand: "Brand", Color: "Color"))
Since you don't have a baseMakeUp initializer that allows you to pass a rating that element's rating is 0. However since you appended it to your array you can now use subscripting to change it's Rating variable like this:
foundation[0].Rating = 3
If you did intend for 2d arrays.
var foundation = [[baseMakeUp]]()
To create the array
foundation.append([])
foundation[0].append(baseMakeUp(Brand: "Brand", Color: "Color"))
foundation[0][0].Rating = 3
The first line appends an empty array to your top level array. The second line appends a baseMakeUp to the array added in the first line. The third line uses subscripting to change the Rating of the first element in the first array of your 2d array.
Hopefully that helps with your problem.
Additionally
I was going to add points 1 and 2 from jday001s answer here, but you should check out their answer as well.
Edit
I just realized that you're trying to add elements to your arrays in the wrong scope.
You'll have to move your
foundation.append(baseMakeUp(Brand: "Brand", Color: "Color")
inside a function and call it yourself or place them inside something like viewDidLoad
eg:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var foundation = [baseMakeUp]()
override func viewDidLoad() {
super.viewDidLoad()
foundation.append(baseMakeUp(Brand: "Brand", Color: "Color")
foundation[0].Rating = 3
}
}
Hopefully that's helpful.
这篇关于如何在Swift中正确声明自定义对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!