ios - Swift: Properly initialize and access my classes in an array -


this swift playground; want collect 'task' class instances in array each task's properties can used in tableview. i'm searching documentation don't see discussion of how initialize , access classes in array.

the last line of code gives error reads anyobject not have member named deadline. it's same whether use swift array of anyobjects, or empty nsmutablearray. tried various ways of casting integer, insists on error. going wrong way store , access data? i'm javascript-brained, similarity of syntax leads me delusions.

import uikit  let todays_date = nsdate() // let cal = nscalendar.currentcalendar() let day_number = cal.ordinalityofunit(.calendarunitday, inunit: .calendarunityear, fordate: todays_date)  var tasks = [] nsmutablearray  class task {      var title: string     var interval: int     var dateactivated: int     var deadline: int     var flag_count: int = 0     var isactivated: bool = false      init(title: string, dateactivated: int, interval: int) {          //initialized selecting new         self.title = title         self.dateactivated = dateactivated         self.interval = interval          //calculated         self.deadline = dateactivated + interval     }  }  var task1 = task(title: "laundry", dateactivated: day_number, interval: 7) var task2 = task(title: "dishes", dateactivated: day_number, interval: 7)  task1.deadline task2.flag_count  tasks.addobject(task1) tasks.addobject(task2)  tasks[0].deadline  //anyobject not have member named deadline 

no need use foundation arrays here, declare array [task] (after declaring task class):

var tasks = [task]() 

then use append instead of addobject:

tasks.append(task1) tasks.append(task2)  tasks[0].deadline // 83 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

ios - Possible to get UIButton sizeThatFits to work? -