'stringByAppendingPathComponent' is not available

I get an error

'stringByAppendingPathComponent' is unavailable: Use 'stringByAppendingPathComponent' on NSString instead. 

when i try to do

 let documentsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String let databasePath = documentsFolder.stringByAppendingPathComponent("test.sqlite") 

This apparently worked for people before, but now it does not work for me in Xcode 7 beta 5.

In this thread on the Apple developer forums, it was suggested to use the extension or make a direct tide of NSString . But if I convert it to NSString

 let databasePath = documentsFolder.stringByAppendingPathComponent("test.sqlite" as NSString) 

then i get an error

 'NSString' is not implicitly convertible to 'String'... 

and it gives me the opportunity to "fix" by inserting as String , which returns us to the original error.

This also happens for stringByAppendingPathExtension .

What should I do?

+14
ios path swift
Aug 20 '15 at 14:10
source share
2 answers

You can create a URL 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 a path, you can get it like this:

 guard let path = fileURL?.path else { return } print(path) // path will exist at this point 

There was some opinion in thread that Apple could intentionally direct people to use URLs rather than string paths.

See also:

+26
Aug 20 '15 at 14:10
source share

You can use:

 let dbPath = (documentsFolder as NSString).stringByAppendingPathComponent("db.sqlite") 
+11
Sep 17 '15 at 13:47
source share



All Articles