簡體   English   中英

iOS Swift 3中來自對象的JSON與數組問題

[英]JSON from Object with array issue in iOS Swift 3

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

公共類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
}

}

當前響應= [“ lessonPlan”:[“ name”:“ test”,“ days”:2,“ weeks”:1,“ hours”:1,“ lessonAssignment”: [HSP.LessonAssignment,HSP.LessonAssignment] ,“ lessonNote“:[]]

預期響應 = [“ lessonPlan”:[“ name”:“ test”,“ days”:2,“ weeks”:1,“ hours”:1,“ lessonAssignment”:[[“ title”:“ some value 1” ,“ category”:“某些值”,“ week”:1,1,“ day”:2]],“ lessonNote”:[]]]

我想代替LessinAssignment對象添加實際值,即數組。 任何線索如何解決這個問題。 我知道我必須在toDictionary方法內部添加更多邏輯,並且基於鍵“ lessonAssignment”,我必須獲取每個數組並將鍵中的值添加為最終數組。

根據我的評論,像這樣:

assignments.map({
  (value: HSP.LessonAssignment) -> NSDictionary in
  return value.toDictionary()
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM