简体   繁体   English

如何快速从结构检索值

[英]How to retrieve values from struct in swift

I'm converting JSON data to a struct, then adding the struct to an array, but I'm having trouble accessing the values in the struct when I do so. 我正在将JSON数据转换为结构,然后将该结构添加到数组中,但是这样做时我在访问结构中的值时遇到了麻烦。

First I've got my struct: 首先,我有我的结构:

struct Skill {
    var name: String

    init(dictionary: [String: Any]){
    name = dictionary["name"] as! String
    }    
}

Then in another class, I am converting my JSON data into the struct and adding it to an array. 然后在另一个类中,我将JSON数据转换为struct并将其添加到数组中。 I can access the values within the for loop (ie skillDict.name), but I cannot access them from the array in another class. 我可以在for循环中访问值(即skillDict.name),但不能从另一个类的数组中访问它们。

var skillArray: NSMutableArray = []
fun getJSON(){
….
if let skill : NSArray = jsonRoot["skills"] as? NSArray
    {

        for each in skill{
            var skillDict = Skill(dictionary: each as! [String : Any])

            skillArray.add(skillDict)      
            }

    }

When I run the below code from another class, I get this error on the first print line:"this class is not key value coding-compliant for the key name". 当我从另一个类运行下面的代码时,我在第一行显示此错误:“该类不符合键名的键值编码”。 I've also tried using The second print line prints all of my objects correctly, but I cannot access the name value. 我也尝试使用第二行打印可以正确打印所有对象,但是我无法访问名称值。

for each in skillArray{
       print(skillArray.value(forKey: "name"))
        print(each) //this line will print correctly, so I know the correct data is in the array

    }

I've also tried using the below code, both inside and outside of the for loop: 我也尝试过在for循环的内部和外部使用以下代码:

print(skillArray.map { $0["name"] as? String })

But I get a compiler error "Type Any has no subscript members" 但是我收到一个编译器错误“ Type Any没有下标成员”

How do I access the name value correctly? 如何正确访问名称值?

if you want to use subscript key isEqual to "name" then return name 如果要使用下标键isEqual等于“名称”,则返回名称

struct Skill {
    var name: String

    init(dictionary: [String: Any]){
        name = dictionary["name"] as! String
    } 

    subscript(_ name: String) -> String? {
        get {
            return name == "name" ? self.name : nil
        }
    }
}

this code can use print(skillArray.map { $0["name"] as? String }) work 此代码可以使用print(skillArray.map { $0["name"] as? String })工作

Instantiate the array as [Skill] : 将数组实例化为[Skill]

var skills = [Skill]()
...
let names = skills.map { $0.name }

You have two ways to fix it either make the skillArray of type [Skill] instead of NSMutablearray or cast $0 in map function to be of type Skill first and then use the underlying property. 您有两种解决方法,要么使[Skill]类型的skillArray代替NSMutablearray要么在map函数中skillArray$0转换为Skill类型,然后再使用基础属性。

eg, This may be helpful: 例如,这可能会有所帮助:

print(skillArray.map { ($0 as! Skill).name })

If you declare skillArray as: 如果将skillArray声明为:

var skillArray = Array<Skill>()

then this should work: 那么这应该工作:

print(each.name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM