IOS enum C method DEBUG, release of a hidden pit

Source: Internet
Author: User

Developed an app that doesn't have any problems in debug mode and crashes directly in release mode.

After a period of positioning finally positioned to the following section of the code:

    
E_bz_testtype type = [dic[@ "type"] integervalue];
Calling code
= [NSString stringWithFormat:@ "%@", Nsstringfrome_bz_testtype (type)];

Where enumeration definitions and enumeration translations are defined in Chinese as follows:

//enumeration in a XXDefine.htypedef ns_enum (Nsinteger, E_bz_testtype) {Ke_bz_testtype_none=0, Ke_bz_testtype_one=1,};//translation function DefinitionNSString *nsstringfrome_bz_testtype (e_bz_testtype type);//the translation function is implemented in a XXDEFINE.MNSString *nsstringfrome_bz_testtype (e_bz_testtype type) {Switch(type) { CaseKe_bz_testtype_none:return @"No";  CaseKe_bz_testtype_one:return @"a"; default:             Break; }}

Note The above code:

1. Code is not inside any class

2. The default of the translation function is break, and no value is returned.

3. In debug/release mode, the translation function does not produce any errors or any warnings

For note 3rd, if you change the translation function implementation to the following, an error will occur

NSString *nsstringfrome_bz_testtype (e_bz_testtype type) {    switch  (type) {          Case  ke_bz_testtype_none:            return@ " no ";         default :              Break ;    }}

So here's the problem:

When the type in the calling code does not belong to any one of the enumerations, for example, type = 100

When using a translation function:

The DEBUG mode is: 100 strings returned

And in release mode it's a direct crash.

Therefore, the translation function can only be modified as follows:

NSString *nsstringfrome_bz_testtype (e_bz_testtype type) {Switch(type) { CaseKe_bz_testtype_none:return @"No";  CaseKe_bz_testtype_one:return @"a"; default:            return @"Unknown"; }}

So there's no problem.

I think this is the result of the Release mode code optimization.

IOS enum C method DEBUG, release of a hidden pit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.