touchJSON 给出 NSInvalidException NSNull isEqualtoString:

发布于 2024-12-25 17:07:21 字数 3454 浏览 1 评论 0原文

您好,我是可可开发的新手,我正在尝试找出我做错了什么。我遵循了一个(教程),该教程使用 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

正如我已经告诉过的,我对此很陌生,因此非常欢迎任何帮助!我已经尝试解决这个问题好几天了,但我似乎找不到准确的答案或解决方案!

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

Hello I am new to Cocoa Development, and I'm trying to figure out what I did wrong. I followed a (tutorial) that uses touchJSON to fill a tableView with a mySQL database in Xcode. When I run the application everything is working fine, but when I scroll down the tableView I get an NSInvalidExeption error:

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

I don't really know if this has something to do with the php code (and database) or the code in Xcode.

This is my php code:

<?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).'}';

?> 

This is my code from 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

As I already told I'm new to this, so any help would be very very welcome! I'm trying to figure out this issue for days now but I can't seem to find an accurate answer or a solution!

Thank you very much for your efforts in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼中杀气 2025-01-01 17:07:21

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

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

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

另外,为什么每次表视图要求一个单元格时都要对行进行排序?当您获取数据时,您可能应该对它们进行一次排序 - 即在您的情况下在 viewDidLoad 中。

I'm guessing one of the objects that's coming from your database is NULL in the DB, being correctly translated into a null in the JSON and being correctly translated into an NSNull in TouchJSON. Then you're grabbing it out of the dictionary and setting it as the text of a UILabel.

You should add checks in your tableView:cellForRowAtIndexPath: to check the objects are actually NSStrings. Probably something like:

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

Also, why are you sorting the rows each time the table view asks for a cell? You should probably just sort them once, when you get the data - i.e. in viewDidLoad in your case.

别在捏我脸啦 2025-01-01 17:07:21

您也可以使用这个:

使用 NSDictionary+Verified 避免 NSNull 对象崩溃

- (id)verifiedObjectForKey:(id)aKey;

Also you can use this:

Avoid crash by NSNull objects with NSDictionary+Verified

- (id)verifiedObjectForKey:(id)aKey;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文