Swifterswift: A bug about string's appendingPathComponent

Created on 16 Sep 2019  路  3Comments  路  Source: SwifterSwift/SwifterSwift

```swift
let string = "https://www.baidu.com"
let newString = string.appendingPathComponent("temp.mp3")
print(newString)
// result: "https:/www.baidu.com/temp.mp3" It is a bad url string

    // If I convert it to a url
    let url = URL(string: string)
    let newUrl = url?.appendingPathComponent("temp.mp3")
    print(newUrl)
    // result:  "https://www.baidu.com/temp.mp3" It is correct

````

documentation

Most helpful comment

Hi @fanglinwei ,

Thanks for raising this issue. Upon checking this, seems like this is the expected behavior when we call .appendingPathComponent(_:) on a string. As per NSString documentation,

Note that this method only works with file paths (not, for example, string representations of URLs).

https://developer.apple.com/documentation/foundation/nsstring/1417069-appendingpathcomponent

All 3 comments

Hi @fanglinwei ,

Thanks for raising this issue. Upon checking this, seems like this is the expected behavior when we call .appendingPathComponent(_:) on a string. As per NSString documentation,

Note that this method only works with file paths (not, for example, string representations of URLs).

https://developer.apple.com/documentation/foundation/nsstring/1417069-appendingpathcomponent

I've added the documentation notes here https://github.com/SwifterSwift/SwifterSwift/pull/709

@cHaLkdusT ,
Thank you for your answer so that we can clearly understand the correct use of this function.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omaralbeik picture omaralbeik  路  3Comments

omaralbeik picture omaralbeik  路  3Comments

bjimenezned picture bjimenezned  路  3Comments

SD10 picture SD10  路  3Comments

g001613001 picture g001613001  路  5Comments