开发者

Proper way to store resources filenames in Objective C

开发者 https://www.devze.com 2023-02-21 11:49 出处:网络
what way would you recommend to store filenames of resources (e.g. images) I use #define structures to store them as constants but I think it is quite ugly as those #define became visible outside clas

what way would you recommend to store filenames of resources (e.g. images) I use #define structures to store them as constants but I think it is quite ugly as those #define became visible outside class if 开发者_JAVA百科you import its header.

As I understand there are few ways to store filenames:

  • #define - I described cons above
  • hardcoded filename in the code (like [UIImage imageNamed:@"test.png"] I think this way make difficult to maintain nice code
  • property file with dictionary (again, we need to store keys somewhere in the code) ....

I'm sure there is a nicer way to do that. Can you share what way do you use?

Actually I have same question for numeric private constants. e.g. button origin for custom view etc.. which shouldn't be visible outside class.


I think it isn't always unacceptable to hard code the filename, if there's one single reference to the file, and the context of the reference (or attached comment) makes it obvious what the role of the filename is.

I would rarely recommend #define for a constant, because of type-checking issues. In Objective C a constant can be defined with

NSString * const kSomeImageFilename = @"myImage.png";


In projects with large number of resources my team practices placing the resources in a plist. Modifications of resource file names in this case only require an appropriate change within the plist. The resource names are then retrieved by key from the plist.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号