我有一个程序可以从链接中检索数据,然后像这样将它写到日志中。
NSURL *getURL=[NSURL URLWithString:@"link.php"];
NSError *error=nil;
NSString *str=[NSString stringWithContentsofURL:getURL encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%",str);
这会按预期将我的 php 中的三个值打印到日志中。
但是我在将其保存在一个数组中时遇到了一些困难,该数组然后将这些值显示在 UISplitviewController
(左侧 Controller 侧)中。
是这样写的
showArray=[[NSMutableArray alloc]initWithContentofURL:getURL];
然后在cellForRowAtIndexPath:
方法是
cell.textLabel.text=[showArray object atIndex:indexPath.row];
我尝试的第二件事是将 myURL 写入一个数组,并尝试像这样用 ContentsofArray
初始化 showArray
NSArray *retults=[NSArray arraywithContentsOFURL:getURL];
showArray=[[NSArray alloc]initWithArray:retults];
但是那行不通 但是如果我说
showArray=[[NSMutableArray alloc]initWithObjects:@"One",@"Two",nil];
在我的左 View Controller 中显示一个和两个.... 希望有人能帮我解决这个问题……谢谢
最佳答案
您要添加 URL 的内容还是 URL 本身?
如果您尝试仅添加 URL,请使用:
showArray = [@[getURL] mutableCopy];
但是,如果您尝试添加 URL 的内容,则 the doc clearly states URL 必须表示数组的字符串表示形式。
此外:
Returns nil if the location can’t be opened or if the contents of the location can’t be parsed into an array.
编辑:
我看到了您对帖子的评论,并且您的数据看起来像 JSON 数据。
您应该看一下 NSJSONSerialisation
类,它使用起来非常简单(您会在此处找到很多有关 SO 的示例)。
关于ios - 将 NSURL 的内容写入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21881696/