jbd*_*d36 35 arrays class instantiation ios swift
这是我的自定义课程...不确定我是否遗漏了任何东西......
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
}
}
Run Code Online (Sandbox Code Playgroud)
我想在这里实例化......
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
Run Code Online (Sandbox Code Playgroud)
我没有包含ViewController类的其余部分,因为我认为没有必要.
当我尝试为foundation [0] .Brand分配值时发生错误
提前感谢您的帮助!
Nig*_*tly 45
首先,我假设您不想要2D阵列.如果你这样做,我会从下面的角度回答你的问题.
var foundation = [baseMakeUp]()
Run Code Online (Sandbox Code Playgroud)
创建一个名为foundation的baseMakeUp的空数组.您不能使用下标向数组添加元素,您只能使用它来更改现有元素.由于您的数组是空的,因此添加带附加的元素.
foundation.append(baseMakeUp(Brand: "Brand", Color: "Color"))
Run Code Online (Sandbox Code Playgroud)
由于您没有baseMakeUp初始化程序,允许您传递元素评级为0的评级.但是,由于您将其附加到数组,现在可以使用下标来更改它的评级变量,如下所示:
foundation[0].Rating = 3
Run Code Online (Sandbox Code Playgroud)
如果您打算使用2d数组.
var foundation = [[baseMakeUp]]()
Run Code Online (Sandbox Code Playgroud)
创建数组
foundation.append([])
foundation[0].append(baseMakeUp(Brand: "Brand", Color: "Color"))
foundation[0][0].Rating = 3
Run Code Online (Sandbox Code Playgroud)
第一行将空数组附加到顶级数组.第二行将baseMakeUp附加到第一行中添加的数组.第三行使用下标来更改二维数组的第一个数组中第一个元素的等级.
希望这有助于解决您的问题.
另外
我打算在jday001s的答案中添加第1点和第2点,但你也应该查看他们的答案.
编辑
我刚刚意识到你正在尝试在错误的范围内向数组中添加元素.
你必须移动你的
foundation.append(baseMakeUp(Brand: "Brand", Color: "Color")
Run Code Online (Sandbox Code Playgroud)
在函数内部并自己调用它或将它们放在像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
}
}
Run Code Online (Sandbox Code Playgroud)
希望这很有帮助.
你有几件事情在这里:
baseMakeUp应该是BaseMakeUp).image,brand,color,rating).还有brand&color在init方法中.你打算让你的foundation阵列是多维的吗?如果你只想要一个常规数组,我会选择以下内容:
var foundation = [BaseMakeup]?
Run Code Online (Sandbox Code Playgroud)正如另一个答案所说,您还需要使用init方法实例化BaseMakeup对象:
let aBaseMakeup = BaseMakeup(brand: "Revlon", color: "Red")
Run Code Online (Sandbox Code Playgroud)之后,您可以将BaseMakeup对象添加到数组中,如下所示:
foundation?.append(aBaseMakeup)
Run Code Online (Sandbox Code Playgroud)希望我能理解你想要完成的事情.
您需要实例化您的类对象
例子:
var foundation = baseMakeUp(Brand: "Some Brand", Color: "Red")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
67051 次 |
| 最近记录: |