2017-04-22 2 views
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 {JSON von Object mit Array-Problem in iOS Swift 3

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 
} 

}

Aktuelle Response = [ "Unterrichtsplanung": [ "name": "test" "Tage": 2, "Woche": 1, "Stunden": 1, "lessonAssignment": [HSP.LessonAssignment, HSP.LessonAssignment] "lessonNote": []]]

Erwartete Antwort = ["lessonPlan": ["name": "test", "tage": 2, "wochen": 1, "stunden": 1, "lektionAssignment": [["titel"] : "1 einen gewissen Wert", "Kategorie": "some value", "Woche": 1, "Tag": 2]] "lessonNote": []]]

Anstelle des LessinAssignment Objekt I möchte die tatsächlichen Werte hinzufügen, die Array ist. Irgendein Hinweis, wie man das löst. Ich weiß, dass ich mehr Logik innerhalb der toDictionary Methode hinzufügen muss und basierend auf dem Schlüssel "lectionAssignment" muss ich jedes Array erhalten und den Wert im Schlüssel als finales Array hinzufügen.

+0

nicht verwandt, aber alle benutzerdefinierten Getter und Setter in der 'LessonPlan' Klasse sind unsinnig. Das ist schnell !. Und warum sind alle Eigenschaften optional, obwohl die Setter nicht-optionale Werte übergeben? – vadian

+0

lektionAusrichtungsschlüssel ist ein Array von LessonAssignment-Instanzen? – chris

+0

@vadian Ja, wir wissen, es ist schnell, aber wir folgen Getter/Setter als unsere Standard-Praktiken. – Scorpion

Antwort

1

Von meinem Kommentar, so etwas wie dieses:

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

Danke, es hat funktioniert :) – Scorpion