【问题标题】:What are keys and how do I use them?什么是密钥以及如何使用它们?
【发布时间】:2012-07-03 16:52:09
【问题描述】:

我正在开发一个与 iCloud 兼容的应用程序,并且我正在研究如何检测文件是否正在上传/下载或已完成。 我发现这可以通过 NSURL "keys," 检测到, 例如 NSURLUbiquitousItemIsDownloadingKeyNSURLUbiquitousItemIsUploadingKey。我还在努力学习编程,那么这些键是什么?如何使用它们来检测文件的状态(我希望应用知道文件何时完成上传到 iCloud 或完成下载(无论设备在哪一侧))。

我读到我可以使用resourceValuesForKeys:error: 来查询这些键的状态,那么我是否将其放入 IF 语句中,看看结果是否符合预期,例如“是”或“否”?感谢您的帮助。

if ([destination resourceValuesForKeys:[NSArray arrayWithObject:NSURLUbiquitousItemIsUploadingKey] error:NULL]) {

    //is uploading??

}

【问题讨论】:

    标签: iphone objective-c ios ipad nsurl


    【解决方案1】:

    您提出的代码看起来几乎可行,但一方面:resourceValuesForKeys:error: 返回一个字典,其键与您传入的常量相同,其值与the documentation for those keys 中指定的值相同。在NSURLUbiquitousItemIsUploadingKey 的情况下,该值是一个包装BOOL 值的NSNumber 实例。

    所以...假设 destination 是一个 NSURL 指向您的无处不在容器中的一个项目:

    NSError *error;
    NSArray *keys = [NSArray arrayWithObject:NSURLUbiquitousItemIsUploadingKey];
    NSDictionary *values = [destination resourceValuesForKeys:keys error:&error];
    if (values == nil)
        NSLog(@"error: %@", error);
    else if ([[values objectForKey:NSURLUbiquitousItemIsUploadingKey] boolValue])
        NSLog(@"uploading");
    else
        NSLog(@"not uploading");
    

    如果你只查询一个键,你可以使用getResourceValue:forKey:error:更简洁一点。

    【讨论】:

      猜你喜欢
      • 2011-05-14
      • 2011-07-04
      • 2016-12-23
      • 2017-10-26
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 2014-01-22
      相关资源
      最近更新 更多