Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user desktop path in Swift 4.0 and Xcode 9.0

Tags:

swift4

xcode9

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.

like image 798
Kader Mokhefi Avatar asked Oct 02 '17 19:10

Kader Mokhefi


People also ask

What version of Swift is available in Xcode?

*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.

What is the Windows version of Swift toolchain?

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.

Where can I install Swift on Linux?

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.

How do I deploy the Windows SDK to Swift?

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.


2 Answers

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:

Capabilities

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.

like image 137
Code Different Avatar answered Oct 13 '22 23:10

Code Different


Try this for Swift 4.0

var desktopPath = (NSSearchPathForDirectoriesInDomains(.desktopDirectory, .userDomainMask, true) as [String]).first
like image 41
Ronald Hofmann Avatar answered Oct 14 '22 00:10

Ronald Hofmann