0

I am adding ID's to NSMutableArray after that i want this MutableArrray as a Swift Array.

here's my code:

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]
}

Output I am Getting: Category ID ( ( 27 ), ( 26 ) )

Output I want : ["27","26"]

3
  • 1
    try for category in categories as [String] Commented Jan 1, 2017 at 7:50
  • @iYoung thanks bro, i got the same solution Commented Jan 1, 2017 at 7:51
  • 1
    Possible duplicate of Convert NSArray to Swift Array<T> Commented Jan 1, 2017 at 8:25

1 Answer 1

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

        }
Sign up to request clarification or add additional context in comments.

1 Comment

You wanted to get output in string or int?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.