将空对象添加到SQLite

时间:2015-03-30 19:13:04

标签: ios objective-c database sqlite nsarray

我在xcode中实现了SQLite。我有2列。当第一列有一行时,第二列设置为null,反之亦然。我试图浏览两个列并检查它是否在该索引处有一个对象。

NSLog(@"%@ and %@", firstArray, secondArray); // It gives the right risults, but there aren't any null objects in any of them.
for (int i = 0; i < [firstArray count]; i++)
{
    if (![[firstArray objectAtIndex:i] length] && [[secondArray objectAtIndex:i] length]) {
        ...
    }
}

我有两个问题。

首先:行中没有任何空对象,即使我将一些设置为null。那么我应该怎么做才能检查是否存在空对象,或者是否有另一种方法来添加实际添加到表行的某种空对象?

第二:当我编译并运行应用程序时,它会因以下错误而崩溃。但是当我删除if语句时,它可以完美地运行。

-[__NSArrayM length]: unrecognized selector sent to instance 0x7fbfeb53b0a0
2015-03-30 11:25:04.731 MyApp [610:15600] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x7fbfeb53b0a0'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000110874f35 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000011050dbb7 objc_exception_throw + 45
2   CoreFoundation                      0x000000011087c04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x00000001107d427c ___forwarding___ + 988
4   CoreFoundation                      0x00000001107d3e18 _CF_forwarding_prep_0 + 120
5   MyApp                        0x000000010fec3b59 -[ViewController ingredientAction] + 889
6   MyApp                       0x000000010feba253 -[ViewController menuAction:] + 195
7   UIKit                               0x0000000110ea58be -[UIApplication sendAction:to:from:forEvent:] + 75
8   UIKit                               0x0000000110fac410 -[UIControl _sendActionsForEvents:withEvent:] + 467
9   UIKit                               0x00000001110269ea -[UISegmentedControl _setSelectedSegmentIndex:notify:animate:] + 570
10  UIKit                               0x0000000111028a0f -[UISegmentedControl touchesEnded:withEvent:] + 143
11  UIKit                               0x0000000111252540 _UIGestureRecognizerUpdate + 9487
12  UIKit                               0x0000000110eeaff6 -[UIWindow _sendGesturesForEvent:] + 1041
13  UIKit                               0x0000000110eebc23 -[UIWindow sendEvent:] + 667
14  UIKit                               0x0000000110eb89b1 -[UIApplication sendEvent:] + 246
15  UIKit                               0x0000000110ec5a7d _UIApplicationHandleEventFromQueueEvent + 17370
16  UIKit                               0x0000000110ea1103 _UIApplicationHandleEventQueue + 1961
17  CoreFoundation                      0x00000001107aa551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18  CoreFoundation                      0x00000001107a041d __CFRunLoopDoSources0 + 269
19  CoreFoundation                      0x000000011079fa54 __CFRunLoopRun + 868
20  CoreFoundation                      0x000000011079f486 CFRunLoopRunSpecific + 470
21  GraphicsServices                    0x0000000114c759f0 GSEventRunModal + 161
22  UIKit                               0x0000000110ea4420 UIApplicationMain + 1282
23  MyApp                        0x000000010fec11b3 main + 115
24  libdyld.dylib                       0x00000001122ad145 start + 1
25  ???                                 0x0000000000000001 0x0 + 1

修改

我将@""代替null放入行中。然后我将if语句(在for循环中)更改为以下内容:

NSStirng *objectAtIndexFromArray = [NSString stringWithFormat:@"%@", [firstArray objectAtIndex:i]];
if ([objectAtIndexFromArray isEqualToString:@""])
{
    NSLog(@"Hello");
}

我没有NSLogs Hello

1 个答案:

答案 0 :(得分:0)

我对ios开发很新,但我知道如果你想为你的数据集添加null对象,你必须使用NSNULL类。

NSArray无法保留nils,如果要检查是否为null,请使用此代码

id object = myArray[0];// similar to [myArray objectAtIndex:0]

if(![object isEqual:[NSNull null]])
{
    //do something if object is not equals to [NSNull null]
}