0
public class LessonAssignment {

    private var title : String?
    private var category : String?
    private var week : Int?
    private var day : Int?


    //Title
    public func getTitle() -> String {
        return title!
    }

    public func setTitle(title : String) {
        self.title = title
    }

    //Category
    public func getCategory() -> String {
        return category!
    }

    public func setCategory(category : String) {
        self.category = category
    }

    //Week
    public func getWeek() -> Int {
        return week!
    }

    public func setWeek(week : Int) {
        self.week = week
    }

    //Day
    public func getDay() -> Int {
        return day!
    }

    public func setDay(day : Int) {
        self.day = day
    }

/**
    Returns an array of models based on given dictionary.

    Sample usage:
    let lessonAssignment_list = LessonAssignment.modelsFromDictionaryArray(someDictionaryArrayFromJSON)

    - parameter array:  NSArray from JSON dictionary.

    - returns: Array of LessonAssignment Instances.
*/
    public class func modelsFromDictionaryArray(array:NSArray) -> [LessonAssignment]
    {
        var models = [LessonAssignment]()
        for item in array {
            models.append(LessonAssignment(dictionary: item as! NSDictionary)!)
        }
        return models
    }

/**
    Constructs the object based on the given dictionary.

    Sample usage:
    let lessonAssignment = LessonAssignment(someDictionaryFromJSON)

    - parameter dictionary:  NSDictionary from JSON.

    - returns: LessonAssignment Instance.
*/
     init() { }

    required public init?(dictionary: NSDictionary) {

        title = dictionary["title"] as? String
        category = dictionary["category"] as? String
        week = dictionary["week"] as? Int
        day = dictionary["day"] as? Int
    }


/**
    Returns the dictionary representation for the current instance.

    - returns: NSDictionary.
*/
    public func dictionaryRepresentation() -> NSDictionary {

        let dictionary = NSMutableDictionary()

        dictionary.setValue(self.title, forKey: "title")
        dictionary.setValue(self.category, forKey: "category")
        dictionary.setValue(self.week, forKey: "week")
        dictionary.setValue(self.day, forKey: "day")

        return dictionary
    }

    func toDictionary() -> [String : Any] {
        var dictionary = [String:Any]()
        let otherSelf = Mirror(reflecting: self)

        for child in otherSelf.children {
            if let key = child.label {
                dictionary[key] = child.value
            }
        }
        return dictionary
    }
}



public class LessonPlan {
    private var name : String?
    private var weeks : Int?
    private var days : Int?
    private var hours : Int?
    private var lessonAssignment = [LessonAssignment]()
    private var lessonNote = [LessonNote]()


    //Name
    public func getName() -> String {
        if name == nil {
            return ""
        } else {
            return name!
        }
    }

    public func setName(name : String) {
        self.name = name
    }

    //Weeks
    public func getWeeks() -> Int {
        if weeks == 0 {
            return 0
        } else {
            return weeks!
        }
    }

    public func setWeeks(weeks : Int) {
        self.weeks = weeks
    }

    //Days
    public func getDays() -> Int {
        if days == 0 {
            return 0
        } else {
            return days!
        }
    }

    public func setDays(days : Int) {
        self.days = days
    }

    //Hours
    public func getHours() -> Int {
        if days == 0 {
            return 0
        } else {
            return hours!
        }
    }

    public func setHours(hours : Int) {
        self.hours = hours
    }

    //LessonAssignment
    public func getLessonAssignment() -> [LessonAssignment] {
        return lessonAssignment
    }

    public func setLessonAssignment(lessonAssignment : [LessonAssignment]) {
        self.lessonAssignment = lessonAssignment
    }

    //LessonNote
    public func getLessonNote() -> [LessonNote] {
        return lessonNote
    }

    public func setLessonNote(lessonNote : [LessonNote]) {
        self.lessonNote = lessonNote
    }


/**
    Returns an array of models based on given dictionary.

    Sample usage:
    let lessonPlan_list = LessonPlan.modelsFromDictionaryArray(someDictionaryArrayFromJSON)

    - parameter array:  NSArray from JSON dictionary.

    - returns: Array of LessonPlan Instances.
*/
    public class func modelsFromDictionaryArray(array:NSArray) -> [LessonPlan]
    {
        var models:[LessonPlan] = []
        for item in array
        {
            models.append(LessonPlan(dictionary: item as! NSDictionary)!)
        }
        return models
    }

/**
    Constructs the object based on the given dictionary.

    Sample usage:
    let lessonPlan = LessonPlan(someDictionaryFromJSON)

    - parameter dictionary:  NSDictionary from JSON.

    - returns: LessonPlan Instance.
*/
     init() { }

    required public init?(dictionary: NSDictionary) {

        name = dictionary["name"] as? String
        weeks = dictionary["weeks"] as? Int
        days = dictionary["days"] as? Int
        hours = dictionary["hours"] as? Int

        lessonAssignment = LessonAssignment.modelsFromDictionaryArray(array:dictionary["lessonAssignment"] as! NSArray)

        lessonNote = LessonNote.modelsFromDictionaryArray(array: dictionary["lessonNote"] as! NSArray)
    }


/**
    Returns the dictionary representation for the current instance.

    - returns: NSDictionary.
*/
    public func dictionaryRepresentation() -> NSDictionary {
        let dictionary = NSMutableDictionary()
        dictionary.setValue(self.name, forKey: "name")
        dictionary.setValue(self.weeks, forKey: "weeks")
        dictionary.setValue(self.days, forKey: "days")
        dictionary.setValue(self.hours, forKey: "hours")
        return dictionary
    }

    func toDictionary() -> [String : Any] {
        var dictionary = [String:Any]()
        let otherSelf = Mirror(reflecting: self)

        for child in otherSelf.children {
            print("Child = \(child)")
            print("Child Label = \(String(describing: child.label))")
            print("Child Value = \(child.value)")

            if let key = child.label {
                 dictionary[key] = child.value
            }
        }
        return dictionary
    }
}

public class ServerRequest {

private var lessonPlan : LessonPlan?

init() {
}

//LessonPlan
public func getLessonPlan() -> LessonPlan {
    return lessonPlan!
}

public func setLessonPlan(lessonPlan : LessonPlan) {
    self.lessonPlan = lessonPlan
}

func toDictionary() -> [String : Any] {
    var dictionary = [String:Any]()
    let otherSelf = Mirror(reflecting: self)

    for child in otherSelf.children {
        if let key = child.label {
            dictionary[key] = lessonPlan?.toDictionary()
        }
    }
    return dictionary
}

}

Current Response = ["lessonPlan": ["name": "test", "days": 2, "weeks": 1, "hours": 1, "lessonAssignment": [HSP.LessonAssignment, HSP.LessonAssignment], "lessonNote": []]]

Expected Response = ["lessonPlan": ["name": "test", "days": 2, "weeks": 1, "hours": 1, "lessonAssignment": [["title" : "some value 1", "category" : "some value", "week" : 1, "day" : 2]], "lessonNote": []]]

Instead of the LessinAssignment Object I would like to add the actual values which is array. Any clue how to resolve this. I know i have to add some more logic inside the toDictionary method and based on the key "lessonAssignment" I have to get each array and add the value in the key as final Array.

10
  • Not related but all custom getters and setters in the LessonPlan class are nonsensical. This is Swift!. And why are all properties optional although the setters pass non-optional values? Commented Apr 22, 2017 at 7:02
  • lessonAssignment key is an array of LessonAssignment instances? Commented Apr 22, 2017 at 7:03
  • @vadian Yes we know its swift but we are following getter/setter as our standard practices. Commented Apr 22, 2017 at 7:06
  • @chri Yes, lessonAssignment is array of custom class. Commented Apr 22, 2017 at 7:06
  • and you want a property of that instance per array entry for the lessonAssignment key rather than the whole instance? Commented Apr 22, 2017 at 7:07

1 Answer 1

1

From my comment, something like this:

assignments.map({
  (value: HSP.LessonAssignment) -> NSDictionary in
  return value.toDictionary()
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.