Swift 为 ShinobiGrids 创建 NSObject 数组

Swift Creating array of NSObject for ShinobiGrids

我对 swift 的 ShinobiGrids SDataGridDataSourceHelper 有疑问,他们有很好的 objective-c 例子,但 swift 的例子不多。

在 objective C 他们创建了一个 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

然后他们填充数组:

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

然后在 SDataGridDataSourceHelper 设置中调用它:

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

这就是我在使用 swift 时遇到问题的地方,我创建了我的 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()
    }
}

然后填充一个数组并将其应用于 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

但是当我 运行 它和我的应用程序崩溃时我得到了这个错误:

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

我查过这个错误,但所有的修复都与我的视图控制器有关,它是空的(但有导航控制器和点击栏控制器)

我是不是做错了什么?这是我正在 https://github.com/shinobicontrols/grids-custom-checkbox

项目的代码

这就是 Shinobigrids 在 Swift 上的所有内容:https://www.shinobicontrols.com/docs/ios/shinobigrids/latest/docs/markdown_files/DataGridUserGuide.html#Quick 入门指南 - Swift

在 Swift 中,4 个对象未隐式推断为公开给 Objective-C。

您需要为每个 属性

添加 @objc 属性
@objc var lot: String
@objc var columnA: String
@objc var columnB: String
@objc var columnC: String
@objc var cameraColumn: String

或者如果所有属性都应该在 ObjC 中使用,请在 class 声明上方添加一次 @objcMembers

@objcMembers
class DataObject : NSObject { ...