Solutions for constants defined in OBJECTIVE-C

Source: Internet
Author: User

Today I used some of the usual strings in my project, so I created a constant.h file that was defined by # define.

Later on the internet to see the big gods are recommended to use string constants to define, so I have all the macro definitions are replaced with nsstring * const, such as nsstring * const ltnnextstep = @ " next ";

The. h file is used primarily in two classes, but a link problem was found after #import the file:

LD:one for1 (use-v to see invocation)

It turns out that the variable defined in standard C is an outer join, that is, if a compilation unit defines a global const constant, it is visible in other compilation units, and a duplicate definition error occurs if the other compilation unit also defines a const constant with the same name. This is different from C + +, where const-defined variables in C + + are internal, that is, the global const constant defined by each compilation unit is unique to itself.

Objective-c is another extension of standard C, then my mistakes are obvious-there is a duplicate definition error when multiple compilation units refer to that constant.h file.

But will

  NSString * const, such as nsstring * const ltnnextstep = @ " next ";

Revision changed to

Static nsstring * const, such as nsstring * const ltnnextstep = @ " next ";

It was successfully compiled and passed. The static here is used to mark the defined const constants as invisible to the outside.

At present, my basic knowledge is not solid, so I can not say why. The study will be studied more carefully in the future.

Using a common method, create a. h and a. m file directly in the. h file:

@interface Constant:nsobject extern Const Ljnextstep; @end

Here the extern c keyword is used to indicate that the variable has been declared, just a reference. The Const keyword indicates that the variable is constant and cannot be modified.

In the. m file:

@implementationconst@ " next "; @end

Solutions for constants defined in OBJECTIVE-C

Related Article

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.