Re: Why is NSString->FSRef so hard?
Re: Why is NSString->FSRef so hard?
- Subject: Re: Why is NSString->FSRef so hard?
- From: Mark Douma <email@hidden>
- Date: Wed, 29 Apr 2009 09:15:16 -0400
On Apr 28, 2009, at 1:34 AM, Erg Consultant wrote:
if( glBundle ) {
// Get main bundle info dict...
d = [ glBundle infoDictionary ];
if( d ) {
// Get the GL bundle's path from the info dict...
z = [ d objectForKey:kNSBundleInitialPathInfoDictKey ];
if( z ) {
// Break the path up into a path component array...
arr = [ z
componentsSeparatedByString:kSlashStringKey ];
// Make a mutable path string to manipulate...
m = [ NSMutableString stringWithCapacity:0 ];
}
}
}
if( m && arr ) {
for(i=0; i<[ arr count] - 1; i++ ) {
[m appendString:[arr objectAtIndex:i] ];
[m appendString:@"/" ];
}
//go up one level to get at Info.plist
[m appendString:@"Info.plist" ];
dd = [ NSDictionary dictionaryWithContentsOfFile:m ];
zzz = [ dd valueForKey:@"CFBundleExecutable" ];
//reset
[ m setString:@"" ];
//Loop again appening all path compoents to final file to
launch...
for(i=0; i<[ arr count] - 1; i++ ) {
[m appendString:[arr objectAtIndex:i] ];
[m appendString:@"/" ];
}
//now get full path to the file we want to actually run...
[ m appendString:zzz ];
[ m appendString:@".ifn" ];
Wow, you are making this problem way, way harder than it needs to
be. ;-)
If you are working with file paths, you shouldn't be using
componentsSeparatedByString:, nor should you be defining "/" to be the
component you should be separating by. What if someone had your app
inside of a folder they named "Apps/Utilities"? The HFS+ filesystem
actually uses a colon as the path separator, so having a / in the name
of a file or folder is perfectly acceptable, but would likely cause a
headache and unexpected results if your code were to encounter it. (Go
to the Finder and try adding a /).
When dealing with file paths, you should be using the following methods:
+ (NSString *)pathWithComponents:(NSArray *)components;
- (NSArray *)pathComponents;
- (NSString *)lastPathComponent;
- (NSString *)stringByDeletingLastPathComponent;
- (NSString *)stringByAppendingPathComponent:(NSString *)str;
- (NSString *)pathExtension;
- (NSString *)stringByDeletingPathExtension;
- (NSString *)stringByAppendingPathExtension:(NSString *)str;
- (NSString *)stringByStandardizingPath;
Note that these aren't in the NSString.h header itself, but in a
separate NSPathUtilities.h file.
Using these higher-level commands will insulate you from even having
to worry about what the path separator even is (it will be handled for
you). The same goes for adding a filename extensions: [fileName
stringByAppendingString:@".jpg"] should be [fileName
stringByAppendingPathExtension:@"jpg"].***
Hope this helps,
Mark
*** Adobe developers, this means you! Maybe then I wouldn't have a
folder in my user library folder whose name is "Application Support/
Adobe/Acrobat".
---------------------------------------------------------------------------
Mark Douma
Grand Rapids, MI, USA
email@hidden
http://homepage.mac.com/mdouma46/
---------------------------------------------------------------------------
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden