Iphone 用字符串对象的内容填充数组

Iphone 用字符串对象的内容填充数组,iphone,ios,xcode,arrays,string,Iphone,Ios,Xcode,Arrays,String,我有一个从URL获取内容的字符串。我正在尝试将这些内容放入一个数组,该数组将填充一个表视图。这是我的密码。我做错了什么?提前谢谢 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSString *strURL = [NSString stringWithFormat:@"http://10.247.245.87/index.php"];

我有一个从URL获取内容的字符串。我正在尝试将这些内容放入一个数组,该数组将填充一个表视图。这是我的密码。我做错了什么?提前谢谢

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *strURL = [NSString stringWithFormat:@"http://10.247.245.87/index.php"];

    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

    NSArray *nameArray = [[NSArray alloc]initWithContentsOfURL:<#(NSURL *)#>;

    return nameArray.count;

}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
NSString*strURL=[NSString stringWithFormat:@]http://10.247.245.87/index.php"];
NSData*dataURL=[NSData datawithcontentsofull:[NSURL URLWithString:strURL]];
NSString*strResult=[[NSString alloc]initWithData:dataURL编码:NSUTF8StringEncoding];
NSArray*nameArray=[[NSArray alloc]initWithContentsOfURL:;
返回nameArray.count;
}
在这行中:

return nameArray.count;

您将始终得到0,因为您不等待serwer的响应。

NSData
之后使用此选项

NSArray *array =   [NSJSONSerialization JSONObjectWithData:dataURL options:NSJSONReadingAllowFragments error:nil];

return array;

我相信有几种方法可以做到这一点,但这里有一种将JSON结果解析为数组的简单方法 然后将JSON.h文件导入您的
#导入“JSON.h”
。之后,您可以使用以下代码行
nameArray=[responseString JSONValue];
将字符串解析为数组

  • 从中获取SBJSON
  • 将SBJSON添加到项目中并导入JSON.h,就像导入“JSON.h”
  • 像这样解析数组
    nameArray=[responseString JSONValue];
  • 快乐编码

    编辑: 在将JSON解析为数组后,您可以尝试执行以下操作以检查是否有字符串数组:

    对于(nameArray中的NSString*myString){
    NSLog(@“%@”,myString);
    }

    如果上述方法可行,则可以从数组中获取字符串,并在
    cellforrowatinexpath
    委托中填充tableview,如下所示:

    cell.textLabel.text = [nameArray objectAtIndex:indexPath.row];
    

    您希望如何用字符串填充数组?是否为.csv样式,不同的元素由某个字符分隔?您得到了什么响应..在浏览器中运行URL后???@AppleDelegate我得到了一系列用逗号分隔的名称。@Rickay我希望用该字符串的内容填充数组,该字符串用JSON编码。我们b浏览器显示以逗号分隔的数据(名称)。以下是它在浏览器中的外观:[“Young,Jason”,“Fairfield,John”,“Youngberg,Molly”]当我在“return”语句的正上方添加这一行时,调试窗口会立即填充所有内容。NSLog(@“%@”,stresult);这个答案不正确。无论在哪个线程上执行此操作,都将被挂起,直到收到服务器的响应。如果在主线程上执行,则线程将被挂起。我同意Rickay所说的,也就是说,将代码尽可能远离主线程。当然。甚至更好r、 使用NSURLConnection可以更好地控制错误和异步操作。这个答案给了我以下编译警告:“从结果类型为'NSInteger'的函数返回'NSArray*\u strong'的整数转换的不兼容指针”(也称为'int');谢谢你的帮助!我会试试看它的去向!好的,这可以根据有多少数据来确定有多少行。现在我需要在表行中显示数据。我知道这与此有关:
    -(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath{UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@“cell”forIndexPath:indexPath];静态NSString*CellIdentifier=@“cell”;if(cell==nil){cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}返回单元格;}
    不客气,如果您需要任何其他帮助,请告诉我。请注意,如果您的项目启用了ARC,您可能需要为来自SBJSON框架的文件添加“fno objc ARC”编译器标志(不过似乎有一个稳定的ARC版本)。通常情况下,您会得到一个字典数组,但我使用的JSON结构比您提供的示例稍微复杂一些。我建议您只需迭代该数组,并尝试从每个索引的内容实例化NSString对象。您能给我举个例子吗?您所说的完全超出了我的理解:-)