objective-c - 检查文件是否存在 objective-c

标签 objective-c cocoa

我使用下面的代码来检查文件是否存在

bool b=[[NSFileManager defaultManager] fileExistsAtPath:filePath];

代码适用于 IOS。

但是当我将它迁移到 mac os x 时,

但是我通过filePath发现文件是否存在于磁盘上, b 总是返回 0,表示文件不存在。

请问ios和macosx有区别吗

欢迎任何评论

最佳答案

从文档中,我认为您正在使用带 波浪号 的路径

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/fileExistsAtPath:isDirectory :

路径

The path of a file or directory. If path begins with a tilde (~), it must first be expanded with stringByExpandingTildeInPath, or this method will return NO.

波浪号 使路径相对于您的主目录(例如/Users/username/)

你可以通过调用 NSLog(@"%@",filePath); 来判断 filePath 是否是 NSString 类型

关于objective-c - 检查文件是否存在 objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7866625/

相关文章:

objective-c - 如何更改 Xcode 项目中的默认 MainMenu.xib 文件?

ios - 如何让用户仅执行特定操作有限次数?

ios - AVAudioPlayer速率变化会引入失真/失真

ios - 以编程方式将选择器添加到 UIButton

iphone - UILabel sizeToFit 和约束

macos - 如何在不继承 NSButtonCell 的情况下创建自定义主题的 NSButton?

objective-c - UITableView Cells 的图像改变了吗?

objective-c - Cocoa OS X NSLevelIndicator 类型

iphone - Objective C iPhone 性能问题

Swift StatusBarItem,使按钮支持拖放