3

我的一个应用程序,我从本地主机解析一些数据并将其打印在表格视图中。要获取数据,用户首先使用警报视图登录。然后使用输入的用户 ID 来获取我使用 JSON 解析的数据。

这个问题肯定有一个非常简单的解决方案,但我似乎无法解决它。问题是当我打印数据时,字符串以这种格式出现:

( “细绳” )

但我希望它只是说:字符串

在表格视图中。这是我的解析方法:

 - (void)updateMyBooks
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Fetch data on a background thread:

    NSString *authFormatString =
    @"http://localhost:8888/Jineel_lib/bookBorrowed.php?uid=%@";

    NSString *string = [[NSString alloc]initWithFormat:@"%@",UserID];

    NSString *urlString = [NSString stringWithFormat:authFormatString, string];

    NSURL *url = [NSURL URLWithString:urlString];

    NSLog(@"uel is %@", url);

    NSString *contents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    response1 = [contents JSONValue];

    if (contents) {

        // ... Parse JSON response and add objects to newBooksBorrowed ...
        BookName = [[NSString alloc]init];
        DateBorrowed = [[NSString alloc]init];
        BookID = [[NSString alloc]init];
        BookExtended = [[NSString alloc]init];
        BookReturned = [[NSString alloc]init];

        BookName = [response1 valueForKey:@"BookName"];
        BookID = [response1 valueForKey:@"BookID"];
        DateBorrowed = [response1 valueForKey:@"DateBorrowed"];
        BookExtended = [response1 valueForKey:@"Extended"];
        BookReturned = [response1 valueForKey:@"Returned"];

        dispatch_sync(dispatch_get_main_queue(), ^{
            // Update data source array and reload table view.
            [BooksBorrowed addObject:BookName];
            NSLog(@"bookBorrowed array = %@",BooksBorrowed);
            [self.tableView reloadData];
        });
    }
});
}

这就是我在表格视图中打印它的方式:

NSString *string = [[NSString alloc] initWithFormat:@"%@",[BooksBorrowed objectAtIndex:indexPath.row]];

NSLog(@"string is %@",string);
cell.textLabel.text = string;

当我在解析过程中使用日志时,它会显示为(“字符串”),所以问题出在解析的某个地方,至少我是这么认为的。

4

2 回答 2

4

如果

NSString *string = [[NSString alloc] initWithFormat:@"%@",[BooksBorrowed objectAtIndex:indexPath.row]];

返回类似“(字符串)”的东西,那么最可能的原因是

[BooksBorrowed objectAtIndex:indexPath.row]

不是字符串,而是包含字符串的数组。在这种情况下,

NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];

应该是解决方案。

于 2013-03-22T10:33:50.183 回答
2
    NSString *string = [[NSString alloc] initWithFormat:@"%@",[BooksBorrowed objectAtIndex:indexPath.row]];
    string = [string stringByReplacingOccurrencesOfString:@"(" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@")" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""];


    NSLog(@"string is %@",string);
    cell.textLabel.text = string;

编辑:

如果它在标签中显示该格式的文本,则使用上面的代码。

如果你在里面看到它,NSlog那么它就在NSString里面NSArray。您需要先从数组中获取该字符串,然后显示,使用@Martin R 建议的代码行。

NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];
于 2013-03-22T10:22:43.343 回答