如何在 Swift 中正确声明自定义对象数组? [英] How to Properly Declare Array of Custom Objects in Swift?

查看:36
本文介绍了如何在 Swift 中正确声明自定义对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的自定义类...不确定我是否遗漏了任何内容...

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

我没有包含 ViewController 类的其余部分,因为我认为没有必要.

I didn't include the rest of the ViewController class, because I didn't think it was necessary.

当我尝试为 Foundation[0].Brand 赋值时发生错误

The error occurs when I attempt to assign a value to foundation[0].Brand

提前感谢您的帮助!

推荐答案

首先,我假设您不想要二维数组.如果你这样做了,我会在下面从这个角度回答你的问题.

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]()

创建一个名为foundation 的baseMakeUp 空数组.您不能使用下标向数组添加元素,只能使用它来更改现有元素.由于您的数组为空,因此您使用 append 添加元素.

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"))

由于您没有允许您传递元素评分为 0 的评分的 baseMakeUp 初始值设定项.但是,由于您将其附加到数组中,您现在可以使用下标来更改它的评分变量,如下所示:

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

如果您确实打算使用二维数组.

    var foundation = [[baseMakeUp]]()

创建数组

    foundation.append([])
    foundation[0].append(baseMakeUp(Brand: "Brand", Color: "Color"))
    foundation[0][0].Rating = 3

第一行将一个空数组附加到您的顶级数组.第二行将 baseMakeUp 附加到第一行中添加的数组.第三行使用下标来更改二维数组第一个数组中第一个元素的评级.

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.

另外

我打算在这里添加 jday001s 答案中的第 1 点和第 2 点,但您也应该查看他们的答案.

I was going to add points 1 and 2 from jday001s answer here, but you should check out their answer as well.

编辑

我刚刚意识到您正试图在错误的范围内向数组添加元素.

I just realized that you're trying to add elements to your arrays in the wrong scope.

你必须移动你的

foundation.append(baseMakeUp(Brand: "Brand", Color: "Color")

在一个函数内部并自己调用它或将它们放在诸如 viewDidLoad 之类的东西中

inside a function and call it yourself or place them inside something like viewDidLoad

例如:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var foundation = [baseMakeUp]()

    override func viewDidLoad() {
        super.viewDidLoad()

        foundation.append(baseMakeUp(Brand: "Brand", Color: "Color")
        foundation[0].Rating = 3
    }
}

希望这会有所帮助.

这篇关于如何在 Swift 中正确声明自定义对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆