gpt4 book ai didi

iphone - 使用 NSDictionary 'initWithContentsOfURL' 代替 Reachability.h

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:07 39 4
gpt4 key购买 nike

我想更好地理解 NSDictionaryinitWithContentsOfURL 的功能。
此函数自行管理连接失败?
来自 NSDictionary 引用的 initWithContentsOfURL:

- (id)initWithContentsOfURL:(NSURL *)aURL

Return Value

An initialized dictionary-which Might be different than the original-that contains the receiver at aURL dictionary, or nil if there is an error or if the contents of the resource are an invalid representation of a dictionary.

好的,但不指定传递的 url 是否有效。

但是因为我确定我的 plist 格式正确,所以我可以使用有问题的方法来查看连接是否可用,而不是使用 Reachability.h 。当然只是了解数据连接是否可用,而不是了解哪种连接处于事件状态(例如 WiFi 等)。我这样说是因为如果我在飞行模式下做这样的简单测试,[dict count]; 总是返回 0。

NSURL * plist = [NSURL URLWithString: @ "http://www.example.com/example.plist"];
NSDictionary * dict = [[[NSDictionary alloc] initWithContentsOfURL:plist] autorelease];
if ([dict count] == 0) {
//no connection
}

TIA。

最佳答案

是的,你可以做到。请记住,[[NSDictionary alloc] initWithContentsOfURL:plist] 是一个同步阻塞调用。如果您阻塞主线程的时间过长,那么您将得到 0x8BADF00D,看门狗将终止您的进程。

关于iphone - 使用 NSDictionary 'initWithContentsOfURL' 代替 Reachability.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7969417/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com