Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

{
   “QuizId” : “23566656”
   “StartTime”:”23:30”
   “EndTime”:”23:45”
   “data”:[
      {“QuestionID”:”131313131”,“AnswerID”:“1233152”},
      {“QuestionID”:”131313141”,“AnswerID”:“1233152”},
      {“QuestionID”:”131313151”,“AnswerID”:“1233152”},
      {“QuestionID”:”131313161”,“AnswerID”:“1233152”},
      {“QuestionID”:”131313171”,“AnswerID”:“1233152”},
      {“QuestionID”:”131313181”,“AnswerID”:“1233152”},
      {“QuestionID”:”131313191”,“AnswerID”:“1233152”},
      {“QuestionID”:”131313101”,“AnswerID”:“1233152”},
   ]
}

Hello I want to Create a NSDictonary on Runtime which Structure Should Be Like above. any Help Will Appreciated Thanks.

I Got Solution Thanks For That Effort

Little Change I want to Make As i Have Modal For all Those Data.

    let info = self.playQuizArray[currentQuizIndex] as! playInfo
    let Ainfo = info.answers[indexPath.row] as! AnswerInfo
    let QuesID = info.quizQId
    for i in 0..<playQuizArray.count {
        data.append(["QuestionID": QuesID[i], "AnswerID": playQuizArray[i]]) // For Answer Ainfo.ansID  
    }
    let dic = [
        "QuizId" : "23566656",// info.quizID
        "StartTime" : "23:30",
        "EndTime" : "23:45",
        "data" : data
    ]
    print(dic)

Thanks.

Here Is The Modal

class AnswerInfo: NSObject {

    var ansId  :String? = ""
    var ans    :String? = ""
    var ansImg :String? = ""

    class func infoWith(dict: NSDictionary?) -> AnswerInfo {
        let anInfo = AnswerInfo()
        anInfo.wrapInfo(dict)
        return anInfo
    }


    func wrapInfo(dict: NSDictionary?)  {
        if dict == nil {
            return
        }
        self.ansId     = dict!.objectForKey("answerId")  as? String
        self.ans  = dict!.objectForKey("AnsImage")  as? String
        self.ansImg = dict!.objectForKey("option")    as? String
    }
}

class playInfo: NSObject {

    var quizId :           String? = ""
    var quizQId :          String? = ""
    var quizQuestion :     String? = ""
    var answers = NSArray()



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.quizQuestion     = dict!.objectForKey("question")  as? String
        self.quizQId          = dict!.objectForKey("questionId")as? String

        if let answer = dict?.objectForKey("keys") as? NSArray {

        let AnsArray = NSMutableArray()
            for ans in answer {
            let ansInfo = AnswerInfo.infoWith(ans as? NSDictionary)
                AnsArray.addObject(ansInfo)
            }
            self.answers = AnsArray
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
239 views
Welcome To Ask or Share your Answers For Others

1 Answer

Add dynamic values to the corresponding keys and it will work.

let dynamicDictionary = NSMutableDictionary()
        dynamicDictionary.setValue("23566656", forKey: "QuizId")
        dynamicDictionary.setValue("23:30", forKey: "StartTime")
        dynamicDictionary.setValue("23:45", forKey: "EndTime")

        let arrayOfDictionaries = NSMutableArray()
        for _ in 0..<4{
            let questionAndAnswer = NSMutableDictionary()
            questionAndAnswer.setValue("131313151", forKey: "QuestionID")
            questionAndAnswer.setValue("1233152", forKey: "AnswerID")
            arrayOfDictionaries.addObject(questionAndAnswer)

        }

        dynamicDictionary.setValue(arrayOfDictionaries, forKey: "data")
        print(dynamicDictionary)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...