我在使用Swift进行编程时遇到了问题。在我创建了SchedulePatientsTest类的实例后,我开始收到此错误:无法使用类型为'的参数列表调用'下标'(名称:$ T4,电子邮件:$ T9,ssn:$ T14,dob:$ T19)'
因为我对Swift很陌生,所以我在调试它时遇到了麻烦,谷歌搜索并没有多大帮助。我知道它抱怨阵列的下标,但我不明白为什么。谢谢你的帮助!
import UIKit
struct SchedulePatientsTest {
let patientNames = [ "John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr" ]
let patientEmails = [ "jlennon@beatles.com", "pmccartney@another.edu","george@something.com","drummer@beatles.org"]
let patientSSN = [ "123-45-6789","333-22-1123","567-39-9282","888-88-8888"]
let patientBirthDates = [ "05/01/1951", "06/23/1964","09/05/1955", "02/28/2001"]
let doctorNames = [ "John Smith", "Jane Doe", "Mary Jones", "Beth Garcia"]
let doctorEmails = [ "John Smith", "Jane Doe", "Mary Jones", "Beth Garcia" ]
let doctorSSN = [ "445-25-3382", "582-10-9983", "180-38-0038", "343 - 34-9875" ]
let doctorBirthDates = [ "06/03/1941", "10/10/1950", "12/25/1965", "07/19/1964" ]
let specialty = [ medicalSpecialties.GENERAL_MEDICINE, medicalSpecialties.PEDIATRICS, medicalSpecialties.GENERAL_MEDICINE, medicalSpecialties.ONCOLOGY]
init()
{
}
}
class Patient {
var name : String = ""
var email : String = ""
var ssn : String = ""
var dob : NSDate = NSDate()
init(name: String, email : String, ssn : String, dob : NSDate) {
self.name = name
self.email = email
self.ssn = ssn
self.dob = dob
}
func getName()->(String)
{
return(name)
}
}
class Doctor : Patient {
var specialty : medicalSpecialties
init(specialty: medicalSpecialties,name: String, email : String, ssn : String, dob : NSDate) {
self.specialty = specialty
super.init(name: name, email: email, ssn: ssn, dob: dob)
}
}
enum medicalSpecialties {
case GENERAL_MEDICINE
case PEDIATRICS
case ONCOLOGY
}
let CMYK = SchedulePatientsTest()
let P1 = Patient(name: CMYK.patientNames[0], email: CMYK.patientEmails[0], ssn: CMYK.patientSSN[0], dob: CMYK.patientBirthDates[0])
答案 0 :(得分:1)
这是一个模糊的错误消息,但是因为您将一个字符串而不是NSDate传递给Patient初始化程序而发生错误。
let patientBirthDates = [ "05/01/1951", ...]
创建一个String对象数组,而不是日期对象。 Patient初始值设定项需要NSDate
个dob
参数。
字符串与NSDate
代码["05/01/1951", "06/23/1964"]
不会创建NSDate对象数组,即使该变量定义为patientBirtDates: [NSDate]
也是如此。这是一个字符串文字数组。这些字符串文字被转换为'进入String
个对象。如果要创建NSDate对象数组,则需要实际实例化NSDate对象:
let patientBirthDates = [
NSDate(timeIntervalSince1970: 100),
NSDate(timeIntervalSince1970: 200),
NSDate(timeIntervalSince1970: 300)
]
您使用哪种创建NSDate的方法取决于您,请查看available ininitializers。