首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >touchJSON给了NSInvalidException NSNull isEqualtoString:

touchJSON给了NSInvalidException NSNull isEqualtoString:
EN

Stack Overflow用户
提问于 2012-01-09 02:22:51
回答 2查看 1.2K关注 0票数 1

大家好,我是Cocoa Development的新手,我正在努力找出我做错了什么。我遵循了一个使用touchJSON在Xcode中用mySQL数据库填充tableView的(tutorial)。当我运行应用程序时,一切正常,但是当我向下滚动tableView时,我得到一个NSInvalidExeption错误:

代码语言:javascript
复制
Terminating app due to uncaught exception 'NSInvalidArgumentException',
    reason: '-[NSNull isEqualToString:]: unrecognized selector sent to 
        instance 0x1469cd8'

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

这是我的php代码:

代码语言:javascript
复制
<?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中的代码:

代码语言:javascript
复制
#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

正如我已经说过的,我是新手,所以任何帮助都将非常非常受欢迎!我试图弄清楚这个问题已经好几天了,但我似乎找不到一个准确的答案或解决方案!

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-09 04:19:51

我猜来自您的数据库的对象之一是DB中的NULL,在JSON中被正确地转换为null,在TouchJSON中被正确地转换为NSNull。然后从字典中取出它,并将其设置为UILabel的文本。

您应该在您的tableView:cellForRowAtIndexPath:中添加检查,以检查对象实际上是NSString。可能如下所示:

代码语言:javascript
复制
id Naam = [dict objectForKey:@"Naam"];
if ([Naam isKindOfClass:[NSString class]]) {
    cell.Naam.text = Naam;
} else {
    cell.Naam.text = @"";
}

另外,为什么每次表视图请求一个单元格时都要对行进行排序?当你得到数据的时候,你可能只需要对它们排序一次--也就是在你的例子中使用viewDidLoad

票数 2
EN

Stack Overflow用户

发布于 2013-05-09 16:02:35

您还可以使用以下命令:

Avoid crash by NSNull objects with NSDictionary+Verified

代码语言:javascript
复制
- (id)verifiedObjectForKey:(id)aKey;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8780018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档