0

I am trying to create a file name from the URL. I have to forcefully remove the first item in the array every time. Like the C# implementation where they check for empty string.

Is it possible to remove the "/" in the array? Is there a better way to implement this?

let url = "https://stackoverflow.com/questions/ask"
var filePathComponents:[String]  = []
filePathComponents = assetURL.pathComponents as! [String]
filePathComponents.removeAtIndex(0)
let fileName = "-".join(filePathComponents)
3
  • Removed the tag. Thank you Commented Aug 19, 2015 at 18:45
  • And the result you desire would be? "questions-ask"? Commented Aug 19, 2015 at 20:45
  • Yes that would be it Commented Aug 19, 2015 at 21:29

4 Answers 4

1

I would recommend

filePathComponents.filter { return $0 != "/" }
Sign up to request clarification or add additional context in comments.

Comments

1

You don't have to use mutability if you use range indexing (with ..<).

And to make it convenient to use, let's put it in an extension as a computed property.

Example for String:

extension String {
    var pathNameWithoutPrefix: String {
        get {
            return "-".join(self.pathComponents[2 ..< self.pathComponents.count])
        }
    }
}

let filePath = "http://stackoverflow.com/questions/ask".pathNameWithoutPrefix
println(filePath)  // "questions-ask"

Example for NSURL:

extension NSURL {
    var pathURLWithoutPrefix: NSURL? {
        get {
            if let filePathComponents = self.pathComponents as? [String] {
                return NSURL(string: "-".join(filePathComponents[1 ..< filePathComponents.count]))
            }
            return nil
        }
    }
}

if let url = NSURL(string: "http://stackoverflow.com/questions/ask"),
    let fileURL = url.pathURLWithoutPrefix {
    println(fileURL)  // "questions-ask"
}

Here's the same extensions for Swift 2:

extension String {
    var pathNameWithoutPrefix: String {
        get {
            let str = self as NSString
            return "-".join(str.pathComponents[2 ..< str.pathComponents.count])
        }
    }
}

extension NSURL {
    var pathURLWithoutPrefix: NSURL? {
        get {
            if let filePathComponents = self.pathComponents {
                return NSURL(string: "-".join(filePathComponents[1 ..< filePathComponents.count]))
            }
            return nil
        }
    }
}

Comments

1
let linkURL = NSURL(string: "http://stackoverflow.com/questions/ask")!
if let comps = linkURL.pathComponents as? [String] {
    let fileName = "-".join(dropFirst(comps))   // "questions-ask"
}

Swift 2.0

let linkURL = NSURL(string: "http://stackoverflow.com/questions/ask")!
if let comps = linkURL.pathComponents?.dropFirst() {
    let fileName = "-".join(comps)   // "questions-ask"
}

1 Comment

Is not the first path component 'questions'? The stuff before that being <proto>://<host>:<port> I'd say?
0

Different approach using the host and path properties of NSURL

if let assetURL = NSURL(string: "http://stackoverflow.com/questions/ask") {
  let fileNameWithHost = assetURL.host!.stringByAppendingPathComponent(assetURL.path!) // "stackoverflow.com/questions/ask"
  let fileNameWithoutHost = assetURL.path!.substringFromIndex(assetURL.path!.startIndex.successor()) // "questions/ask"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.