"plist" does not load from iPhone test class

I am trying to execute a single code to load and request data from a plist file. I am loading data with code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"availableshops" ofType:@"plist"];
NSArray *arrayOfShops = [[NSArray alloc] initWithContentsOfFile:path];

When testing the application in the simulator and on the device, everything works. BUT, when I run unit test, the code " [arrayOfShops count]" always returns " 0".

I have the same files in "Copy Bundle Resources", "Compile Sources" and "Link Binary With Libraries" for the main target and target. I also tried with and without a primary goal as “Direct Breaks” for the purpose of unit testing.

Does anyone know what the problem is?

+3
source share
3

[NSBundle mainBundle] Xcode Unit Test, , :

NSBundle *bundle = [NSBundle bundleForClass:[self class]];

NSString *path = [bundle pathForResource:@"availableshops" ofType:@"plist"];
NSArray *arrayOfShops = [[NSArray alloc] initWithContentsOfFile:path];
+2

. . -, mainBundle .

+1

Are you sure the plist root element is an array? Wiring plist may help.

You can also try the following:

NSArray* shopsArray = [NSArray arrayWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"availableshops.plist"];
0
source

Source: https://habr.com/ru/post/1722990/


All Articles