在Swift 2中使用什么代替stringByAppendingPathComponent

在Swift 2中使用什么代替stringByAppendingPathComponent,swift,swift2,Swift,Swift2,我刚刚用新的Swift 2安装了Xcode 7,现在有50多个错误,说“stringByAppendingPathComponent”不可用,我应该使用“URLByAppendingPathComponent”。我已将所有纹理属性设置为: let dropTexture = SKTexture(image: UIImage( contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent(

我刚刚用新的Swift 2安装了Xcode 7,现在有50多个错误,说“stringByAppendingPathComponent”不可用,我应该使用“URLByAppendingPathComponent”。我已将所有纹理属性设置为:

let dropTexture = SKTexture(image: UIImage(
    contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent(
        "P04_rainDrop1.png"))!)
我一直在这样做,这样当场景发生变化时,它们就不会留在记忆中,而且它一直在完美地工作。但是,直接替换“URLByAppendingPathComponent”并不能修复错误


如何更改此选项以修复错误并获得相同的SKTexture?

您所要做的就是通过附加PathComponent恢复
Strings,如下所示:

    let dropTexture = SKTexture(image: UIImage(
        contentsOfFile:(NSBundle.mainBundle().resourcePath! as NSString).stringByAppendingPathComponent(
            "P04_rainDrop1.png"))!)
正如利奥·达布斯(Leo Dabus)正确地说的,通过在字符串中添加一个扩展名,您可以避免所有的铸造。然而,正如他所建议的,您不应该调用
NSString(string:)
,这会生成一个额外的字符串。刚出演:

extension String {
    func stringByAppendingPathComponent(pathComponent: String) -> String {
        return (self as NSString).stringByAppendingPathComponent(pathComponent)
    }
}

这仍然是实现这一目标的体面方式吗?我找不到苹果在Swift nativly yetNever上将字符串转换为NSString上实现了这一点。看看苹果的处方@布伦南很公平。我只是不想回答“不要那样做”,如果不是必要的话。但我确实尽可能使用URL而不是字符串文件路径(现在几乎无处不在)。