在Swift类中获取和设置数组 [英] Get and Set Array in Swift Class

查看:80
本文介绍了在Swift类中获取和设置数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个IOS应用程序,以某种方式必须在swift类中进行聚合,但是每次我想要获取数据时,它总是返回错误。似乎我的数据总是返回nil结果。

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.

我想在我创建的视图控制器类
中推送DoctorList成员对象(计划)对象,我也调用了init()函数。但是,但是,当我推送(或调用)DoctorList类的init函数并传递ScheduleList数组时,doctorlist类中的schedule成员的内容将始终为空。

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.

所以当我尝试获取时间表时,它将返回nil结果。

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.

我有两个这样的类

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!
    }

}

还有一个

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
}

计划的返回值始终为空

对不起,有人可以建议如何快速创建全局变量吗?

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

推荐答案

您尚未初始化 Schedule 数组。

循环中的追加语句永远不会执行:

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:


  • 随处可见!您应该确定哪些属性可以为零,不能为零,并摆脱不必要的

  • Swift中的约定是变量名 lowerCamelCase ,类名 CamelCase

  • 无需继承 NSObject ,除非您想要从ObjC世界中获取某些东西,无论是与ObjC一起使用还是使用KVO

  • 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

这篇关于在Swift类中获取和设置数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆