我使用此代码在项目中创建了一个目录
NSError * error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/createDirectory1"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
NSLog(@"Create a new Dir");
}
else
{
NSLog(@"Dir already exist");
}
现在我要检查目录是否存在?我怎样才能通过客观的c代码得到它?
答案 0 :(得分:1)
import UIKit
class NavigationBarView: UIView {
@IBOutlet var view: UIView!
required init(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
NSBundle.mainBundle().loadNibNamed("NavigationBarView", owner: self, options: nil)
self.addSubview(self.view)
}
}
答案 1 :(得分:0)
首先,你应该得到项目路径。你可以像下面的代码那样做。
NSString *getProjectPath(){
NSString *projectPath = [NSString stringWithFormat:@"%s,__FILE__]; // your project path may have some same prefix to the macro __FILE__.
// do something
return projectPath;
}
然后你用 [[NSFileManager defaultManager] fileExistsAtPath:dataPath]检查它是否存在
答案 2 :(得分:0)
BOOL fileOrDirectoryExists = [[NSFileManager defaultManager] fileExistsAtPath:dataPath];
fileExistsAtPath:dataPath
方法返回一个布尔值,指示文件或目录是否存在于指定路径中。你已经在做了,正如雷木思所说的那样。