• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Best way to build relative pathname?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best way to build relative pathname?


  • Subject: Re: Best way to build relative pathname?
  • From: Lorenzo <email@hidden>
  • Date: Mon, 12 May 2003 11:32:26 +0200

Hi Uli,
In Project Builder you can easily take a look at the
"NSString (Objective C)" help file or "NSPathUtilities.h" file
To do that (if you don't know yet) go to the Find panel
then start a search for "NSPathUtilities". Then click on the "book" icon in
the result list.

I show you here some sample. I try to be simplest I can.
I used this pathname in the following samples
myPath = @"/User/John/Documents/Personal/Bills.rtf


////////////////////////////////////////
- (NSString *)stringByDeletingLastPathComponent
e.g.
myPath = [myPath stringByDeletingLastPathComponent];
fileName is equal to @"/User/John/Documents/Personal"
then again,
myPath = [myPath stringByDeletingLastPathComponent];
fileName is equal to @"/User/John/Documents"


////////////////////////////////////////
- (NSString *)stringByAppendingPathComponent:(NSString *)aString
To add a path component you don't need to add the slash:
continuing from the last sample...
myPath = [myPath stringByAppendingPathComponent:@"Personal"];
fileName is equal to @"/User/John/Documents/Personal"


////////////////////////////////////////
- (NSArray *)pathComponents
it gives an array. Each element of the array is an element of the path.
e.g.

NSArray *pathArray = [myPath pathComponents];
it gives:
[myPath objectAtIndex:0] is @"/"
[myPath objectAtIndex:1] is @"User"
[myPath objectAtIndex:2] is @"John"
etc. etc.

You can use this API this way:
for(i = 0; i<[myPath count]; i++){
firstItem = [myPath objectAtIndex:i];
if([firstItem isEqualToString:yourItem]){
// do what you need
}
}


////////////////////////////////////////
+ (NSString *)pathWithComponents:(NSArray *)components;
this is the inverse API of the previous one.
myPath = [NSString pathWithComponents:pathArray];



////////////////////////////////////////
- (NSString *)lastPathComponent;
e.g.
fileName = [myPath lastPathComponent];
fileName is equal to @"Bills.rtf"


Then if you appy this
////////////////////////////////////////
- (NSString *)stringByDeletingPathExtension;
fileName = [fileName stringByDeletingPathExtension];
fileName is equal to @"Bills"




Best Regards
--
Lorenzo
email: email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Best way to build relative pathname?
      • From: "M. Uli Kusterer" <email@hidden>
  • Prev by Date: Re: Force display of spinning busy cursor?
  • Next by Date: [ANNOUNCE] GeekTool 2.0
  • Previous by thread: Best way to build relative pathname?
  • Next by thread: Re: Best way to build relative pathname?
  • Index(es):
    • Date
    • Thread