Swift为ShinobiGrids创建NSObject数组

时间:2018-03-13 13:54:15

标签: ios swift shinobi

我对ShinobiGrids SDataGridDataSourceHelper有一个swift的问题,他们有很好的objective-c例子但对swift来说并不多。

在目标C中,他们创建了一个NSObject类

@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 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()
    }
}

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

但是当我运行它并且我的应用程序崩溃时,我收到此错误:

  

此类不是关键批次的密钥值编码兼容。

我已查找此错误,但所有修复都与我的视图控制器有关,它是空的(但有导航控制器和控制杆控制器)

我做错了吗?以下是我正在处理https://github.com/shinobicontrols/grids-custom-checkbox

项目的代码

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

1 个答案:

答案 0 :(得分:1)

在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中使用,则在类声明之上添加一次@objcMembers

@objcMembers
class DataObject : NSObject { ...