简体   繁体   English

如何从返回的plist数组中获取字符串

[英]how to take string from returned plist array

When I am trying to take the value from a plist and append it to an array 当我尝试从plist中获取值并将其附加到数组中时

nameArray.append(namesArray!.objectForKey("Item1")! as! String)

The target item is a string but it appears to be inside the plist array, can anyone explain how to get it out please? 目标项目是一个字符串,但它似乎位于plist数组中,请问有人可以解释如何将其取出来吗?

The print of namesArray!.objectForKey("Item1")! 名称数组!.objectForKey(“ Item1”)的打印! followed by the error are shown below: 错误如下所示:

在此处输入图片说明

Cast the value of the "Item1" key as an array of Strings, then fetch the first object from the array (since it appears there's only one). 将“ Item1”键的值强制转换为字符串数组,然后从该数组中获取第一个对象(因为它看起来只有一个)。 And if you like the idea that your app should not crash everytime a value is nil , better use if let than force-unwrapping everything with ! 而且,如果您希望应用程序每次值都不nil都不会崩溃, if let更好用! .

Example: 例:

if let names = namesArray, 
    let items = names.objectForKey("Item1") as? [String], 
    let result = items.first {

    nameArray.append(result)
}

只需获取数组的第一个元素:

nameArray.append((namesArray!.objectForKey("Item1")! as! [String])[0])

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

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