I have a RadioStation class file and a view controller where I create two station objects based on this class, one for FM and one for AM.
I created both of these station objects from within the same required init method. I'm not sure this was this the best way to do this, for example can you imagine if you wanted 8 such objects, this would just get very clunky. Is the issue rather that I am creating them in the view controller, and should I have in fact created separate subclass files for each and init them in those?
Or should I have created two separate init methods, one for each new object?
RadioStation Class File: RadioStation.swift
import UIKit
class RadioStation: NSObject {
var name: String
var frequency: Double
override init() { //init class method to set default values.
name = "Default"
frequency = 100
}
static var minAMFFrequency: Double = 520.0
static var maxAMFFrequency: Double = 1610.0
static var minFMFFrequency: Double = 88.3
static var maxFMFFrequency: Double = 107.9
func isBandFM() -> Int {
if frequency >= RadioStation.minFMFFrequency && frequency <= RadioStation.maxAMFFrequency {
return 1 //FM
} else {
return 0 //AM
}
}
}
View Controller Class
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var stationName: UILabel!
@IBOutlet weak var stationFrequency: UILabel!
@IBOutlet weak var stationBand: UILabel!
var myStation: RadioStation
var myStationAM: RadioStation //second object variable
required init?(coder aDecoder: NSCoder) {
myStation = RadioStation()
myStationAM = RadioStation()
myStation.frequency = 104.7
myStationAM.frequency = 600.2
myStation.name = "FM1"
myStationAM.name = "AM1"
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func buttonClick(_ sender: Any) {
stationName.text = myStation.name //set top left label text to name property of myStation object instance
stationFrequency.text = "\(myStation.frequency)"
if myStation.isBandFM() == 1 {
stationBand.text = "FM'"
} else {
stationBand.text = "AM"
}
}
@IBAction func buttonClickAM(_ sender: Any) {
stationName.text = myStationAM.name
stationFrequency.text = "\(myStationAM.frequency)"
if myStationAM.isBandFM() == 1 {
stationBand.text = "FM"
} else {
stationBand.text = "AM"
}
}
}