簡體   English   中英

如何從包含路徑的NSString創建FSRef?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM