如何将NSMutable数组转换为Swift数组?
问题描述:
我将ID添加到NSMutableArray
之后,我希望这个MutableArrray
作为Swift数组。如何将NSMutable数组转换为Swift数组?
这里是我的代码:
var categories = NSMutableArray()
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (!categoryTable.hidden)
{
print("select multiple categories")
categoryList = categoryData[indexPath.row]
categories.addObject(["\(categoryList!.categoryID)"])
}
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let listCategoryID = categoryData[indexPath.row]
for category in categories as [AnyObject]
{
if("\(listCategoryID.categoryID)" == category.objectAtIndex!(0) as! String)
{
categories.removeObject(category)
}
}
print("Catgeory ID \(categories)")
}
// Convert NSMutableArray To Swift Array
@IBAction func saveBtnPressed(sender: AnyObject) {
let catgID = categories as AnyObject as! [String]
}
输出我得到:分类标识((27),(26))
输出我想: “27”,“26 “]
答
var catg_id:[Int] = []
for category in categories as [AnyObject]
{
catg_id.append(Int(category.objectAtIndex!(0) as! NSNumber))
}
+0
你想得到输出字符串或int? – iYoung
在类别为[字符串]试'为类别' – iYoung
@iYoung感谢兄弟,我得到了相同的解决方案 –
可能重复[转换NSArray到Swift数组](http://*.com/questions/24422840/convert-nsarray-to-swift-arrayt) –
alternatiph