Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

ios - how to make Dyanamic NSDictonary in Swift

{
   “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

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

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...