How to create an FSRef from an NSString containing a path?

I have a file system path in NSString, but I need FSRef for the system call that I will make. What is the best way to create FSRef?

+3
source share
3 answers

Try FSPathMakeRef:

NSString *path = @"Some... path... here";
FSRef f;
OSStatus os_status = FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &f, NULL);

if (os_status == noErr) {
    NSLog(@"Success");
}
+5
source

You can use FSPathMakeRef()to make FSRefUTF-8 C from a string path, and you can use a -UTF8Stringmethod NSStringto get UTF-8 C string:

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()
+2
source

Nathan Day NSString + NDCarbonUtilities:

- (BOOL)getFSRef:(FSRef *)aFSRef
{
    return FSPathMakeRef( (const UInt8 *)[self fileSystemRepresentation], aFSRef, NULL ) == noErr;
}

. NDAlias ​​ http://homepage.mac.com/nathan_day/pages/source.xml ( MIT).

+1
source

Source: https://habr.com/ru/post/1710194/


All Articles