![](/img/trans.png)
[英]How to populate data from NSMutableArray into struct using Swift3
[英]How to join the objects of NSMutableArray with a separator in Swift3
我有一个NSMutableArray声明为:
var operatingDays = NSMutableArray()
接下来,我将对象附加到数组中,如下所示:
for operatingDays in forecast.operatingDays! {
print(operatingDays.days)
print(operatingDays.times)
self.operatingDays.add(operatingDays.days)
}
因此,营业日数组现在包含数组。
现在,我想将每个数组转换为用空格分隔的字符串,并将其显示给UITableViewCell。 为此,我正在像这样在cellForRowAtIndexPath使用代码:
cell.operationalDays.text = (operatingDays[indexPath.row] as AnyObject).joined(separator: " ")
但是它显示了错误:
正如我在第一条评论中所述,将您的operatingDays[indexPath.row]
强制转换为[String]
,请尝试一下
if let arrayOfStrings = operatingDays[indexPath.row] as? [String]
{
cell.operationalDays.text = arrayOfStrings.joined(separator: " ")
}
希望这可以帮助
解决方案如下:
var operatingDays = [Any]()
cell.operationalDays.text = (operatingDays[indexPath.row] as? [String])?.joined(separator: " ")
要么
if let data = operatingDays[indexPath.row] as? [String] {
cell.operationalDays.text = data.joined(separator: " ")
}
尝试按以下方式联合数组:
let tempArr = operatingDays[indexPath.row] as! NSArray
cell.operationalDays.text = tempArr.componentsJoined(by: " ")
将AnyObject强制转换为Array以使用联合函数。
if let isArray = operatingDays[indexPath.row] as? [String] {
//Now you can able to use joined function
cell.operationalDays.text = isArray.joined(separator: " ")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.