Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
File URL with characters
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Mark FX" data-source="post: 1584821" data-attributes="member: 211556"><p>Firstly you would not use this [[nsopenpanle URLs]objectAtindex:0]; line of code in isolation like you've done.</p><p>You would probably do something like this.</p><p></p><p>[CODE]</p><p>NSURL *panelURL = [[nsopenpanle URLs]objectAtindex:0];</p><p>[/CODE]</p><p></p><p>This would give you an NSURL class object if your NSOpenPanel did indeed return a file url and not an error.</p><p>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</p><p></p><p>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.</p><p>So something like this would be more suitable.</p><p></p><p>[CODE]</p><p>NSOpenPanel *myPanel = [NSOpenPanel openPanel];</p><p>[myPanel runModal];</p><p> </p><p>if ([[myPanel URLs] objectAtIndex:0] != nil) {</p><p> NSURL *panelURL = [[myPanel URLs] objectAtIndex:0];</p><p> NSString *urlString = [[[panelURL absoluteString] stringByRemovingPercentEncoding] lastPathComponent];</p><p> NSLog(@"%@", urlString);</p><p>}</p><p>[/CODE]</p><p></p><p>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.</p><p></p><p>The - (NSString *)absoluteString method of the NSURL class will return a NSString object.</p><p>The - (NSString *)stringByRemovingPercentEncoding method of the NSString class will remove the percent encoding, and replace with the original characters.</p><p>And finally the - (NSString *)lastPathComponent method of the NSString class will remove all of the folder path components, leaving only the file name remaining.</p><p></p><p>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.</p><p></p><p>Regards Mark</p></blockquote><p></p>
[QUOTE="Mark FX, post: 1584821, member: 211556"] 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]; [/CODE] 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); } [/CODE] 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 [/QUOTE]
Verification
Name this item 🌈
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
File URL with characters
Top