How can I remove part of NSURL and keep the result in NSString format?
How can I format it file:///Applications/App%20Store.app
to be stored as an App Store string? I want to remove everything up to "App% 20Store.app" and automatically add space for% 20 to the new saved row.
My code takes user input from NSOpenPanel()
and stores the result shown above into an NSURL variable.
EDIT . He works with it! Thanks to
var URLinString : String = selectedApp!.absoluteString!
let newString = URLinString.stringByReplacingOccurrencesOfString("file:///Applications/", withString: "")
let newerString = newString.stringByReplacingOccurrencesOfString("%20", withString: " ")
+3
source to share
4 answers
Objective-C
NSURL *url = [NSURL URLWithString:@"file:///Applications/App%20Store.app"];
NSString *lastComponent = [url lastPathComponent];
NSLog(@"Last path component:%@", lastComponent);
Swift
var url = NSURL(string:"file:///Applications/App%20Store.app")
var lastComponent = url?.lastPathComponent
println("\(lastComponent)")
if lastComponent == "App Store.app" {
println("Yes")
}
Console
Optional("App Store.app")
Yes
+12
source to share
According to what you say, you want to get the filename.
You can use methods of the NSString class (Edited thanks to comments)
In Objective-C :
NSURL *myNSURL = [NSURL URLWithString:@"file:///Applications/App%20Store.app"];
//Extract filename and remove file extension
NSString* fileName = [[myNSURL lastPathComponent] stringByDeletingPathExtension];
In Swift :
var myNSURL = NSURL(string:"file:///Applications/App%20Store.app")
//Extract filename and remove file extension
var fileName = myNSURL?.lastPathComponent?.stringByDeletingPathExtension
+2
source to share