Objective c 如何从包含路径的NSString创建FSRef?
我有一个NSString格式的文件系统路径,但是我需要一个FSRef用于我将要进行的系统调用。创建FSRef的最佳方法是什么?试试FSPathMakeRef:Objective c 如何从包含路径的NSString创建FSRef?,objective-c,cocoa,macos,filesystems,Objective C,Cocoa,Macos,Filesystems,我有一个NSString格式的文件系统路径,但是我需要一个FSRef用于我将要进行的系统调用。创建FSRef的最佳方法是什么?试试FSPathMakeRef: NSString *path = @"Some... path... here"; FSRef f; OSStatus os_status = FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &f, NULL); if (os_status == noE
NSString *path = @"Some... path... here";
FSRef f;
OSStatus os_status = FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &f, NULL);
if (os_status == noErr) {
NSLog(@"Success");
}
您可以使用从UTF-8 C字符串路径生成FSRef
,也可以使用NSString
的方法获取UTF-8 C字符串:
FSRef fsref;
Boolean isDirectory;
OSStatus result = FSPathMakeRef([myString UTF8String], &fsref, &isDirectory);
if(result < 0)
// handle error
// If successful, fsref is valid, and isDirectory is true if the given path
// is a directory. If you don't care about that, you can instead pass NULL
// for the third argument of FSPathMakeRef()
FSRef-FSRef;
布尔isDirectory;
OSStatus result=FSPathMakeRef([myString UTF8String],&fsref,&isDirectory);
如果(结果<0)
//处理错误
//如果成功,则fsref有效,如果给定路径
//是一个目录。如果您不关心这一点,可以改为传递NULL
//对于FSPathMakeRef()的第三个参数
您可以在Nathan Day的NSString+NDCarbonUtilities类别中使用此方法:
- (BOOL)getFSRef:(FSRef *)aFSRef
{
return FSPathMakeRef( (const UInt8 *)[self fileSystemRepresentation], aFSRef, NULL ) == noErr;
}
更多信息请参见NDAlias(麻省理工学院许可)