I am trying to get the path to the desktop of the current user, but I get a path to another location in the library (see code).
The same code in Playgrounds gives the right answer. I used the same code in an earlier created project version of Xcode and that gives also the good results.
The tests are done under MacOS High Sierra, Xcode Version 9.0 (9A235) and Swift 4.0.
Here is the code used:
public func userDesktop() -> String {
let paths = NSSearchPathForDirectoriesInDomains(.desktopDirectory, .userDomainMask, true)
let userDesktopDirectory = paths[0]
return userDesktopDirectory
}
let myPath:String = userDesktop() + "/"
print(myPath)
let userDesktopDirectory:String = NSHomeDirectory() + "/" + "Desktop/"
print(userDesktopDirectory)
/* results in Playground */
/Users/kader/Desktop/
/Users/kader/Desktop/
/* Results from a project in Xcode 9.0 */
/Users/kader/Library/Containers/com.kader.testuserpath/Data/Desktop/
/Users/kader/Library/Containers/com.kader.testuserpath/Data/Desktop/
What is wrong about this code or is it a bug?
I am curious whether more people have experienced the same problem.
*Swift 4.0 is available as part of Xcode 9.0. *Swift 3.1.1 is available as part of Xcode 8.3.2. *Swift 3.1 is available as part of Xcode 8.3. *Swift 3.0.2 is available as part of Xcode 8.2.
1 Swift Windows 10 toolchain is provided by Saleem Abdulrasool. Saleem is the platform champion for the Windows port of Swift and this is an official build from the Swift project. Swift 5.6 Snapshots are prebuilt binaries that are automatically created from release/5.6 branch.
Packages for Linux are tar archives including a copy of the Swift compiler, lldb, and related tools. You can install them anywhere as long as the extracted tools are in your PATH. Note that nothing prevents Swift from being ported to other Linux distributions beyond the ones mentioned below.
In order to make the Windows SDK accessible to Swift, it is necessary to deploy a few files into the Windows SDK. The following will modify your Visual Studio Installation, and as such will require to be run from an (elevated) “Administrator” x86 Native Tools for VS2019 Command Prompt.
I noticed that the Cocoa Application template in Xcode 9 turns on App Sandbox by default. With this mode on, the app is restricted to access files within its own container, just like iOS apps. You can turn it off in the Capabilities tab in your project settings. A screenshot from Apple's documentation:
What your app can do inside a sandbox. If you are doing this for fun, have no intention of distributing it through the Mac App Store, or want a "computer application" in the traditional sense of the words, turn off sanboxing altogether. It has long been the pain of the Mac App Store and the reason that many developers are leaving the store.
Try this for Swift 4.0
var desktopPath = (NSSearchPathForDirectoriesInDomains(.desktopDirectory, .userDomainMask, true) as [String]).first
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With