Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Calling an init from another init

Tags:

ios

init

I have an object with two init methods. One of them takes in a NSDictionary, and the other takes in a whole ton of String variables. I want to call the NSDictionary init, and from there convert my dictionary into Strings and call the other init with my Strings. Here is my code:

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
    }
}

However, I cannot build my project, because "Cannot assign to 'self' in method"

I've tried also calling init(...) instead of Event(...) but I get another compiler error.

How can I do this?

UPDATE I used a convenience init, but now I have a new problem. Here is my new code:

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

and the compiler error is () is not convertible to StringLiteralConvertible

UPDATE 2 I fixed it. Here is the final code in case anyone needs it:

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"]))
}
like image 375
Jonathan Allen Grant Avatar asked Jul 29 '15 18:07

Jonathan Allen Grant


People also ask

What is .init in Swift?

An initializer is a special type of function that is used to create an object of a class or struct. In Swift, we use the init() method to create an initializer.

What is a convenience init Swift?

Convenience initializers are secondary, supporting initializers for a class. You can define a convenience initializer to call a designated initializer from the same class as the convenience initializer with some of the designated initializer's parameters set to default values.

What is Failable initializer in Swift?

In a failable initializer, return nil indicates that initialization has failed; no other value can be returned. In the example, failure occurs when the string could not be parsed as an integer. Otherwise, self is initialized to the parsed value.


1 Answers

Use a convenience init!

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"]))
}
like image 153
Jonathan Allen Grant Avatar answered Oct 03 '22 00:10

Jonathan Allen Grant