gpt4 book ai didi

ios - Swift 为 ShinobiGrids 创建 NSObject 数组

转载 作者:行者123 更新时间:2023-11-28 14:56:59 26 4
gpt4 key购买 nike

我在使用 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];

这就是我在使用 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()
}
}

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

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

我做错了什么吗?这是我正在处理的项目的代码 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 中使用,则在类声明上方添加一次 @objcMembers

@objcMembers
class DataObject : NSObject { ...

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com