Minimi Minimi - 1 year ago 42
iOS Question

Xcode loads same image for two different strings

I have two images in the assets folder named like so:

backgroundImage = @"background.wood";
backgroundImageLight = @"";

The names in the assets folder are the same as these strings. When I try to load these images, no matter what backgroundLight always loads the background image. I had to change the name in the assets folder and in the string from "" to "somedifferentstring" for it to actually load the correct image. Is this a bug with Xcode? Why does it load the same image for these different strings? Does it have to do with the beginnings being similar? I tried emptying the cache manually to make sure that the image wasn't pre loaded but that didn't help either.


The code to display/load the images:

SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:backgroundImage];
background.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
background.size = self.frame.size;
background.zPosition = background_z_order;

SKSpriteNode *backgroundLight = [SKSpriteNode spriteNodeWithImageNamed:backgroundImageLight];
NSLog(@"%@, %@", backgroundImage, backgroundImageLight);
backgroundLight.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
backgroundLight.size = self.frame.size;
backgroundLight.zPosition = background_z_order;

[mainView addChild:background];
[mainView addChild:backgroundLight];


All of these load different images, so maybe it's the second "." that throws it off idk:

Answer Source

The last "." is taken to be the start of a file extension. For compatibility with older projects where an asset was moved from the app bundle to the asset catalog when asset catalogs were invented, it is legal to say @"myImage.jpg" and this will still be found in the asset catalog if the image set is called myImage. The file extension is stripped off and the image set is still found. Similarly, your dot-suffixed .lightning is being stripped off and the resulting background.wood is found — and that is the end of the search.

So the moral is, Don't Do That. Use an underscore instead of a dot.