如何在Swift中制作Dyanamic NSDictonary

时间:2016-08-23 05:55:34

标签: ios swift dynamic nsdictionary

{
   “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”},
   ]
}

您好我想在运行时创建NSDictonary,结构应该如上所述。任何帮助都会得到赞赏。

我得到了解决方案,感谢您的努力

我想做的小改变 因为我对所有这些数据都有模态。

    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)

感谢。

这是模态

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
        }
    }
}

3 个答案:

答案 0 :(得分:2)

将动态值添加到相应的键,它将起作用。

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)

答案 1 :(得分:1)

您可以使用swift Dictionary [String: AnyObject]代替NSMutableDictionary

var data = [[String: AnuObject]]()
for i in 0..<questionArr.count {
    data.append(["QuestionID": questionArr[i], "AnswerID": answerArr[i]])
}        
let dic = [
    "QuizId" : "23566656",
    "StartTime" : "23:30",
    "EndTime" : "23:45",
    "data" : data
]
print(dic)

答案 2 :(得分:0)

希望这会对你有所帮助:

//Contruct the dic like this
var dic: [String: AnyObject] = ["QuizId": "23566656",
                               "StartTime": "23:30",
                               "EndTime": "23:45",
                               "data":[[String: String]](count: 7, repeatedValue: ["QuestionID":"131313131","AnswerID":"1233152"])
                                ]

// And now retrive value like this

let quizId = dic["QuizId"] as! String
let StartTime = dic["StartTime"] as! String
let EndTime = dic["EndTime"] as! String
let data = dic["data"] as! [[String: String]]