Creating an array of all objects of the class
Is there a way to create an array of all created color objects? So, every time a new color is added, is it automatically added to the array as well?
class Colours {
var colourName: String
var colourShades: [String?]
init(colourName: String, colourShades: [String?]) {
self.colourName = colourName
self.colourShades = colourShades
}
}
var red = Colours(colourName: "Red", colourShades: ["Crimson", "Cherry", "Rose"])
var blue = Colours(colourName: "blue", colourShades:["Ice", "Baby", "Royal"])
To give some context, I am trying to develop an IOS application that includes a color table. Then, when the user clicks on a color, it translates them into another table with shades of that color.
I need an array of colors so that I can auto fill the rows of the table and then when the user adds a new color it will automatically add a new row.
source to share
Use the static array defined in Colours
to keep all generated colors. You can access this array Colours.allColours
from anywhere in your application.
Create a protocol named ColourWatcher
and create static
delegate
on Colours
for the class that will be notified when the color is added.
You have a tableView implementation ColourWatcher
and add yourself as delegate
. Then when the color is added the method newColourAdded
will be called in yours TableViewController
and you can reload the data.
Also, I would recommend making an array of hues just [String]
instead of using options. An empty array will mean that you have no shades.
protocol ColourWatcher: class {
func newColourAdded(colour: Colour)
}
class Colours {
static var allColours: [Colours] = []
static weak var delegate: ColourWatcher?
var colourName: String
var colourShades: [String]
init(colourName: String, colourShades: [String]) {
self.colourName = colourName
self.colourShades = colourShades
Colours.allColours.append(self)
Colours.delegate?.newColourAdded(colour: self)
}
}
class MyTableViewController: UIViewController, ColourWatcher {
func viewDidLoad() {
super.viewDidLoad()
Colours.delegate = self
}
func newColourAdded(colour: Colour) {
// reload table view or just insert a new row
// using the passed in colour
}
}
source to share
Is there a way to create an array of all created color objects? So every time a new color is added, is it automatically added to the array as well?
You can declare one array in your class and add it like
lazy var colorList = {
Colours()
}()
I need an array of colors so that I can auto fill the table rows and then when the user adds a new color, it will automatically add a new row.
func addColor() {
colorList.append(Colours(colourName: "Red", colourShades:
["Crimson", "Cherry", "Rose"]))
colorList.append(Colours(colourName: "blue", colourShades:["Ice",
"Baby", "Royal"]))
}
as soon as you call the above method when you add color and then update the table to display the new color information in the table row.
To give some context, I am trying to develop an IOS application that includes a color table. Then when the user clicks on a color, it will bring them to another table that has shades of that color.
Now in your tableView method cellForrowatindexpath
you just need to access the above array colorlist
and get colorname
and display in your table.
And when the user clicks on a cell, then inside your method didSelecteRowAtIndexpath
loads another class and passes the above array information to colorlist
this class and to display the shade of the color access information colourShades
from colorlist
.
source to share
In my opinion, you want to keep the state throughout the application.
The best way to solve your problem is to create a Singleton instance.
eg.
class ColoursSharedModel {
static let shared : ColoursSharedModel = ColoursSharedModel()
var choosenColors : [Colors] = []
private init() {}
}
Later you have to add
ColoursSharedModel.shared.choosenColors.append(self)
in Color initialization.
source to share
1) Add your own notification
extension NSNotification.Name {
// 1
static let NewColourAdded = NSNotification.Name(rawValue: "NewColourAdded")
}
2) Set the access modifier for the object pool (static variable all
) asprivate(set)
3) Add initialized colors to the object pool
4) Send your notification
class Colours {
// 2
private(set) static var all: [Colours] = []
var name: String
var shades: [String]
init(name: String, shades: [String]) {
self.name = name
self.shades = shades
// 3
Colours.all.append(self)
// 4
NotificationCenter.default.post(
name: .NewColourAdded,
object: self
)
}
}
Using
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(self.onNewColourAdded),
name: .NewColourAdded,
object: nil
)
}
func onNewColourAdded(notification: Notification) {
guard let newColours = notification.object as? Colours else {
return
}
// Do something
}
}
source to share