目标c-获取空值 [英] objective c - getting null values

查看:55
本文介绍了目标c-获取空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上停留了一段时间,但是发生的是,当请求从mysql返回时,有一个空值,但是我该如何检查空值是否符合我的条件,以便不存储该值在数组中.代码在下面

I have been stuck on this for some time now but whats happening is that when the request comes back from mysql there is a null value but how do i check that a null value has hit my condition so that i dont store this value in the array. The code is below

我一直收到的错误是-[NSNull isEqualToString:]:无法识别,并且我试图将数组绑定到tableview内部

The error i keep getting is -[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:]:无法识别的选择器已发送到实例0xe565e82012-12-06 12:22:11.321 JobAddSite [7718:207] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[NSNull isEqualToString:]:无法识别的选择器已发送至实例0xe565e8'* 初次调用堆栈:(0 CoreFoundation 0x00de95a9 exceptionPreprocess + 1851个libobjc.A.dylib 0x00f3d313 objc_exception_throw + 442 CoreFoundation 0x00deb0bb-[NSObject(NSObject)didNotRecognizeSelector:] + 1873 CoreFoundation 0x00d5a966 __ 转发 + 9664 CoreFoundation 0x00d5a522 _CF_forwarding_prep_0 + 505 JobAddSite 0x000024cd-[ResultViewController viewDidLoad] + 6046 UIKit 0x00389089-[UIViewController视图] + 1797 UIKit 0x0038aa3d-[UIViewController viewControllerForRotation] + 638 UIKit 0x00386988-[UIViewController _visibleView] + 909 UIKit 0x0062893c-[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 35410 UIKit 0x0030081e-[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 95411 UIKit 0x00588619-[UIWindowController转换:从ViewController:toViewController:target:didEndSelector:] + 138112 UIKit 0x0038d65d-[UIViewController presentModalViewController:withTransition:] + 347813 JobAddSite 0x0000213f-[JobAddSiteViewController搜索] + 27014 UIKit 0x002d94fd-[UIApplication sendAction:to:from:forEvent:] + 11915 UIKit 0x00369799-[UIControl sendAction:to:forEvent:] + 6716 UIKit 0x0036bc2b-[UIControl(内部)_sendActionsForEvents:withEvent:] + 52717 UIKit 0x0036a7d8-[UIControl touchesEnded:withEvent:] + 45818 UIKit 0x002fdded-[UIWindow _sendTouchesForEvent:] + 56719 UIKit 0x002dec37-[UIApplication sendEvent:] + 44720 UIKit 0x002e3f2e _UIApplicationHandleEvent + 757621 GraphicsServices 0x01741992 PurpleEventCallback + 155022 CoreFoundation 0x00dca944 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 5223 CoreFoundation 0x00d2acf7 __CFRunLoopDoSource1 + 21524 CoreFoundation 0x00d27f83 __CFRunLoopRun + 97925 CoreFoundation 0x00d27840 CFRunLoopRunSpecific + 20826 CoreFoundation 0x00d27761 CFRunLoopRunInMode + 9727 GraphicsServices 0x017401c4 GSEventRunModal + 21728个GraphicsServices 0x01740289 GSEventRun + 11529 UIKit 0x002e7c93 UIApplicationMain + 116030 JobAddSite 0x00001dec main + 10231 JobAddSite 0x00001d7d开始+ 53)抛出"NSException"实例后终止调用

2012-12-06 12:22:11.314 JobAddSite[7718:207] position Senior Network and Systems Consultant 2012-12-06 12:22:11.316 JobAddSite[7718:207] position SAP Business One / SAP B1 Consultant London 2012-12-06 12:22:11.317 JobAddSite[7718:207] position Business Development Manager - Market Leading IT Company 2012-12-06 12:22:11.318 JobAddSite[7718:207] -[NSNull isEqualToString:]: unrecognized selector sent to instance 0xe565e8 2012-12-06 12:22:11.321 JobAddSite[7718:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull isEqualToString:]: unrecognized selector sent to instance 0xe565e8' * Call stack at first throw: ( 0 CoreFoundation 0x00de95a9 exceptionPreprocess + 185 1 libobjc.A.dylib 0x00f3d313 objc_exception_throw + 44 2 CoreFoundation 0x00deb0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 3 CoreFoundation 0x00d5a966 __forwarding + 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 transition:fromViewController:toViewController:target:didEndSelector:] + 1381 12 UIKit 0x0038d65d -[UIViewController presentModalViewController:withTransition:] + 3478 13 JobAddSite 0x0000213f -[JobAddSiteViewController search] + 270 14 UIKit 0x002d94fd -[UIApplication sendAction:to:from:forEvent:] + 119 15 UIKit 0x00369799 -[UIControl sendAction:to:forEvent:] + 67 16 UIKit 0x0036bc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 17 UIKit 0x0036a7d8 -[UIControl touchesEnded:withEvent:] + 458 18 UIKit 0x002fdded -[UIWindow _sendTouchesForEvent:] + 567 19 UIKit 0x002dec37 -[UIApplication sendEvent:] + 447 20 UIKit 0x002e3f2e _UIApplicationHandleEvent + 7576 21 GraphicsServices 0x01741992 PurpleEventCallback + 1550 22 CoreFoundation 0x00dca944 CFRUNLOOP_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 ) terminate called after throwing an instance of 'NSException'

推荐答案

只需尝试以下一项:

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

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

This one is the same but maybe more readable:

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

这篇关于目标c-获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆