ios 如何在 Swift 中正确声明自定义对象数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28163034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Properly Declare Array of Custom Objects in Swift?
提问by jbd36
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.
我没有包含 ViewController 类的其余部分,因为我认为没有必要。
The error occurs when I attempt to assign a value to foundation[0].Brand
当我尝试为 Foundation[0].Brand 赋值时发生错误
Appreciate your help in advance!
提前感谢您的帮助!
回答by Nightly
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 的baseMakeUp 空数组。您不能使用下标向数组添加元素,只能使用它来更改现有元素。由于您的数组为空,因此您可以使用 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:
由于您没有允许您传递元素评分为 0 的评分的 baseMakeUp 初始值设定项。但是,由于您将其附加到数组中,您现在可以使用下标来更改它的评分变量,如下所示:
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.
第一行将一个空数组附加到您的顶级数组。第二行将 baseMakeUp 附加到第一行中添加的数组。第三行使用下标来更改二维数组第一个数组中第一个元素的评级。
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.
我打算在这里添加 jday001s 答案中的第 1 点和第 2 点,但您也应该查看他们的答案。
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
在函数内部并自己调用它或将它们放在诸如 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.
希望这有帮助。
回答by jday
You have a few things going on here:
你有几件事在这里发生:
- You are missing some naming conventions. Class names should be capitalized (
baseMakeUp
should beBaseMakeUp
). - Class vars should be lower case (
image
,brand
,color
,rating
). Alsobrand
&color
in the init method. Do you intend for your
foundation
array to be multidimensional? If you just want a regular array, I'd go with something like:var foundation = [BaseMakeup]?
As the other answer says, you also need to instantiate BaseMakeup objects using your init method:
let aBaseMakeup = BaseMakeup(brand: "Revlon", color: "Red")
After that, you can add BaseMakeup objects to your array like this:
foundation?.append(aBaseMakeup)
- 您缺少一些命名约定。类名应该大写(
baseMakeUp
应该是BaseMakeUp
)。 - 类变量应该是小写 (
image
,brand
,color
,rating
)。还有brand
&color
在 init 方法中。 你打算让你的
foundation
数组是多维的吗?如果您只想要一个常规数组,我会选择以下内容:var foundation = [BaseMakeup]?
正如另一个答案所说,您还需要使用 init 方法实例化 BaseMakeup 对象:
let aBaseMakeup = BaseMakeup(brand: "Revlon", color: "Red")
之后,您可以像这样将 BaseMakeup 对象添加到您的数组中:
foundation?.append(aBaseMakeup)
Hopefully I'm understanding what you are trying to accomplish.
希望我理解你想要完成的任务。
回答by Tina Detroja
First Create a custom class...
首先创建一个自定义类...
import Foundation
class ClassName{
var id: Int?
var name: String?
var category_name: String?
var price: Double?
init(json: JSON) {
self.id = json["id"].int
self.name = json["name"].string
self.category_name = json["category_name"].string
self.price = json["price"].double
}
}
Use can use this Class in viewcontroller by following lines
使用可以通过以下几行在视图控制器中使用此类
var arrayName:Array<yourclassname> = Array()
print("/(yourclaseename[0].name)")
回答by gnm1978
You need to instantiate your class object
您需要实例化您的类对象
example:
例子:
var foundation = baseMakeUp(Brand: "Some Brand", Color: "Red")