帮助解析iOS中的XML

时间:2011-09-07 14:35:10

标签: xml ios core-data xml-parsing

我的应用程序出现问题,我试图通过复制标签来保存核心数据中的内容,但它给了我这个错误

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSCFString appendString:]:nil参数'

这是代码

- (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; 

}

2 个答案:

答案 0 :(得分:0)

我认为你在这里有错误的陈述:

    if( (clientName.text) || (clientAccount.text) || (clientMobile.text) != NULL)

看起来应该是这样的(如果我没记错的话):

if( (clientName.text != NULL) && (clientAccount.text != NULL) && (clientMobile.text != NULL))

答案 1 :(得分:0)

在if语句中,您同时检查所有三个UILabel的零文本。这意味着,如果其中一个中有文本而另外两个为空,则if条件将返回YES。然后尝试将此空文本分配给自定义对象。

您应该修改if语句以分别引用每个字段。因此:

if (clientAccount.text != nil)  client.ACCOUNT = clientAccount.text;
if (clientName.text != nil)     client.NAME = clientName.text;
if (clientMobile.text != nil)   client.MOBILE = clientMobile.text;

如果这不能解决问题,请发布导致崩溃的确切行。