No worries! On the bright side, you just learned first hand what motivates code conventions. If struct task
was named according to Swift's convention of using UpperCamelCase
for types, @Joanna_Carter wouldn't have has to ask what type Task
is.
- The names got you all mixed up. You were subscripting
task
(task[indexPath.row]
), which was subscripting yourstruct task
(which was incorrectly named)- which as a formal error because your struct doesn't define a static subscript operator,
- but it's also a logical error, because you actually meant to be subscripting your array of tasks, which you called
Task
. That's an incorrect name, because:- it's
UpperCamelCase
(identifiers of values should belowerCamelCase
), and - it's singular, yet it was an array of a plurality of structs.
- it's
- The "task" in
taskName
andtaskPoints
instruct Task
are redundant. We know they belong toTask
already. - Task points should be a numeric type, probably
Int
but possiblyDouble
/Decimal
(if you need fractional numbers of points). - You should format the
task.points
field according to the correct locale for the user.
import UIKit
struct Task {
var taskName: String
var taskPoints: Int
}
class TasksTableViewCell: UITableViewCell {
@IBOutlet weak var taskName: UILabel!
@IBOutlet weak var taskPoints: UILabel!
}
func formatQuantity(_ i: Int) -> String {
let nf = NumberFormatter() // Cache this if necessary for performance
nf.usesGroupingSeparator = true
nf.locale = Locale.current
return nf.string(from: NSNumber(value: i)!
}
class AddTaskTableViewController: UITableViewController { // Make sure you update the name in your story board to be capitalized
var tasks = [
Task(name: "Plant a tree", points: 100),
task(name: "Turn off all unused lights", points: 25),
task(name: "Recycle all the recyclables", points: 25),
]
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskTableViewCell", for: indexPath)
as! TasksTableViewCell
let tasks = tasks[indexPath.row]
cell.taskName?.text = tasks.name
cell.taskPoints?.text = formatQuantity(tasks.points)
return cell
}
}
P.S. In "Closing the lights" it's a regionalism used by some Francophone English speakers, such as in Quebec, but the correct phrase it "Turn off all unused lights".
P.P.S. If it's recyclable, then it's not trash. Idk what the correct term is, but around here (Toronto, Canada) we say "Recycle all recyclables".