SUDHAKAR RAYAPUDI SUDHAKAR RAYAPUDI - 2 months ago 13
iOS Question

How to delete last path component of a String in Swift?

I've a String

11/Passion/01PassionAwakening.mp3


I need to delete last path component

01PassionAwakening.mp3


then it looks like

11/Passion


How can Do it?

and I need both the components for my need

Answer

You can separate your url in two part. Which is given below

    let str  : NSString = "www.music.com/Passion/PassionAwakening.mp3" 
    let path : NSString = str.stringByDeletingLastPathComponent
    let ext  : NSString = str.lastPathComponent

    print(path)
    print(ext)

Output

www.music.com/Passion
PassionAwakening.mp3

For more info please have a look

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/ManipulatingPaths.html