我对ShinobiGrids的SDataGridDataSourceHelper for swift有一个问题,它们有objective-c的很好例子,但是对于swift来说没有太多。
在objective 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];

这就是我在斯威夫特身上遇到麻烦的地方,我创建了我的班级:
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

但是当我运行它并且我的应用程序崩溃时,我会得到这个错误:
该类不符合密钥批的密钥值编码。
我已经查找了这个错误,但是所有的修复都与我的视图控制器有关,它是空的(但是有导航控制器和点击栏控制器)
我做错什么了吗?这是我正在进行的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 add中使用一次
@objcMembers
class DataObject : NSObject { ...

关于ios - 快速为ShinobiGrids创建NSObject数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49258020/

10-12 03:49