Search code examples
cocoaswiftnsindexset

How to get an int out of selectedRowIndexs in Swift?


I haven't worked in Xcode, Objective C or Cocoa before, and I decided to give Swift a try. The simple app I'm building (for Mac OS, not iOS) displays in a table view some data stored in an array.

I'm trying to let the user delete a selected row, the only problem is I can't get an index in the form of a simple int from myTableView.selectedRowIndexes, this piece of code returns me an NSIndexSet object, and I don't know how to deal with it.

Since I want the user to be able to select only one row at a time I think firstIndex is what I need to use, Apple documentation says: var firstIndex: Int { get } but I really lack the knowledge to understand how to use it.

I'm sorry for the very basic question. Any help will be very appreciated!


Solution

  • You are correct. Just ask for the first index:

    let allSelectedRows = myTableView.selectedRowIndexes
    let selectedRow = allSelectedRows.firstIndex