File URL with characters

Joined
Jun 8, 2013
Messages
158
Reaction score
0
Points
16
Location
Būr said,Egypt
Your Mac's Specs
MacBook Pro (i5)
hello,guys.
i'm objective-c/OS X developer and my question is:i want to get a file URL from NSopenpanel but when i write [[nsopenpanle URLs]objectAtindex:0];
i receive the URL of the file but if the file has a character then i get %20 and so on,for example if the file is called ">Arrow.png" then it shows up like %60Arrow.png
so i want to receive it with out these numbers i just want to receive the character.
any help will be appreciated,thanks
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
Firstly you would not use this [[nsopenpanle URLs]objectAtindex:0]; line of code in isolation like you've done.
You would probably do something like this.

Code:
NSURL *panelURL = [[nsopenpanle URLs]objectAtindex:0];

This would give you an NSURL class object if your NSOpenPanel did indeed return a file url and not an error.
So as you've not posted the relevent code which you are using in your project, it makes it very hard for people to help you

Although I'm guessing your trying to get a String representation of the file URL, in which case you need to create a NSString object from the file URL.
So something like this would be more suitable.

Code:
NSOpenPanel *myPanel = [NSOpenPanel openPanel];
[myPanel runModal];
        
if ([[myPanel URLs] objectAtIndex:0] != nil) {
    NSURL *panelURL = [[myPanel URLs] objectAtIndex:0];
    NSString *urlString = [[[panelURL absoluteString] stringByRemovingPercentEncoding] lastPathComponent];
    NSLog(@"%@", urlString);
}

I've kept the code very simple for you and others to understand, and I've also not put in any error checking which you might want to do in your project.

The - (NSString *)absoluteString method of the NSURL class will return a NSString object.
The - (NSString *)stringByRemovingPercentEncoding method of the NSString class will remove the percent encoding, and replace with the original characters.
And finally the - (NSString *)lastPathComponent method of the NSString class will remove all of the folder path components, leaving only the file name remaining.

In future I would advise posting the actual relevant code as used in your project, as this makes it easier for people to help you, rather than them guessing what you trying to achieve.

Regards Mark
 
OP
Yodda_Hunter
Joined
Jun 8, 2013
Messages
158
Reaction score
0
Points
16
Location
Būr said,Egypt
Your Mac's Specs
MacBook Pro (i5)
Thanks alot,i'll keep it in mind ;)
And about the codes i'll try them and tell you if things went cool!
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top