Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer constants

If I declare a string constant like so:

You should create a header file like

// Constants.h
extern NSString * const MyFirstConstant;
extern NSString * const MySecondConstant;
//etc.

You can include this file in each file that uses the constants or in the pre-compiled header for the project.

You define these constants in a .m file like

// Constants.m

NSString * const MyFirstConstant = @"FirstConstant";
NSString * const MySecondConstant = @"SecondConstant";

What do I do to define integer constants?

like image 477
Dan Morgan Avatar asked Feb 16 '09 19:02

Dan Morgan


People also ask

How do you write integer constants?

Integer constants are interpreted as decimal values (base 10) by default. To specify a constant that is not in base 10, use the following extension syntax: [s] [[base] #] nnn... Is an optional plus (+) or minus (-) sign.

What are the 3 types of constants?

Character constants, real constants, and integer constants, etc., are types of primary constants.

What is integer constant rule?

1) An integer constant must have at least one digit. 2) It must not have a decimal point. 3) It can either be positive or negative. 4) No commas or blanks are allowed within an integer constant. 5) If no sign precedes an integer constant, it is assumed to be positive.

Are all integers constants?

All integers are constants, but not all constants are integers. All integers are constants, but not all constants are integers.


1 Answers

Replace NSString* with NSInteger.

This is true of any constant type, be it a primitive such as int/float, or a class such as NSString or NSInteger.

The only thing to be aware of is whether you desire a constant or a pointer to a constant (such as withNSString), and how it's initialized in the .m file

Integer example:

// constants.h
extern NSInteger const MyIntegerConstant;

// constants.m
NSInteger const MyIntegerConstant = 666;

(Note: for the reason why NSInteger instead of just regular "int", see this post)

Class example:

// constants.h
extern MyClass* const MyClassConstant;

// constants.m
MyClass* const MyClassConstant= [[MyClass alloc] initWith: paramOne and:paramTwo];
like image 88
Andrew Grant Avatar answered Oct 07 '22 19:10

Andrew Grant