Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift: Bundle.main.path returns nil

Tags:

xcode

swift

i created a folder in my assets called "json" into there i dragged&dropped a file called code.json from the finder. Now i want to open the file but

Bundle.main.path(forResource: "code", ofType: "json", inDirectory: "json")

just returns nil.

I also tried to put it in the assets without the subfolder and using following way

Bundle.main.path(forResource: "code", ofType: "json")

with the same result.

Can anyone help me? What am i doing wrong?

like image 426
Ginso Avatar asked Oct 24 '25 18:10

Ginso


2 Answers

You need to make the folder reference when you drag it to xcode , it's color is blue not yellow

enter image description here

//

enter image description here

like image 56
Sh_Khan Avatar answered Oct 26 '25 08:10

Sh_Khan


It is clear from your question that you are trying to add the code.json file in Assets.xcassets and trying to access it's path with Bundle.main.path(forResource: ), which will always returns nil, because Assets.xcassets is present in Bundle and then your file exists in it.

Try adding the files directly to the project, as suggested in previous answer, approach is right, Bundle.main.path(forResource: "code", ofType: "json")

like image 45
Bhavin Kansagara Avatar answered Oct 26 '25 07:10

Bhavin Kansagara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!