touchJSON 给出 NSInvalidException NSNull isEqualtoString:
您好,我是可可开发的新手,我正在尝试找出我做错了什么。我遵循了一个(教程),该教程使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜测来自数据库的对象之一在数据库中为
NULL
,在 JSON 中被正确转换为null
并被正确转换为TouchJSON 中的 >NSNull
。然后,您从字典中取出它并将其设置为UILabel
的文本。您应该在
tableView:cellForRowAtIndexPath:
中添加检查,以检查对象实际上是NSString
。可能是这样的:另外,为什么每次表视图要求一个单元格时都要对行进行排序?当您获取数据时,您可能应该对它们进行一次排序 - 即在您的情况下在
viewDidLoad
中。I'm guessing one of the objects that's coming from your database is
NULL
in the DB, being correctly translated into anull
in the JSON and being correctly translated into anNSNull
in TouchJSON. Then you're grabbing it out of the dictionary and setting it as the text of aUILabel
.You should add checks in your
tableView:cellForRowAtIndexPath:
to check the objects are actuallyNSString
s. Probably something like: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.您也可以使用这个:
使用 NSDictionary+Verified 避免 NSNull 对象崩溃
Also you can use this:
Avoid crash by NSNull objects with NSDictionary+Verified