ios - 将 NSURL 的内容写入数组

标签 ios uisplitview

我有一个程序可以从链接中检索数据,然后像这样将它写到日志中。

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/

相关文章:

ios - 后台模式下的位置更新仅需 10 分钟

ios - 如何使用 Swift 将文本文件逐行加载到数组中?

ios - 丢弃从右到左在 iOS 中不起作用

ios - 具有 UITabBar 行为的 UISplitview

ios - 在分割 View 中向原型(prototype)单元添加自定义标签

ios - Split View Controller 只显示一个 childviewcontroller

objective-c - 如何更新 splitViewController 中的详细 View ?

ios - 在 QLPreviewController swift 中禁用捏合以解除手势

ios - "pushes"匹配 UIInterpolatingMotionEffect? (即访问 UIInterpolatingMotionEffect 上的 "physics")

ios - UISplitView:当分割 View 弹出窗口出现时关闭另一个弹出窗口