iOS

NSURL

How to get last string component from NSURL String.

 NSURL *url = [NSURL URLWithString:@"https://www.example.com/images/apple-tree.jpg"];
 NSString *fileName = [url lastPathComponent];
 // fileName = "apple-tree.jpg"

How to get last string component from URL (NSURL) in Swift

Swift 2.3

let url = NSURL(string: "https://google.com/lastPath")
let lastPath = url?.lastPathComponent

Swift 3.0

  let url = URL(string: "https://google.com/lastPath")
  let lastPath = url?.lastPathComponent

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow