Stringbyappendingpathcomponent Is Unavailable

stringByAppendingPathComponent is unavailable

It looks like the method stringByAppendingPathComponent is removed in Swift 2.0, so what the error message suggests is to use:

let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("instagram.igo")

Update:

URLByAppendingPathComponent() has been replaced by appendingPathComponent() so instead do:

let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("instagram.igo")

stringByAppendingPathComponent' is unavailable

You can create a URL rather rather than a String path.

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let fileURL = documentsURL?.appendingPathComponent("test.sqlite")

If you absolutely need the path, then you can get it like this:

guard let path = fileURL?.path else {
return
}

print(path) // path will exist at this point

There was some discussion in the thread you mentioned that Apple may be purposely guiding people toward using URLs rather than String paths.

See also:

  • What's the difference between path and URL in iOS?
  • NSFileManager URL vs Path
  • Swift 2.0: Why Guard is Better than If

stringByAppendingPathComponent Unavailable

let nsPath = Path as! NSString

now NSPath has all the functionality you want - in swift2.0 they kinda disabled implicit casting even more

Swift 2.0 - stringByAppendingPathComponent error conversion

Apple wants you to use URLs instead of paths so they have made it unavailable in Swift 2. If you don't want to use NSURL, you can temporarily cast it to NSString instead:

let path = (DocumentsDirectory as NSString).stringByAppendingPathComponent("notes.plist")

stringByAppendingPathCompnent is unavailable

Instead of that you can try this:

return try!  NSFileManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true).URLByAppendingPathComponent(fileName).path!

stringByExpandingTildeinPath is unavailable

Why did you ask for it to not expand the ~ then expend it by yourself? Much simpler to let the API handle it by itself:

let filename = "myimage.wai"
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let destinationPath = documentsPath + "/" + filename

What to use in place of stringByAppendingPathComponent in Swift 2

All you have to do is cast to NSString to recover stringByAppendingPathComponent, like this:

    let dropTexture = SKTexture(image: UIImage(
contentsOfFile:(NSBundle.mainBundle().resourcePath! as NSString).stringByAppendingPathComponent(
"P04_rainDrop1.png"))!)

As Leo Dabus rightly says, you can save yourself from all that casting by adding an extension to String. However, you should not, as he suggests, call NSString(string:), which generates an extra string. Just cast:

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

StringByAppendingPathComponent() and WriteToFile()

Regarding the first part of your question, as dan stated in the comments you should be using fullPath.appendingPathComponent(name) instead.

Regarding your second question:

The main difference between writeToFile and write(to: is the fact that the first is for Strings and the seconds is for NSData.

Somewhat related:

According to the NSData Class Reference

In iOS 2.0+ you have:

write(to:atomically:)

and

write(toFile:atomically:)

Given:

Since at present only file:// URLs are supported, there is no
difference between this method and writeToFile:atomically:, except for
the type of the first argument.

None of this has changed in Swift 3 according to the Swift Changelog.



Related Topics



Leave a reply



Submit