Basic starting configuration

I'm trying to get an hold on swift. So I decided to use it to perform some treatments on a file.plist i'm interested in.


I downloaded xcode, had some tutorial on Apple ios apps, thought it was a good idea to have them stored on an iCloud directory :

iCloud Drive/MyApps

Everything went fine.


But since I just wanted to experiment with swift, I decided to create a MacOs/Commandlinetool package and play with it.

So I have my PlistReader folder created in my MyApp Folder and I am starting to work with my file.plist with it


I can't figure how on earth I can get access to this file

if I try to debug my swift code


let stringPath = Bundle.main.path(forResource: "file", ofType: "plist")
print(stringPath)

returns :

"/Users/MyName/Library/Developer/Xcode/DerivedData/PlistReader-caqhxzhblczwvlccnkygmsfmbdae/Build/Products/Debug/fileplist"


So it seems Xcode replicates my files in a arbitrary folder, vaguely related to what is defined in its preference file.

But not all is there since other test files in subdirectories were not replicated.


I'm clearly doing something wrong here but don"t know what.

Either in the Build Phases / Copy Files parameters

or

Preferences /locations ?


On top of that all tutorials I found seem broken with swift 5.1


Someone could help me about how to configure xcode to play with swift and access a simple text file ?

Answered by HugoParis in 405113022

What I now understand is that Bundle is refering to a Directory where the command line file is copied, which is the path I referred to in the initial post. Not the one where the source file is created. I am not sure this new place is supposed to be used by the developer

And that, for strange reason, the data file (file.plist) is not copied along the command line file, even though is is checked to be in "Build phase" tab.

A I said in my last post, there is a worlkaround, even if not very elegant.

Thank you for your input

Is your app sandboxed ?

No Sandboxing I am aware of, though maybe the problem is here.


Basically, I have a conceptual misunderstanding of how to start.

I have built my little program :

1) as a Mac app, and it works, getting the file from inside the package

2) as a Playground and it works, getting the file from Documents/Shared Playground Data through the playgroundSharedDataDirectory variable

3) as a cmd line, with same code as 1) and it doesn't work


My guess is that

1) either I don't have the good command, "Bundle.main.path(forResource: "file", ofType: "plist") " is not available in command line mode

2) either the command is good but I missed a parameter somewhere, and the program doesn't know the file is in its folder along main.swift

3) either there is a bug and this is due to the fact that i'm working on icloud drive

4) either I have missed something on how people are supposed to get familiar with Swift & Xcode and I should not try to work in command line mode. Though I find it more practical than using a Mac app, since I don't want to handle window dialogs for the time being. And I prefer to avoid the playground to have source control

Have a look here:


https://stackoverflow.com/questions/38422467/getting-path-for-resource-in-command-line-tool


It explains that file must be in the same directory as the command line.

Unless (but did not test) you make a cd before ?

At least I have found a workaround with


let path = "/Users/myName/Library/Mobile Documents/com~apple~CloudDocs/myDirectory/file.plist"

Accepted Answer

What I now understand is that Bundle is refering to a Directory where the command line file is copied, which is the path I referred to in the initial post. Not the one where the source file is created. I am not sure this new place is supposed to be used by the developer

And that, for strange reason, the data file (file.plist) is not copied along the command line file, even though is is checked to be in "Build phase" tab.

A I said in my last post, there is a worlkaround, even if not very elegant.

Thank you for your input

Thanks for the feedback.


Don't forget to close the thread.


may be you could file a request for improvement.

Basic starting configuration
 
 
Q