2012-11-15 42 views
4

现在我正在研究需要NSFileManager的iOS应用程序,但是当我执行attributesOfItemAtPath:方法时,它将返回null。 NSData和dataWithContentsOfFile:方法也失败。我不明白为什么会发生这种情况。我使用的文件URL如下所示:file://localhost/private/var/mobile/Applications/597DE145-33D7-4F92-AE95-029D5CF15291/tmp/Kv7DWqtRWH7WnN47HdDW。我怀疑它可能与URL有关,但我不确定。NSFileManager Attributesforitem失败并返回空

回答

13

您需要将NSURL转换为适用于接受字符串的任何NSFileManager方法的路径。通过调用URL上的path方法来完成此操作。

NSURL *someURL = ... // some file URL 
NSString *path = [someURL path]; // convert the file:// URL to a file path 
NSDictionary *info = [[NSFileManager defaultManager] attributesOfItemAtPath:path]; 
+2

我希望我可以upvote这一百万次 – virindh

+0

我在那里与你:) –