0

我已经坚持了一段时间了,但是发生的事情是,当请求从 mysql 返回时,有一个空值,但是我如何检查空值是否达到了我的条件,以便我不将该值存储在数组中. 代码如下

我不断收到的错误是 -[NSNull isEqualToString:]: unrecognized and im trying to bind the array inside a tableview

- (void)viewDidLoad {

JobAddSiteAppDelegate *ja = (JobAddSiteAppDelegate *)[[UIApplication sharedApplication] delegate];

tempArray = [[NSMutableArray alloc] init];
NSString *strURL = [NSString stringWithFormat:@"http://www.bestitjobs.co.uk/appresults3.php", ""];
NSData *nsData = [NSData dataWithContentsOfURL:[NSURL URLWithString: strURL]];
//NSLog(@"JSON: %@", nsData);
//NSString *strResult = [[[NSString alloc] initWithData:nsData encoding:NSUTF8StringEncoding] autorelease];
//NSLog(@"Result: %@",strResult);

//NSError *e = nil;
JSONDecoder* decoder = [[JSONDecoder alloc] init];
NSDictionary* listDictionary = [nsData objectFromJSONData];
NSArray* people =[listDictionary objectForKey:@"jobs"];

for (NSDictionary *person in people) { 
    NSLog(@"people %@",people);
    if([person objectForKey:@"position"] != [NSNull class])
    { 
            NSString *position = [person objectForKey:@"position"];
            NSLog(@"position %@",position);
            [tempArray addObject: position];
    }
}
self.listData = tempArray;


[tempArray release];
[listData release];
[super viewDidLoad];

}

错误

2012-12-06 12:22:11.314 JobAddSite[7718:207] 职位高级网络和系统顾问 2012-12-06 12:22:11.316 JobAddSite[7718:207] 职位 SAP Business One / SAP B1 顾问伦敦 2012-12 -06 12:22:11.317 JobAddSite[7718:207] 职位业务开发经理 - 市场领先的 IT 公司 2012-12-06 12:22:11.318 JobAddSite[7718:207] -[NSNull isEqualToString:]:无法识别的选择器发送到实例0xe565e8 2012-12-06 12:22:11.321 JobAddSite[7718:207] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSNull isEqualToString:]:无法识别的选择器发送到实例 0xe565e8”*首先调用堆栈抛出:(0核心基础0x00de95a9exceptionPreprocess + 185 1 libobjc.A.dylib 0x00f3d313 objc_exception_throw + 44 2 CoreFoundation 0x00deb0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 3 CoreFoundation 0x00d5a966 __转发+ 966 4 CoreFoundation 0x00d5a522 _CF_forwarding_prep_0 + 50 5 JobAddSite 0x000024cd -[ResultViewController viewDidLoad] + 604 6 UIKit 0x00389089 -[UIViewController view] + 179 7 UIKit 0x0038aa3d -[UIViewController viewControllerForRotation] + 63 8 UIKit 0x00386988 -[UIViewController _visibleView] + 90 9 UIKit 0x0062893c -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 354 10 UIKit 0x0030081e -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 954 11 UIKit 0x00588619 -[UIWindowController 转换:fromViewController:toViewController:target:didEndSelector:] + 1381 12 UIKit 0x0038d65d -[UIViewController presentModalViewController:withTransition:] + 3478 13 JobAddSite 0x0000213f -[JobAddSiteViewController 搜索] + 270 14 UIKit 0x002d94fd -[UIApplication sendAction:to:15:forEvent:] + 11 UIKit 0x00369799 -[UIControl sendAction:to:forEvent:] + 67 16 UIKit 0x0036bc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 17 UIKit 0x0036a7d8 -[UIControl touchesEnded:withEvent:] + 458 18 UIKit 0x002fddedEvent-[ :] + 567 19 UIKit 0x002dec37 -[UIApplication sendEvent:] + 447 20 UIKit 0x002e3f2e _UIApplicationHandleEvent + 7576 21 GraphicsServices 0x01741992 PurpleEventCallback + 1550 22 CoreFoundation 0x00dca944CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52 23 CoreFoundation 0x00d2acf7 __CFRunLoopDoSource1 + 215 24 CoreFoundation 0x00d27f83 __CFRunLoopRun + 979 25 CoreFoundation 0x00d27840 CFRunLoopRunSpecific + 208 26 CoreFoundation 0x00d27761 CFRunLoopRunInMode + 97 27 GraphicsServices 0x017401c4 GSEventRunModal + 217 28 GraphicsServices 0x01740289 GSEventRun + 115 29 UIKit 0x002e7c93 UIApplicationMain + 1160 30 JobAddSite 0x00001dec main + 102 31 JobAddSite 0x00001d7d start + 53 ) 在抛出“NSException”实例后调用终止

4

3 回答 3

1

试试这个:

if ([person objectForKey:@"position"]) {..}

这是相同的,但可能更具可读性:

if ([person objectForKey:@"position"] != nil) {..}
于 2012-12-06T11:05:25.573 回答
0

嗨使用以下代码。

它运作良好。

下载支持文件

NSMutableArray *empArray = [[NSMutableArray alloc] init];



SBJSON *parser = [[SBJSON alloc] init];


NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.bestitjobs.co.uk/appresults3.php"]]];

NSLog(@"GGGG:%@",request1);


NSData *response1 = [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil];



NSString *json_string = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding];



NSDictionary *statuses = [parser objectWithString:json_string error:nil];


NSLog(@"List Dictionary:%@",statuses);
NSArray* people =[statuses objectForKey:@"jobs"];

for(NSDictionary *test in people)
{
    [empArray addObject:[test valueForKey:@"position"]];


}


NSLog(@"EMP ARRAY CATEGORY:%@",empArray);

注意:在使用 SBJSon 支持文件和导入 JSON.h 之前

并检查您的控制台。

于 2012-12-06T11:16:11.773 回答
0

像这样检查条件并将值添加到数组

for (NSDictionary *person in people) { 
   NSLog(@"people %@",people);
   NSString *position = [person objectForKey:@"position"];
   if(position) {
      [tempArray addObject: position];
   }
}
于 2012-12-06T11:58:52.130 回答