我有一个带有两个init方法的对象。其中一个接受NSDictionary,另一个接收大量的String变量。我想调用NSDictionary init,然后从那里将我的字典转换为字符串并使用我的字符串调用其他init。这是我的代码:
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(...)
,但我得到了另一个编译器错误。
我该怎么做?
UPDATE 我使用了一个方便的init,但现在我遇到了一个新问题。这是我的新代码:
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"])
}
,编译错误为() is not convertible to 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"]))
}
答案 0 :(得分:8)
使用便利初始化!
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"]))
}