How to Properly Declare Array of Custom Objects in Swift?
Solution 1
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.
Solution 2
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)
Hopefully I'm understanding what you are trying to accomplish.
Solution 3
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)")
jbd36
Updated on July 09, 2022Comments
-
jbd36 almost 2 years
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!
-
Nightly about 9 yearsI've updated my answer. I hope I'm being clear enough.
-
jbd36 about 9 yearsYes that was it the whole time, I was in the wrong scope trying many things! Thanks again for your help, I hope to pass it on!
-
Lohith Korupolu over 7 yearsBut when I need to access the foundation array now, it would result like - BaseMakeup(Brand: "Something", color:"Something"). How do I get it as a simple result like {Brand: "SOmething", color: "Something"}?