gpt4 book ai didi

ios - iOS Swift 3 中来自对象的 JSON 存在数组问题

转载 作者:行者123 更新时间:2023-11-29 00:23:56 25 4
gpt4 key购买 nike

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)类 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": []]]

我想添加作为数组的实际值,而不是 LessinAssignment 对象。任何线索如何解决这个问题。我知道我必须在 toDictionary 方法中添加更多逻辑,并且基于键“lessonAssignment”,我必须获取每个数组并将键中的值添加为最终数组。

最佳答案

根据我的评论,是这样的:

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

关于ios - iOS Swift 3 中来自对象的 JSON 存在数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43556259/

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