![](/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.