Swift Creating array of NSObject for ShinobiGrids

user979331 Source

I am having an issue with ShinobiGrids SDataGridDataSourceHelper for swift, they have great examples of objective-c but not much for swift.

In objective C they create an NSObject class

@interface Student : NSObject

@property NSString *name;
@property NSNumber *credits;
@property BOOL canGraduate;

- (id)initWithName:(NSString *)name andCredits:(NSNumber *)credits canGraduate:(BOOL)canGraduate;

@end

@interface Student : NSObject

@property NSString *name;
@property NSNumber *credits;
@property BOOL canGraduate;

- (id)initWithName:(NSString *)name andCredits:(NSNumber *)credits canGraduate:(BOOL)canGraduate;

@end

and then they populate the array:

- (NSArray *)createMockStudentArray {
    return @[[[Student alloc] initWithName:@"Bill"      andCredits:@40  canGraduate:NO],
             [[Student alloc] initWithName:@"Rob"       andCredits:@80  canGraduate:YES],
             [[Student alloc] initWithName:@"James"     andCredits:@80  canGraduate:YES],
             [[Student alloc] initWithName:@"Harry"     andCredits:@30  canGraduate:NO],
             [[Student alloc] initWithName:@"Sue"       andCredits:@90  canGraduate:YES],
             [[Student alloc] initWithName:@"Rachel"    andCredits:@120 canGraduate:YES],
             [[Student alloc] initWithName:@"Annie"     andCredits:@70  canGraduate:NO],
             [[Student alloc] initWithName:@"Daniel"    andCredits:@80  canGraduate:YES],
             [[Student alloc] initWithName:@"Harry"     andCredits:@80  canGraduate:YES],
             [[Student alloc] initWithName:@"Tom"       andCredits:@90  canGraduate:YES],
             [[Student alloc] initWithName:@"Fred"      andCredits:@40  canGraduate:NO],
             [[Student alloc] initWithName:@"Andy"      andCredits:@10  canGraduate:NO],
             [[Student alloc] initWithName:@"Sarah"     andCredits:@60  canGraduate:NO],
             [[Student alloc] initWithName:@"Elliot"    andCredits:@80  canGraduate:YES],
             [[Student alloc] initWithName:@"Babra"     andCredits:@75  canGraduate:YES],
             [[Student alloc] initWithName:@"Sam"       andCredits:@110 canGraduate:YES],
             [[Student alloc] initWithName:@"William"   andCredits:@120 canGraduate:YES],
             [[Student alloc] initWithName:@"Helen"     andCredits:@90  canGraduate:YES],
             [[Student alloc] initWithName:@"Jim"       andCredits:@100 canGraduate:YES],
             [[Student alloc] initWithName:@"Oleg"      andCredits:@90  canGraduate:YES],
             [[Student alloc] initWithName:@"Andrew"    andCredits:@110 canGraduate:YES]];
}

and then calling it in the SDataGridDataSourceHelper settings:

SDataGridDataSourceHelper *_dataSourceHelper = [[SDataGridDataSourceHelper alloc] initWithDataGrid:_grid];
    _dataSourceHelper.delegate = self;
    _dataSourceHelper.data = [self createMockStudentArray];

and thats where I am having trouble with swift, I created my class:

class DataObject : NSObject
{

    var lot: String
    var columnA: String
    var columnB: String
    var columnC: String
    var cameraColumn: String

    init(fromString lot: String, columnA: String, columnB: String, columnC: String, cameraColumn: String) {
        self.lot = lot
        self.columnA = columnA
        self.columnB = columnB
        self.columnC = columnC
        self.cameraColumn = cameraColumn
        super.init()
    }
}

and then populated an Array and applied it to SDataGridDataSourceHelper:

let helper = SDataGridDataSourceHelper(dataGrid: grid!)
        helper?.delegate = self

        var array = [DataObject.init(fromString: "lot", columnA: "colum a", columnB: "colum b", columnC: "colum c", cameraColumn: "camera")] as Array

        array.append(DataObject.init(fromString: "lot", columnA: "colum a", columnB: "colum b", columnC: "colum c", cameraColumn: "camera"))

        print(array)

        helper?.data = array

but then I get this error when I run it and my app crashes:

this class is not key value coding-compliant for the key lot.

I have looked up this error, but all the fixes have to do with my view controller, which is empty (but has navigation controller and tap bar controller)

Am I doing something wrong? Here is the code for the project I am working off of https://github.com/shinobicontrols/grids-custom-checkbox

and this is literally everything Shinobigrids has on Swift: https://www.shinobicontrols.com/docs/ios/shinobigrids/latest/docs/markdown_files/DataGridUserGuide.html#Quick Start Guide - Swift

iosswiftshinobi

Answers

answered 6 months ago vadian #1

In Swift 4 objects are not implicitly inferred to be exposed to Objective-C.

You need to add the @objc attribute to each property

@objc var lot: String
@objc var columnA: String
@objc var columnB: String
@objc var columnC: String
@objc var cameraColumn: String

Or if all properties are supposed to be used in ObjC add once @objcMembers above the class declaration

@objcMembers
class DataObject : NSObject { ...

comments powered by Disqus