2016-08-09 39 views
2

Ich bekomme ein dynamisches Array, das (Dictionary mit Array) und wollen den Index von Array zu erhöhen und weiter zum nächsten Wörterbuch auf Aktion gehen.Wie Index von Array zu erhöhen, wenn der Wert dynamisch (Swift) ist

Nachdem der Wert in swapLibs Parsing

var currentQuizIndex = 0 
func Dataparsed() { 
    ServiceManager.service(ServiceType.POST, path: urslStr, param: nil, completion: { (sucess, response, error, code) -> Void in 
     if (code == 200) { 
      let QuizData = (swapLibs?.valueForKey("quiz") as! NSArray) 
      let quizData = playInfo.PlayQuizInfo(QuizData[self.currentQuizIndex] as? NSDictionary) 
      self.playQuizArray.addObject(quizData) 
      self.playQuizTitle?.text = quizData.quizQuestion 
      self.playQImage.sd_setImageWithURL(NSURL(string: quizData.quizQImage!)) 
      self.QNumber?.text = "\(self.Qno)/\(self.noofQuestion)" 
     } 
    }) 
} 

Und Die Modal ist

class playInfo: NSObject { 

    var quizId :   String? = "" 
    var quizQId :   String? = "" 
    var quizQImage :  String? = "" 
    var quizQuestion :  String? = "" 
    var quizType :   String? = "" 
    var quizIndex :  String? = "" 


    class func PlayQuizInfo(dict: NSDictionary?) -> playInfo { 

     let Pinfo = playInfo() 
     Pinfo.WrapPlayQuiz(dict) 
     return Pinfo 
    } 

    func WrapPlayQuiz(dict: NSDictionary?) { 
     if dict == nil { 
      return 
     } 
     self.quizId   = dict!.objectForKey("quizId") as? String 
     self.quizIndex  = dict!.objectForKey("index")  as? String 
     self.quizQImage  = dict!.objectForKey("QuesImage")  as? String 
     self.quizQuestion  = dict!.objectForKey("question") as? String 
     self.quizType   = dict!.objectForKey("Type")  as? String 
     self.quizQId   = dict!.objectForKey("questionId")  as? String 
    } 

} 

Hier Struktur

{ 
    "quiz":[ 
     { 
     "quizId":"7295", 
     "QuesImage":"http:\/\/proprofs.com\/api\/ckeditor_images\/man-approaches-woman1(1).jpg", 
     "question":"How do you know him?", 
     "questionId":"216210", 
     "Type":"PQ", 
     "index":4, 
     "keys":[ 
      { 
       "answerId":"8266", 
       "option":"He's in one or more of my classes, and we're good friends.", 
       "AnsImage":"Image Not Available" 
      }, 
      { }, 
      { }, 
      { }, 
      { } 
     ] 
     }, 
     { }, 
     { }, 
     { }, 
     { }, 
     { }, 
     { }, 
     { }, 
     { }, 
     { }, 
     { }, 
     { } 
    ] 
} 

Jedes Wörterbuch wird mit einem Gehalt gleiche Tonart wie oben

Jede Hilfe wird geschätzt. Danke.

+0

Können Sie die Antwortstruktur anzeigen? –

+0

Kommentieren Sie hier, wenn Sie Ihre Frage aktualisieren. –

+0

@ Mr.UB Sie können es überprüfen. Danke –

Antwort

1

Da ich nicht ServiceManager an meinem Ende haben, damit ich diesen Code hypothetisch erstellt haben. Es könnte das Problem lösen, alle Daten in einem Array zu speichern. Es fügt dem Array auch Schlüssel als Objekt hinzu.

EDIT 1: Korrekte QuizKey-Objekt-Array-Bildung. Lassen Sie mich wissen, wenn irgendein Fehler auftritt, da ich es nicht an meinem Ende testen kann.

Edit 2: Ich habe eine allgemeine Viewcontroller seine Arbeit perfectly.Try diese View-Controller-Datei ausgeführt wird gemacht, und Sie werden die Ergebnisse sehen.

class TestVC: UIViewController { 

    //An Array similar to the response you are getting from the server 
    var response:[AnyObject] = [ 
     [ 
      "quizId" : "1111", 
      "QuesImage" : "http://proprofs.com/api/ckeditor_images/man-approaches-woman1(1).jpg", 
      "question" : "How do you know him?", 
      "questionId" : "216210", 
      "Type" : "PQ", 
      "index" : 4, 
      "keys":[ 
       [ 
        "answerId":"8266", 
        "option":"He's in one or more of my classes, and we're good friends.", 
        "AnsImage":"Image Not Available" 
       ], 
       [ 
        "answerId":"8266", 
        "option":"He's in one or more of my classes, and we're good friends.", 
        "AnsImage":"Image Not Available" 
       ], 
       [ 
        "answerId":"8266", 
        "option":"He's in one or more of my classes, and we're good friends.", 
        "AnsImage":"Image Not Available" 
       ] 
      ] 
     ], 
     [ 
      "quizId" : "2222", 
      "QuesImage" : "http://proprofs.com/api/ckeditor_images/man-approaches-woman1(1).jpg", 
      "question" : "How do you know him?", 
      "questionId" : "216210", 
      "Type" : "PQ", 
      "index" : 4, 
      "keys":[ 
       [ 
        "answerId":"8266", 
        "option":"He's in one or more of my classes, and we're good friends.", 
        "AnsImage":"Image Not Available" 
       ], 
       [ 
        "answerId":"8266", 
        "option":"He's in one or more of my classes, and we're good friends.", 
        "AnsImage":"Image Not Available" 
       ], 
       [ 
        "answerId":"8266", 
        "option":"He's in one or more of my classes, and we're good friends.", 
        "AnsImage":"Image Not Available" 
       ] 
      ] 
     ] 
    ] 

    var playQuizArray:[playInfo]! = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     print(response) 

     for dict in response { 
      self.playQuizArray.append(playInfo.PlayQuizInfo(dict as? [String:AnyObject])) 
     } 

     print(self.playQuizArray) 

     let quiz = self.playQuizArray[0] 
     print("quizId \(quiz.quizId)") 
     print("keyAnswerId \(quiz.quizKeys![0].keyAnswerId)") 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

class playInfo: NSObject { 
    var quizId :   String? = "" 
    var quizQId :   String? = "" 
    var quizQImage :  String? = "" 
    var quizQuestion :  String? = "" 
    var quizType :   String? = "" 
    var quizIndex :  String? = "" 
    //quizKeys will contain quiz array 
    var quizKeys :  [QuizKey]? = [] 

    class func PlayQuizInfo(dict: [String:AnyObject]?) -> playInfo { 

     let Pinfo = playInfo() 
     Pinfo.WrapPlayQuiz(dict) 
     return Pinfo 
    } 

    func WrapPlayQuiz(dict: [String:AnyObject]?) { 
     if dict == nil { 
      return 
     } 
     self.quizId   = dict!["quizId"] as? String 
     self.quizIndex  = dict!["index"] as? String 
     self.quizQImage  = dict!["QuesImage"] as? String 
     self.quizQuestion  = dict!["question"] as? String 
     self.quizType   = dict!["Type"] as? String 
     self.quizQId   = dict!["questionId"] as? String 

     //add key object array to the quizKeys 
     if let arrKeys = dict!["keys"] as? [AnyObject] { 
      for arr in arrKeys { 
       let key:QuizKey = QuizKey.QuizKeyInfo(arr as? [String : AnyObject]) 
       self.quizKeys?.append(key) 
      } 
     } 
    } 

} 

class QuizKey: NSObject { 
    var keyAnswerId :  String? = "" 
    var keyOption :   String? = "" 
    var keyAnsImage :  String? = "" 

    class func QuizKeyInfo(dict: [String:AnyObject]?) -> QuizKey { 
     let QKeys = QuizKey() 
     QKeys.WrapQuizKeys(dict) 
     return QKeys 
    } 

    func WrapQuizKeys(dict: [String:AnyObject]?) { 
     if dict == nil { 
      return 
     } 

     self.keyAnswerId  = dict!["answerId"] as? String 
     self.keyOption  = dict!["option"]  as? String 
     self.keyAnsImage  = dict!["AnsImage"]  as? String 
    } 

} 
+0

Arbeits Fine @ Mr.UB Könntest du mich bitte wissen lassen, wie man den Index für Action oder Button Pressed erhöht. Durch den Weg Danke. Für die Mühe –

+0

Dict in append (Key) in nächsten qkeys.wrapQuizKeys (Dict) erhalten bekommen nil –

+0

Sie meine diese Zeile? 'QKeys.WrapQuizKeys (dict)' –