【问题标题】:How to Convert NSMutable Array to Swift Array?如何将 NSMutableArray 转换为 Swift 数组?
【发布时间】:2017-01-01 07:21:53
【问题描述】:

我将 ID 添加到 NSMutableArray 之后,我希望这个 MutableArrray 作为 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]
}

我得到的输出:类别 ID ( ( 27 ), ( 26 ) )

我想要的输出:["27","26"]

【问题讨论】:

标签: ios arrays swift2 nsmutablearray


【解决方案1】:
var catg_id:[Int] = []
        for category in categories as [AnyObject]
        {
            catg_id.append(Int(category.objectAtIndex!(0) as! NSNumber))

        }

【讨论】:

  • 您想以字符串或整数形式输出?
猜你喜欢
  • 1970-01-01
  • 2016-06-06
  • 2014-11-08
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多