代码之家  ›  专栏  ›  技术社区  ›  Mike A

arrayWithContentsOfURL的正确用法是什么?

  •  4
  • Mike A  · 技术社区  · 16 年前

    我有www.myhost.com/test.html

    <array><string>test</string><string>test2</string></array>
    

    那我有

    NSURL *baseURL = [NSURLRLWithString:@"http://www.myhost.com/test.html"];
    NSArray *array = [NSArray arrayWithContentsOfURL:baseURL];
    NSLog(@"%@", [array description]);
    

    它一直返回(null)。我错过了什么?谢谢

    3 回复  |  直到 13 年前
        1
  •  5
  •   cobbal    16 年前

    它可能应该是一个完整的plist格式(带有doctype等)

    The man page

    创建适当plist的最简单方法是使用“Property List Editor.app”

    旁注: NSLog(@"%@", [array description]); 与 NSLog(@"%@", array);

        2
  •  5
  •   luvieere    16 年前

    arrayWithContentsOfURL documentation 明确表示,

    由aURL确定的位置必须 (NSString、NSData、NSArray或 NSDictionary对象)。

    也就是说,通过调用 writeToURL:atomically arrayWithContentsOfURL: 或者实例方法 initWithContentsOfURL: .

        3
  •  0
  •   Brad Miller    16 年前

    我的另一个发现是,如果您试图动态构建列表,以便通过web提供服务,而不是来自使用plist编辑器创建的静态文件,则必须转义字符串。如果文档中有任何xml错误,则不会加载数组。例如我有一个&在我的一些字符串中,仍然得到一个空数组,直到我转义字符串并替换&具有 &amp;