如何在Swift中正确声明自定义对象数组?

时间:2021-07-31 16:30:57

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!

提前感谢您的帮助!

3 个解决方案

#1


39  

First, I'm going to assume you didn't want 2d arrays. If you did, I'll answer your question from that perspective below.

首先,我假设你不想要2d数组。如果你这样做了,我将从下面这个角度回答你的问题。

    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的空数组。不能使用subscript向数组添加元素,只能使用它来更改现有的元素。由于数组是空的,所以您可以使用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:

由于没有一个baseMakeUp初始化器允许您传递一个评级,所以元素的评级为0。然而,由于你将它添加到你的数组中,你现在可以使用下标来改变它的评级变量:

foundation[0].Rating = 3

If you did intend for 2d arrays.

如果你打算让2 d数组。

    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添加到第一行中添加的数组中。第三行使用下标修改2d数组第一个数组中的第一个元素的评级。

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.

希望这是有帮助的。

#2


6  

You have a few things going on here:

你有一些事情要做:

  1. You are missing some naming conventions. Class names should be capitalized (baseMakeUp should be BaseMakeUp).
  2. 您缺少一些命名约定。类名应该大写(baseMakeUp应该是baseMakeUp)。
  3. Class vars should be lower case (image, brand, color, rating). Also brand & color in the init method.
  4. 类vars应该是小写的(image, brand, color, rating)。在init方法中也使用品牌和颜色。
  5. 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]?
    
  6. As the other answer says, you also need to instantiate BaseMakeup objects using your init method:

    正如另一个答案所说,您还需要使用init方法实例化BaseMakeup对象:

    let aBaseMakeup = BaseMakeup(brand: "Revlon", color: "Red")
    
  7. After that, you can add BaseMakeup objects to your array like this:

    之后,可以向数组中添加BaseMakeup对象:

    foundation?.append(aBaseMakeup)
    

Hopefully I'm understanding what you are trying to accomplish.

希望我能理解你的目标。

#3


0  

You need to instantiate your class object

您需要实例化类对象。

example:

例子:

var foundation = baseMakeUp(Brand: "Some Brand", Color: "Red")

#1


39  

First, I'm going to assume you didn't want 2d arrays. If you did, I'll answer your question from that perspective below.

首先,我假设你不想要2d数组。如果你这样做了,我将从下面这个角度回答你的问题。

    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的空数组。不能使用subscript向数组添加元素,只能使用它来更改现有的元素。由于数组是空的,所以您可以使用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:

由于没有一个baseMakeUp初始化器允许您传递一个评级,所以元素的评级为0。然而,由于你将它添加到你的数组中,你现在可以使用下标来改变它的评级变量:

foundation[0].Rating = 3

If you did intend for 2d arrays.

如果你打算让2 d数组。

    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添加到第一行中添加的数组中。第三行使用下标修改2d数组第一个数组中的第一个元素的评级。

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.

希望这是有帮助的。

#2


6  

You have a few things going on here:

你有一些事情要做:

  1. You are missing some naming conventions. Class names should be capitalized (baseMakeUp should be BaseMakeUp).
  2. 您缺少一些命名约定。类名应该大写(baseMakeUp应该是baseMakeUp)。
  3. Class vars should be lower case (image, brand, color, rating). Also brand & color in the init method.
  4. 类vars应该是小写的(image, brand, color, rating)。在init方法中也使用品牌和颜色。
  5. 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]?
    
  6. As the other answer says, you also need to instantiate BaseMakeup objects using your init method:

    正如另一个答案所说,您还需要使用init方法实例化BaseMakeup对象:

    let aBaseMakeup = BaseMakeup(brand: "Revlon", color: "Red")
    
  7. After that, you can add BaseMakeup objects to your array like this:

    之后,可以向数组中添加BaseMakeup对象:

    foundation?.append(aBaseMakeup)
    

Hopefully I'm understanding what you are trying to accomplish.

希望我能理解你的目标。

#3


0  

You need to instantiate your class object

您需要实例化类对象。

example:

例子:

var foundation = baseMakeUp(Brand: "Some Brand", Color: "Red")