开发者

Objective-C: Best way to store error messages

开发者 https://www.devze.com 2022-12-18 15:19 出处:网络
What\'s the best way to store error messages (specially while developing with Xcode and Objective-C for Mac and iPhone):

What's the best way to store error messages (specially while developing with Xcode and Objective-C for Mac and iPhone):

  1. Creating a separate class that contains list of error code and corresponding messages
  2. .plist of error code and corresponding messages
  3. Localizable.string 开发者_StackOverflowfile with error code and corresponding messages (the application may or may not support localization)
  4. Other(s)

I'm sure i don't have to give a reason why anyone would want to keep all the error messages in one location. Thanks.


Your file doesn't have to be called Localizable.strings. You can have a file called Errors.strings. When you want to get the error description, you can use:

NSString *errCode = @"err1";
NSString *errDesc = [[NSBundle mainBundle] localizedStringForKey:errCode
                                                           value:nil
                                                           table:@"Errors"];

You could use a plist file, however all the work is done for you with a strings file, you don't have to write any code to parse any file or initialise some dictionary or array (no matter how easy that is anyway).


I would go for option 3. If you want to support localizations later on, you will need the .strings files anyway.


I've done something similar to this in the past, and I used something like option #2.

A file (.plist or otherwise) where an entry consists of "ErrCode Description" makes it easy to parse/search for certain errors.

0

精彩评论

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

关注公众号