[英]How do I create an FSRef from a NSString containing a path?
我在NSString中有一个文件系统路径,但是我将要进行的系统调用需要FSRef。 创建FSRef的最佳方法是什么?
尝试FSPathMakeRef:
NSString *path = @"Some... path... here";
FSRef f;
OSStatus os_status = FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &f, NULL);
if (os_status == noErr) {
NSLog(@"Success");
}
您可以使用FSPathMakeRef()
作出FSRef
从UTF-8的C字符串路径,可以使用-UTF8String
的方法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()
您可以从Nathan Day的NSString + NDCarbonUtilities类别中使用此方法:
- (BOOL)getFSRef:(FSRef *)aFSRef
{
return FSPathMakeRef( (const UInt8 *)[self fileSystemRepresentation], aFSRef, NULL ) == noErr;
}
有关更多信息,请参见http://homepage.mac.com/nathan_day/pages/source.xml上的 NDAlias(已获得MIT许可)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.