将NSARRAY转换为NSDictionary,作为JSon序列化swift4 Xcode 9的结果使用

将NSARRAY转换为NSDictionary,作为JSon序列化swift4 Xcode 9的结果使用,swift,nsarray,converter,nsdictionary,Swift,Nsarray,Converter,Nsdictionary,我想将NSArray转换为NSDictionary,然后选择NSDictionary中的键和值,以便以后能够使用NSDictionary中的键将数据添加到对象中 我怎样才能以最聪明的方式做到这一点 以下是我目前掌握的情况: func makeCall(completion: result: NSDictionary or Dictionary){ let json = try JSONSerialization.jsonObject(with: data!, options: JSON

我想将
NSArray
转换为
NSDictionary
,然后选择
NSDictionary
中的键和值,以便以后能够使用
NSDictionary
中的键将数据添加到对象中

我怎样才能以最聪明的方式做到这一点

以下是我目前掌握的情况:

func makeCall(completion: result: NSDictionary or Dictionary){
    let json = try JSONSerialization.jsonObject(with: data!, options:  JSONSerialization.ReadingOptions(rawValue: 0)) as? NSDictionary
    let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? Array<Any>
}
func makeCall(完成:结果:NSDictionary或Dictionary){
让json=try JSONSerialization.jsonObject(with:data!,options:JSONSerialization.ReadingOptions(rawValue:0))作为?NSDictionary
让json=try JSONSerialization.jsonObject(使用:data!,选项:.allowFragments)作为?数组
}

这两个JSON文件看起来几乎相同。不同之处在于var类型,因此您将以数组样式获取键和值。我们需要字典样式的它,以便通过键获取值。

Swift 4

在Swift中,您更应该使用Dictionary,并且只有在明确需要该类型时才使用NSDictionary

    //your NSArray
    let myArray: NSArray = ["item1","item2","item3"]

    //initialize an emtpy dictionaty
    var myDictionary = [String:String]()

    //iterate through the array
    for item in myArray
    {
        //add array items to dictionary as key with any value you prefer
        myDictionary.updateValue("some value", forKey: item as! String)
    }

    //now you can use myDictionary as Dictionary
    print ("my dictionary: ")
    print (myDictionary)

    //if you prefer to use it as an NSDictionary
    let myNSDictionary = myDictionary as NSDictionary!
    print ("my NSDictionary: ")
    print (myNSDictionary)

Swift 4

在Swift中,您更应该使用Dictionary,并且只有在明确需要该类型时才使用NSDictionary

    //your NSArray
    let myArray: NSArray = ["item1","item2","item3"]

    //initialize an emtpy dictionaty
    var myDictionary = [String:String]()

    //iterate through the array
    for item in myArray
    {
        //add array items to dictionary as key with any value you prefer
        myDictionary.updateValue("some value", forKey: item as! String)
    }

    //now you can use myDictionary as Dictionary
    print ("my dictionary: ")
    print (myDictionary)

    //if you prefer to use it as an NSDictionary
    let myNSDictionary = myDictionary as NSDictionary!
    print ("my NSDictionary: ")
    print (myNSDictionary)

您可以添加一个示例,使您的问题变得更清楚吗?如果可能,请不要在Swift中使用NS类。您可以添加一个示例,使您的问题变得更清楚吗?如果可能,请不要在Swift中使用NS类。我们从Json文件获取数据,并将其放入数组中。Json文件的类型如下:name subtype:name when created:time keyForData:Data。我们如何使用数组中已有的键来获取数组中的数据?在Swift中,您更应该使用数组,您明确地不需要
NSArray
您可以将从服务器接收到的数据直接转换到NSArray,而不是访问值let data:data=dataFromServer let myNSArray=try!JSONSerialization.jsonObject(带有:data,options:.mutableLeaves)作为?Nsarraye从Json文件中获取数据,并将其放入数组中。Json文件的类型如下:name subtype:name when created:time keyForData:Data。我们如何使用数组中已有的键来获取数组中的数据?在Swift中,您更应该使用数组,您明确地不需要
NSArray
您可以将从服务器接收到的数据直接转换到NSArray,而不是访问值let data:data=dataFromServer let myNSArray=try!JSONSerialization.jsonObject(带有:data,options:.mutableLeaves)作为?不可变数组