touchJSON给出NSInvalidException NSNull isEqualtoString:

时间:2012-01-08 18:22:51

标签: xcode json touchjson nsnull

您好我是Cocoa Development的新手,我正在试图弄清楚我做错了什么。我跟着一个(tutorial)使用touchJSON在Xcode中用mySQL数据库填充tableView。当我运行应用程序时一切正常,但是当我向下滚动tableView时出现NSInvalidExeption错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException',
    reason: '-[NSNull isEqualToString:]: unrecognized selector sent to 
        instance 0x1469cd8'

我真的不知道这是否与php代码(和数据库)或Xcode中的代码有关。

这是我的PHP代码:

<?php

$link = mysql_pconnect("localhost", "root", "root") or die("Could not connect");
mysql_select_db("PartyON") or die("Could not select database");

$arr = array();
$rs = mysql_query("SELECT id, Maand, Naam, Locatie, Plaats FROM tblWebData");

while($obj = mysql_fetch_object($rs)) {
    $arr[] = $obj;
}

echo '{"tblWebData":'.json_encode($arr).'}';

?> 

这是我的Xcode代码:

#import "GentDataView.h"
#import "CJSONDeserializer.h"
#import "GentDetailCell.h"

@implementation GentDataView

@synthesize rows, tableview;

- (void)viewDidLoad {

    [super viewDidLoad];    

    NSURL *url = [NSURL URLWithString:@"http://localhost:8888/example3.php"]; //URL Modification

    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL

    //  NSLog(jsonreturn); // Look at the console and you can see what the restults are 

    NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];

    NSError *error = nil;   

    // In "real" code you should surround this with try and catch

    NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];

    if (dict)
    {
        rows = [dict objectForKey:@"tblWebData"];
    }

    NSLog(@"Array: %@",rows);   

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [rows count];

}

// Customize the appearance of table view cells.
- (GentDetailCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    


    static NSString *CellIdentifier = @"Cell";

    GentDetailCell *cell = (GentDetailCell *) [tableview dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

        cell = [[GentDetailCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

    // Configure the cell.  
    NSSortDescriptor *sorteerDiscriptor = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:NO];

    rows = [rows sortedArrayUsingDescriptors:[NSArray arrayWithObject:sorteerDiscriptor]];

    NSDictionary *dict = [rows objectAtIndex: indexPath.row];

    cell.Naam.text = [dict objectForKey:@"Naam"];
    cell.Plaats.text = [dict objectForKey:@"Plaats"];
    cell.Maand.text = [dict objectForKey:@"Maand"]; 
    cell.Locatie.text = [dict objectForKey:@"Locatie"]; 
    cell.imageView.image = [NSURL URLWithString:@"http://www.iconarchive.com/show/flags-icons-by-iconscity/belgium-icon.html"];

    //cell.textLabel.text = [dict objectForKey:@"post_title"];
    //cell.detailTextLabel.text = [dict objectForKey:@"post_content"];  
    //tableView.backgroundColor = [UIColor cyanColor];  

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}
@end

正如我已经告诉我的新手,所以任何帮助都会非常受欢迎!我正试图解决这个问题好几天,但我似乎无法找到准确的答案或解决方案!

非常感谢你提前做出的努力!

2 个答案:

答案 0 :(得分:2)

我猜测来自数据库的其中一个对象是NULL在数据库中,正确地转换为JSON中的null并正确转换为NSNull在TouchJSON中。然后你将它从字典中删除并将其设置为UILabel的文本。

您应该在tableView:cellForRowAtIndexPath:中添加支票,以检查对象实际上是NSString。可能类似于:

id Naam = [dict objectForKey:@"Naam"];
if ([Naam isKindOfClass:[NSString class]]) {
    cell.Naam.text = Naam;
} else {
    cell.Naam.text = @"";
}

另外,为什么每次表视图要求一个单元格时对行进行排序?当你得到数据时,你可能只需要对它们进行一次排序 - 例如在你的情况下在viewDidLoad

答案 1 :(得分:0)

你也可以使用它:

Avoid crash by NSNull objects with NSDictionary+Verified

- (id)verifiedObjectForKey:(id)aKey;