我的一个应用程序,我从本地主机解析一些数据并在表格视图中打印它。要获取数据,用户首先使用警报视图登录。输入的用户ID然后用于获取我使用JSON解析的数据。
这个问题肯定有一个非常简单的解决方案,但我似乎无法修复它。问题是,当我打印数据时,字符串的格式为:
(“string”)
但我想要它只是说:string
在表格视图中。这是我的解析方法:
- (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;
当我在解析过程中使用log时,它出现为(“string”)所以问题出在解析的某个地方,至少这就是我的想法。
答案 0 :(得分:4)
如果
NSString *string = [[NSString alloc] initWithFormat:@"%@",[BooksBorrowed objectAtIndex:indexPath.row]];
返回类似“(string)”的内容,那么最可能的原因是
[BooksBorrowed objectAtIndex:indexPath.row]
不是字符串,而是包含字符串的数组。在那种情况下,
NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];
应该是解决方案。
答案 1 :(得分: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
NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];
。
你需要首先从数组中获取该字符串,然后显示,使用@Martin R建议的代码行。
{{1}}