Help with Parsing XML in iOS
I'm having a problem with my app, I'm attempting to save stuff in core data by copying a label, but its giving me this error
Terminating app due开发者_如何学运维 to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString appendString:]: nil argument'
This is the code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
ClientDownload *currentClientDownload = [[xmlClientDownloadParser clientArray] objectAtIndex:indexPath.row];
CGRect contentFrame = CGRectMake(45, 2, 265, 30);
clientName = [[[UILabel alloc] initWithFrame:contentFrame] autorelease];
clientName.numberOfLines = 2;
clientName.font = [UIFont boldSystemFontOfSize:12];
clientName.text = [currentClientDownload NAME];
[cell.contentView addSubview:clientName];
CGRect detailFrame1 = CGRectMake(45, 40, 265, 30);
clientMobile = [[[UILabel alloc] initWithFrame:detailFrame1] autorelease];
CGRect detailFrame2 = CGRectMake(45, 40, 265, 30);
clientAccount = [[[UILabel alloc] initWithFrame:detailFrame2] autorelease];
clientAccount.font = [UIFont systemFontOfSize:10];
clientAccount.text = [currentClientDownload ACCOUNT];
clientMobile.font = [UIFont systemFontOfSize:10];
clientMobile.text = [currentClientDownload MOBILE];
[cell.contentView addSubview:clientAccount];
[cell.contentView addSubview:clientMobile];
NSLog(@"SaveData");
Clients *client = (Clients *)[NSEntityDescription insertNewObjectForEntityForName:@"Clients" inManagedObjectContext:managedObjectContext];
if( (clientName.text) || (clientAccount.text) || (clientMobile.text) != NULL)
{
client.ACCOUNT = clientAccount.text;
client.NAME = clientName.text;
client.MOBILE = clientMobile.text;
NSLog(@"CLIENT GENERATED");
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(@"Problem saving: %@", [error localizedDescription]);
}
}
NSError * error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Clients" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects)
{
NSLog(@"ACCOUNT: %@",[info valueForKey:@"ACCOUNT"]);
NSLog(@"NAME: %@",[info valueForKey:@"NAME"]);
NSLog(@"MOBILE: %@",[info valueForKey:@"MOBILE"]);
}
[fetchRequest release];
return cell;
}
As I see you have wrong if-statement here:
if( (clientName.text) || (clientAccount.text) || (clientMobile.text) != NULL)
It should look like this one (if I'm not mistaken):
if( (clientName.text != NULL) && (clientAccount.text != NULL) && (clientMobile.text != NULL))
In your if statement, you are checking all three UILabel
s for zero text at the same time. That means, if there is text in one of them and the other two are empty, the if condition will return YES
. You then try to assign this empty text to your custom object.
You should revise your if statements to refer to each field separately. Thus:
if (clientAccount.text != nil) client.ACCOUNT = clientAccount.text;
if (clientName.text != nil) client.NAME = clientName.text;
if (clientMobile.text != nil) client.MOBILE = clientMobile.text;
If this does not fix it, please post the exact line where you get the crash.
精彩评论