如何将CSV解析数组中的值放入领域对象属性?

时间:2015-03-19 15:15:19

标签: ios objective-c realm

我已经实现了一个CHCSV Parser,它的委托方法为源CSV文件的每一行返回一个名为currentRow的NSMutableArray。如果我在解析一行后NSLog数组,我可以看到数组中的文本项被引用,数字(整数)是裸的。 currentRow的日志输出如下所示:("urgent_needs",0,0,0,1,"",YES,YES)

好的,所以我需要将这些值放入Realm数据对象中,其定义如下:

@interface Tile : RLMObject
@property NSString *wordKey;
@property NSInteger pageNum;
@property NSInteger viewNum;
@property NSInteger viewItemNum;
@property NSInteger targetPageNum;
@property BOOL enqueue;
@property BOOL playAudio;
@property NSString *action;
@end

我使用以下代码尝试设置对象的属性:

    [realm beginWriteTransaction];
    [Tile createInRealm:realm withObject:@{@"wordKey": currentRow[0],
                                           @"pageNum": currentRow[1],
                                           @"viewNum": currentRow[2],
                                           @"viewItemNum": currentRow[3],
                                           @"targetPageNum": currentRow[4],
                                           @"action": currentRow[5],
                                           @"enqueue": currentRow[6],
                                           @"playAudio": currentRow[7]}
     ];
    [realm commitWriteTransaction];

当我运行此代码时,我得到以下异常:

Error adding data object: Invalid value '(0)' for property 'pageNum'

我已经尝试了几种方法来构建数组元素,但都无济于事。它似乎接受了第一个属性的字符串数据而没有任何抱怨。我怀疑它是一个简单的解决方案,但我很难过。我感谢任何想法或意见。

1 个答案:

答案 0 :(得分:0)

'(0)'是一个字符串,pageNum是一个整数。尝试

@([currentRow[1] integerValue]) 

编辑:看来你的init方法需要一个字典,字典需要对象。您将不得不进入该方法的实现并使用

[object[@"pageNum"] integerValue]