0
我將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 「]
在類別爲[字符串]試'爲類別' – iYoung
@iYoung感謝兄弟,我得到了相同的解決方案 –
可能重複[轉換NSArray到Swift數組](http://stackoverflow.com/questions/24422840/convert-nsarray-to-swift-arrayt) –
alternatiph