我有一个对象有两个初始化方法。其中一个接受NSDictionary,另一个接受大量的String变量。我想调用NSDictionary的初始化方法,然后将我的字典转换为字符串,并使用这些字符串调用另一个初始化方法。下面是我的代码:
init(data: NSDictionary) {
self = Event(data["event_id"] as! String, eventName: data["event_name"] as! String, eventDescription: data["description"] as! String, eventCategories: data["categories"] as! [String], eventSecurity: data["event_security"] as! String, authorUserID: data["author_user_id"] as! String, timestampCreated: data["timestamp_created"] as! String, timestampUpdated: data["timestamp_updated"] as! String, startDateTime: data["start_date_time"] as! String, endDateTime: data["end_date_time"] as! String, location: Location(streetAddress: data["location_street_address"] as! String, city: data["location_city"] as! String, state: data["location_state"] as! String, zipCode: data["location_zipcode"] as! String, countryCode: data["location_country_code"] as! String), reoccurDateStart: data["reoccur_date_start"] as! String, reoccurDateEnd: data["reoccur_date_end"] as! String, reoccurDaysOfWeek: data["reoccur_days_of_week"] as! String, eventType: data["categories"] as! String)
}
init(eventID: String, eventName: String, eventDescription: String, eventCategories: [String], eventSecurity: String, authorUserID: String, timestampCreated: String, timestampUpdated: String, startDateTime: String, endDateTime: String, location: Location, reoccurDateStart: String, reoccurDateEnd: String, reoccurDaysOfWeek: String, eventType: String){
self.eventID = eventID
self.eventName = eventName
self.eventDescription = eventDescription
self.eventCategories = eventCategories
self.eventSecurity = eventSecurity
self.authorUserID = authorUserID
self.timestampCreated = timestampCreated
self.timestampUpdated = timestampUpdated
self.startDateTime = startDateTime
self.endDateTime = endDateTime
self.location = location
self.reoccurDateStart = reoccurDateStart
self.reoccurDateEnd = reoccurDateEnd
self.reoccurDaysOfWeek = reoccurDaysOfWeek
self.eventType = eventType
setUpCustomVariables()
}
func setUpCustomVariables() {
let form = NSDateFormatter()
form.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
form.timeZone = NSTimeZone(forSecondsFromGMT: 0)
startDate = form.dateFromString(startDateTime)!
endDate = form.dateFromString(endDateTime)!
startTime = 800
endTime = 815
form.dateStyle = NSDateFormatterStyle.NoStyle
form.timeStyle = NSDateFormatterStyle.ShortStyle
niceStartTime = form.stringFromDate(startDate!)
form.dateFormat = "yyyyMMddHHmm"
let startDateIntString: String = form.stringFromDate(startDate!)
startDateInt = Int(startDateIntString)
form.dateFormat = "yyyyMMdd"
startDateIntJustDate = Int(form.stringFromDate(startDate!))
form.timeStyle = NSDateFormatterStyle.NoStyle
form.dateStyle = NSDateFormatterStyle.LongStyle
niceStartDate = form.stringFromDate(startDate!)
typeOfRepeat = "One-Time Event"
if eventType == "Phone" {
eventTypeNumber = 1
} else if eventType == "Video" {
eventTypeNumber = 2
} else if eventType == "Location" {
eventTypeNumber = 3
} else {
eventTypeNumber = 4
}
}
然而,我无法构建我的项目,因为出现了“在方法中无法分配给'self'”的错误。
我也尝试过调用init(...)
而不是Event(...)
,但我得到了另一个编译器错误。
我该怎么办?
更新 我使用了方便的初始化程序,但现在我有一个新问题。这是我的新代码:
convenience init(data: NSDictionary) {
self.init(data["event_id"], eventName: data["event_name"], eventDescription: data["description"], eventCategories:data["categories"], eventSecurity:data["event_security"], authorUserID:data["author_user_id"], timestampCreated:data["timestamp_created"], timestampUpdated:data["timestamp_updated"], startDateTime:data["start_date_time"], endDateTime:data["end_date_time"], location: Location(streetAddress:data["location_street_address"], city:data["location_city"], state:data["location_state"], zipCode:data["location_zipcode"], countryCode:data["location_country_code"]), reoccurDateStart:data["reoccur_date_start"], reoccurDateEnd:data["reoccur_date_end"], reoccurDaysOfWeek:data["reoccur_days_of_week"], eventType:data["categories"])
}
编译器报错:() 不能转换为 StringLiteralConvertible
更新2:我已经修复了。这是最终的代码,以防有人需要:
convenience init(data: NSDictionary) {
self.init(eventID: String(data["event_id"]), eventName: String(data["event_name"]), eventDescription: String(data["description"]), eventCategories: [String(data["categories"])], eventSecurity: String(data["event_security"]), authorUserID: String(data["author_user_id"]), timestampCreated: String(data["timestamp_created"]), timestampUpdated: String(data["timestamp_updated"]), startDateTime: String(data["start_date_time"]), endDateTime: String(data["end_date_time"]), location: Location(streetAddress: String(data["location_street_address"]), city: String(data["location_city"]), state: String(data["location_state"]), zipCode: String(data["location_zipcode"]), countryCode: String(data["location_country_code"])), reoccurDateStart: String(data["reoccur_date_start"]), reoccurDateEnd: String(data["reoccur_date_end"]), reoccurDaysOfWeek: String(data["reoccur_days_of_week"]), eventType: String(data["categories"]))
}