Multinerd Multinerd - 7 days ago 6
iOS Question

How can I access a Swift extension in Objective-C?

I am using the framework GlyuckDataGrid to implement a datagrid in Objective-C. There's an extension called

IndexPath+DataGrid.swift
that is vital to the datagrid's implementation, however it is written in Swift.

I am using CocoaPods and I have the following imports and still unable to access it.

@import GlyuckDataGrid;
#import <GlyuckDataGrid/GlyuckDataGrid-Swift.h>

JAL JAL
Answer

The framework was using a pure Swift type (IndexPath) instead of its Objective-C equivalent (NSIndexPath). In your case, the solution was to extend NSIndexPath as well as IndexPath to expose the functionality to Objective-C:

public extension NSIndexPath {
    /**
     Returns an index-path object initialized with the indexes of a specific row and column in a data grid view.
     - parameter column: An index number identifying a column in a DataGridView object in a row identified by the row parameter.
     - parameter row:    An index number identifying a row in a DataGridView object.
     - returns: An NSIndexPath object.
     */
    convenience init(forColumn column: Int, row: Int) {
        self.init(item: column, section: row)
    }

    /// An index number identifying a column in a row of a data grid view. (read-only)
    var dataGridColumn: Int {
        return self.index(atPosition: 1)
    }

    /// An index number identifying a row in a data grid view. (read-only)
    var dataGridRow: Int {
        return self.index(atPosition: 0)
    }

    /// An index number for single-item indexPath
    var index: Int {
        return self.index(atPosition: 0)
    }
}

From c281729.

Comments