Hello all,
Basically I am developing a SAT prep application where there is Task by Task solutions for Questions that users can follow. Some of the Tasks are interactive where user can tap on the screen, and some of them are just read only. I am trying to follow protocol oriented programming approach and here's how I structured it:
protocol Task {
var canInteract: Bool { get }
var highlightText: String { get }
var qwzrdText: String { get }
}
extension Task where Self: Interactable {
var canInteract: Bool { return true }
}
protocol Interactable {
var tapPoint: CGPoint { get }
}
struct SimpleTask: Task {
let canInteract = false
let highlightText: String
var qwzrdText: String { return "Simple Task" }
}
struct InteractiveTask: Task, Interactable {
let highlightText: String
var qwzrdText: String { return "Interactive Task" }
var tapPoint: CGPoint { return CGPointMake(100, 200) }
}
struct Solution {
var tasks:[Task]
mutating func addTask(newTask: Task) {
tasks.append(newTask)
}
}
var solution = Solution(tasks: [])
let interactiveTask = InteractiveTask(highlightText: "InteractiveTask")
solution.addTask(interactiveTask)
let simpleTask = SimpleTask(highlightText: "SimpleTask")
solution.addTask(simpleTask)
So I let's assume I'd like to to use above solution array as my datasource in my viewcontroller. What I am looking for is a declarative way to update the label that I bind to the above datasource on change. I know property observer is what I am looking for. What I don't know, should I have a generic variable in the viewcontroller and assign the current Task and update that? If this is the case, how am I going to address the heteragonious types that I have?
//UIViewController
var label = UILabel(frame: CGRectMake(0, 0, 200, 200))
var task = SimpleTask(highlightText: "Test") { //addresses SimpleTask but not InteractiveTask?
didSet {
label.text = simpleTask.highlightText
}
}
func nextTapped() {
solution[whatever] = task
}
Thank you.