将空对象添加到 SQLite

Adding null object to SQLite

我在 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]) {
        ...
    }
}

我有两个问题。

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

其次:当我编译并 运行 应用程序时,它崩溃并出现以下错误。但是当我删除 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");
}

我没有得到任何 HelloNSLogs

我是 ios 开发的新手,但我知道如果您想将空对象添加到您的数据集合中,您必须使用 NSNULL class.

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]
}