gpt4 book ai didi

ios - swift : Calling an init from another init

转载 作者:可可西里 更新时间:2023-11-01 03:29:26 38 4
gpt4 key购买 nike

我有一个带有两个初始化方法的对象。其中一个接受 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(...) 但我遇到了另一个编译器错误。

我该怎么做?

更新 我使用了便利的 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"]))
}

最佳答案

使用方便的初始化!

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"]))
}

关于ios - swift : Calling an init from another init,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31707916/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com