我是Swift的新手并试图创建和使用一个类,但是我收到一个错误:“在调用中缺少参数'time'的参数” 有人能指出我正确的方向吗?我在Apple网站上看了几个样本,但没有运气。 任何建议将不胜感激。 谢谢! 布赖恩
这是我的班级:
class CurrentSevereReportModel {
var time: NSDate?
var magnitude: String?
var location: String?
var county: String?
var remarks: String?
var lat: NSNumber?
var lon: NSNumber?
var type: NSNumber?
var reportid: NSNumber?
init(time: NSDate, magnitude: String, location: String, county: String, remarks: String, lat: NSNumber, lon: NSNumber, type: NSNumber, reportid: NSNumber) {
self.time = time
self.magnitude = magnitude
self.location = location
self.county = county
self.remarks = remarks
self.lat = lat
self.lon = lon
self.type = type
self.reportid = reportid
}
}
以下是我尝试使用它并获取错误的方法:“在调用中缺少参数'time'的参数”
var records: [CurrentSevereReportModel]
func buildRecord(data : [String : String], type : Int, index:Int)
{
let record = CurrentSevereReportModel() // <-- error here
var timeDate = data[reportHeaders[0]]!
let length = timeDate.characters.count
if (length > 4)
{
let index1 = timeDate.endIndex.advancedBy(-4)
timeDate = timeDate.substringFromIndex(index1)
}else if (length < 4)
{
let restCount = 4 - length
var index: Int = 0
for index = 0; index < restCount; index++
{
timeDate = "0" + timeDate
}
}
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyMMddHHmm"
record.time = dateFormatter.dateFromString(timeDate)
record.type = type
record.magnitude = data[reportHeaders[1]]!
record.location = data[reportHeaders[2]]!
record.county = data[reportHeaders[3]]!
record.remarks = data[reportHeaders[4]]!
record.lat = Double(data[reportHeaders[5]]!)
record.lon = Double(data[reportHeaders[6]]!)
record.comments = data[reportHeaders[7]]!
record.reportid = index
print(record.reportid)
records.append(record)
}
答案 0 :(得分:2)
我真的不认为以这种方式进行初始化是一个好主意。 几个论点:
如果你真的需要这种行为(我强烈建议反对),并且你正在寻找摆脱错误,只需创建空初始化器:
init() {}
它会完成工作。
答案 1 :(得分:0)
您的初始值设定项已定义:
init(time: NSDate, magnitude: String, location: String, county: String,
remarks: String, lat: NSNumber, lon: NSNumber, type: NSNumber,
reportid: NSNumber) {
这意味着您必须以定义的方式调用:
let record = CurrentSevereReportModel(time: someDate, magnitude: someString,
// ... and so on ...
)