Get and Set Array in Swift Class

2019-09-16 14:14发布

问题:

I was build an IOS application, and somehow I have to do an aggregation in swift class, but every time I want to get the data, it always return an error. it seems like I the data always return a nil result.

I want to push the DoctorList member object (schedule) in the view controller class I have create the object, and I also called the init() function. but, however, when I push the (or call) the init function for the DoctorList class and pass the array of ScheduleList, the content of the schedule member in doctorlist class will always be empty.

so when I try to get the schedule, it will return a nil result.

can every one tell me what I did wrong, because I already change the code but it still give a nil result.

I have two class like this

class DoctorList: NSObject {
var DoctorID: String?
var DoctorName: String?
var SpecialtyID: String?
var SpecialtyName: String?
var ImageURL: String?
var Schedule: [ScheduleList]?

init(_ DoctorID:String, _ DoctorName:String, _ SpecialtyID:String, _ SpecialtyName:String, _ ImageUrl:String, _ Schedule:[ScheduleList] ){
    self.DoctorID = DoctorID
    self.DoctorName = DoctorName
    self.SpecialtyID = SpecialtyID
    self.SpecialtyName = SpecialtyName
    self.ImageURL = ImageUrl
    for sc in Schedule {
        self.Schedule?.append(ScheduleList(sc.DoctorID!, sc.DayName!, sc.FirstHour!, sc.LastHour!))
    }

}

var getSchedule: [ScheduleList] {
    get {
        return self.Schedule!
    }

}

and this one

class ScheduleList: NSObject {
var DoctorID: String?
var DayName: String?
var FirstHour: String?
var LastHour: String?

init(_ DoctorID:String, _ DayName:String, _ FirstHour:String, _ LastHour:String ){
    self.DoctorID = DoctorID
    self.DayName = DayName
    self.FirstHour = FirstHour
    self.LastHour = LastHour
}

the return value for the schedule was always empty

I'm sorry, could anyone give a suggestion how to make a global variable in swift?

回答1:

You haven't initialized the Schedule array.

The append statement in the loop just never execute:

for sc in Schedule {
    // self.Schedule is nil so anything come after the question mark does not run
    self.Schedule?.append(ScheduleList(sc.DoctorID!, sc.DayName!, sc.FirstHour!, sc.LastHour!))
}

To fix it initialize your array before use:

self.Schedule = [ScheduleList]()
for sc in Schedule {
    self.Schedule?.append(ScheduleList(sc.DoctorID!, sc.DayName!, sc.FirstHour!, sc.LastHour!))
}

Also, your code is a pain to read:

  • Optionals everywhere! You should decide what properties can and cannot be nil and get rid of the unnecessary ? and !
  • The convention in Swift is lowerCamelCase for variable names, and CamelCase for class names
  • No need to inherit from NSObject unless you want something from the ObjC world, whether working with ObjC or use KVO


回答2:

Just noticed, recommendations from @CodeDifferent should be appropriate and helpful. It is because you haven't initialised the Schedule in DoctorList

init(_ DoctorID:String, _ DoctorName:String, _ SpecialtyID:String, _ SpecialtyName:String, _ ImageUrl:String, _ Schedule:[ScheduleList] ){
        self.DoctorID = DoctorID
        self.DoctorName = DoctorName
        self.SpecialtyID = SpecialtyID
        self.SpecialtyName = SpecialtyName
        self.ImageURL = ImageUrl

        // Use this for init an empty array and append the content
        self.Schedule = [ScheduleList]()
        self.Schedule?.append(contentsOf: Schedule)
    }

An example of the result:

let schedule = ScheduleList("scheduleId", "name", "1st", "last")
let doctorList = DoctorList("docId", "docName", "specId", "scpecName", "imgURL", [schedule])
if let list = doctorList.Schedule {
    print("\(list[0].DoctorID)")
}


标签: swift class oop