swift - Use Local JSON file in Unit Test -


i have app called "ffquestions". use local json file called qa.json.

is there way can use qa.json in "ffquestionstests" folder files without recreating file , dropping in folder? need change code it? or simple adding target membership "ffquestionstests"?

this method loading qa.json...

func loadjsonfromfile() -> [string: any]? {     guard let url = bundle.main.url(forresource: "qa", withextension: "json") else {return nil}     guard let data: data = nsdata(contentsof: url) data? else {return nil}      {         let object = try jsonserialization.jsonobject(with: data, options: .allowfragments)         guard let dictionary = object as? [string: any] else {return nil}         return dictionary     } catch  {         print("not parsed")     }      return nil } 

even thought might need done differently in unit test tried out...

func loadjsonfromtestsfile() -> [string: any]? {     let testbundle = bundle(for: type(of: self))     let url = testbundle.url(forresource: "qa", withextension: "json")      guard let data: data = nsdata(contentsof: url!) data? else {return nil}      {         let object = try jsonserialization.jsonobject(with: data, options: .allowfragments)         guard let dictionary = object as? [string: any] else {return nil}         print(dictionary)         return dictionary     } catch  {         print("not parsed")     }      return nil } 

so i've tried, i'm still getting nil crash.

it simple adding target membership "ffquestionstests"


Comments