2017-01-01 59 views
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 「]

+1

在類別爲[字符串]試'爲類別' – iYoung

+0

@iYoung感謝兄弟,我得到了相同的解決方案 –

+1

可能重複[轉換NSArray到Swift數組](http://stackoverflow.com/questions/24422840/convert-nsarray-to-swift-arrayt) – alternatiph

回答

0
var catg_id:[Int] = [] 
     for category in categories as [AnyObject] 
     { 
      catg_id.append(Int(category.objectAtIndex!(0) as! NSNumber)) 

     } 
+0

你想得到輸出字符串或int? – iYoung