2012-12-06 48 views
0

我一直在這一段時間卡住了,但發生了什麼是,當請求從mysql返回有一個空值,但我如何檢查空值已經打我的條件我不把這個值存儲在數組中。該代碼是下面目標c - 獲得空值

我不斷收到是錯誤 - [NSNull isEqualToString:]:無法識別和即時通訊試圖陣列綁定的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月6日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公司-12-06 12:22:11.318 JobAddSite [7718:207] - [NSNull isEqualToString:]:無法識別的選擇器發送到實例0xe565e8 2012年12月6日2012年12月6日12:22:11.321 JobAddSite [7718:207] *終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: ' - [NSNull isEqualToString:]:無法識別的選擇發送到實例0xe565e8' *調用堆棧在第一擲: ( 0的CoreFoundation 0x00de95a9 exceptionPreprocess + 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的視圖] + 179 7的UIKit 0x0038aa3d - [UIViewController中viewControllerForRotation] + 63 8的UIKit 0x00386988 - [UIViewController中_visibleView] + 90 9的UIKit 0x0062893c - [UIClientRotationContext initWithClient:toOrientation:持續時間:andWindow:] + 354 10的UIKit 0x0030081e - [一個UIWindow _setRotatableClient:toOrientation:updateStatusBar:持續時間:力:] + 954 11的UIKit 0x00588619 - [UIWindowController過渡:fromViewController:toViewController:目標:didEndSelector:] + 1381 12的UIKit 0x0038d65d - [UIViewController中presentModalViewController:withTransition :] + 3478 13 JobAddSite 0x0000213f - [JobAddSiteViewController搜索] + 270 14的UIKit 0x002d94fd - [UIApplication的sendAction:爲:從:forEvent:] + 119 15的UIKit 0x00369799 - [UIControl sendAction:至: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主+ 102 31 JobAddSite 0x00001d7d啓動+ 53 ) 終止稱爲投擲的 'NSException'

+0

你用什麼JSON格式? – 2012-12-06 11:03:51

+0

PREMKUMAR,即時通訊使用JSONKit – redoc01

+0

它也是像SBJson一樣獲得響應後都一樣 – 2012-12-06 11:59:19

回答

1

剛剛嘗試這一個實例後:

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

這一個是相同的,但也許更具可讀性:

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

xapslock,仍然沒有運氣在for循環之後視圖不想切換重複 – redoc01

0

嗨請使用下面的代碼。

它工作得很好。

Download Supporting Files

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

並檢查您的控制檯。

0

檢查條件像這樣和增值陣列

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