Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Created May 19, 2018 06:18
Show Gist options
  • Save KentarouKanno/03f9c3e2f487bcaea94ba3f35f65fcde to your computer and use it in GitHub Desktop.
Save KentarouKanno/03f9c3e2f487bcaea94ba3f35f65fcde to your computer and use it in GitHub Desktop.

参考URL: Stringのpath操作系のメソッドをExtensionで復活させる

extension String {
    
    private var ns: NSString {
        return (self as NSString)
    }
    
    public func substring(from index: Int) -> String {
        return ns.substring(from: index)
    }
    
    public func substring(to index: Int) -> String {
        return ns.substring(to: index)
    }
    
    public func substring(with range: NSRange) -> String {
        return ns.substring(with: range)
    }
    
    public var lastPathComponent: String {
        return ns.lastPathComponent
    }
    
    public var pathExtension: String {
        return ns.pathExtension
    }
    
    public var deletingLastPathComponent: String {
        return ns.deletingLastPathComponent
    }
    
    public var deletingPathExtension: String {
        return ns.deletingPathExtension
    }
    
    public var pathComponents: [String] {
        return ns.pathComponents
    }
    
    public func appendingPathComponent(_ str: String) -> String {
        return ns.appendingPathComponent(str)
    }
    
    public func appendingPathExtension(_ str: String) -> String? {
        return ns.appendingPathExtension(str)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment