如何在 Swift 中制作动态 NSDictionary

Posted

技术标签:

【中文标题】如何在 Swift 中制作动态 NSDictionary【英文标题】:how to make Dyanamic NSDictonary in Swift 【发布时间】:2016-08-23 05:55:34 【问题描述】:

   “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
        
    

【问题讨论】:

你的代码是客观的 C 但你在 Swift 中标记, @Anbu.Karthik 在 SWIFT 中 【参考方案1】:

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

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)

【讨论】:

【参考方案2】:

您可以这样使用 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)

【讨论】:

请帮助我编辑感谢帮助。 @NDoc @AvinashMishra 是 QuesID 是数组吗?也请提供更多详细信息的模型类 playInfoanswerinfo 根据您的需要编辑完成谢谢。 @AvinashMishra 一个 playInfo 有多个答案,您如何区分所选答案? 这些是要显示的 Ans 选项,并且必须选择一个 Ans 以发送回服务器每个 QUEstID 将有一个 AnsID 在 Array 中。【参考方案3】:

希望对您有所帮助:

//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]]

【讨论】:

请帮助我编辑感谢帮助。

以上是关于如何在 Swift 中制作动态 NSDictionary的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift 中的 Spritkit 中制作动态位置

如何在 swift 中在动态 TableView 中创建动态 tableView?

如何根据内容动态制作 Swift UIStackView 大小?

Swift UI - 如何制作图像网格?

swift 如何在swift中制作一个二维数组

如何在 Swift 中制作可设计的 UIImage