Objective-C Variables, Data-types, Constants and Preprocessors

Ashish P
iOSMastery.com
Published in
6 min readNov 5, 2017

--

In this Article you are going to learn the basic building blocks of objective-c. If you are a beginner in programming and new to objective-c, this session is for you.

In any language we give names to objects or places in the world. These names represent the information for us. This is how we remember things. We humans have names for everything like persons, places, emotions. All that we could think of is named. Likewise in Programming language we store the information in the variables, Constants, objects, properties, enums, structures,etc. Each one has a unique purpose.Lets look at them one by one.

Variables

As the name suggests a variable is a chunk of data that can store information which can be changed. For example, if ‘name’ is a variable then,

NSString *name = @”apple”;

//or

NSString *name = @”google”;

both are valid since name is a variable. That means name can store any type of information provided it is text. The NSString represents a text type of variables. These types are also called data-types. There are different data-types available for different types of data. Objective-c is superset of C, so it has all the data types of C and adds few of its own.

There are integers, floats, doubles, NSString, NSDate, NSArray, NSMutableArray, NSDictionary. Thats Looks like a lot but don’t be bothered, if you follow our tutorials, you will find it easy and fun to learn. For now just understand that variables can store the data that can be changed provided it is of any specific data-type.

Data Type:-

We all use different types of data to represent information every day. Numbers Contain digits, Words contain alphabets. Similarly in a programming language, there are different data-types like integers, floats, strings, etc all having their own characteristics like floats can represent decimal numbers, strings contain series of characters that can be altered and concatenated. Lets take a look at data types provided by apple for iOS and Mac-OS.

A Data-Type is Predefined set of characteristics that are applied to data belonging to that type.

Data-types inherited from C

The C Data-types

Preprocessing macros:-

We learned about the variables. Variables enable us to store and manipulate the information. But what if we want to keep the value fixed. Well theres a data-type for that in every programming language, it is called constant.

In objective-c we can define a constant value with two methods:-

  1. Defining a value with preprocessing micro
  2. Defining a constant

Pre-Processing Macro

A pre-processing micro is defined as:-

#define my_money 1000000000

What we are doing here is defining a name ‘my_money’ for a value followed by it after a space. Now we can use this name for the value wherever we want it. And if we want to change the value we don’t have to scramble through our code to find and change it. Just change it in the definition above and all your code will reflect the change.

Behind the scene:-

What happens when our code runs is, my_money is replaced with 1000000000 and this is done before the compiler compiles the code. Thats why the name Pre-processing Macro.

We can also use it for representing string value like so:-

#define my_name @“Ashish”

You must have noticed that we don’t provide type information while defining a macro. This is because macro is just replaces our code before compile time. It just represents our data unlike constant which allocate a physical memory for our data.

Sure macro is convenient to use but we compiler can’t do the appropriate checks during compile time. Consider the following example:-

#define aValue 2

#define TOTAL aValue + 3

So our Total represents ‘2 + 3’ instead of 5.

So if we do something like:-

int result = TOTAL * 2;

NSLog(@”result %d”,result);

We would want the result to be 10. Right?

it becomes , int result = 2 + 3 * 2;

Here’s the output:-

2015–05–23 17:11:15.939 DefineVsConstant[1086:65791] result 8

So you can see result turns out to be 8 instead of 10. Hence we should use const instead of macros in Object Oriented Programming.

Constants

Constants are the variables whose values remain fixed at all times and that cannot be altered during execution of the program. It is defined as follows:-

const int aConstant = 11;

Behind the scene

Here a memory is allocated to aConstant and it is assigned the value 11. This value cannot be changed during execution of program. The compiler will throw an error if we try to do so.

Static

We can modify the scope of the constants using static and extern. If we don’t want the constant to be available outside your class define it as static.

we can use static as below:-

static const int aConstant = 11;

This ensures that aConstant is available in only the file where it is defined.

Extern

But if you want it to be global then you can use extern like so:-

In .h :-

extern const NSString *baseURLString;

In .m :-

Write this after @implementation line:-

NSString *baseURLString = @“www.onestopios.com”;

You can’t use an extern defined in a method. You have to define it at the right place.

Create a separate Constants.h and Constants.m file to declare and define your constants. This will make your code easier to maintain and debug.

Memory Size Changes in data-types due to 64-bit runtime environment

Ever Wondered how the same piece of code manages to work on different platforms having different hardware and software-architectures ? The answer is, they follow standard conventions like size and format of data-types. They also follow conventions as to how the code is called or linked together. These conventions are called application binary interface. iOS uses low level application binary interface.

“Usually these topics are not included in the tutorial learning sites, I intend to provide it because i want to provide in depth knowledge on every individual topic. Though it may seem very technical and not fun at start but it will help you in every step of your future endeavours like in an interview, in a debate with your senior or colleague as to what is the best way to do a thing and why? This can make a difference between a mediocre and an expert programmer. So read on…”

With iOS 7 apple introduced 64 bit architecture on its latest iOS devices. But this doesn’t mean that they support only 64 bit architecture but both 32 bit and 64 bit architectures. This can cause problems hence we have to follow certain rules:-

a) 64 bit runtime environment requires more memory for some data-types as pointed by the table below. So we need to pay more attention on memory optimisation.

b) If your application is shipped for both 64-bit and 32-bit runtime environment, its possible that both use same pool of data. For example, exchanging data with the cloud or between devices through your server. In these cases we have to make sure that our variables or objects uses data types whose memory sizes and offsets doesn’t change.

c) Calculations for the numbers that require range higher than the limits of data types might be affected. e.g. an integer with value higher than the capacity of a 32-bit integer might produce different results in 64-bit runtime environment.

d) Values could get truncated if assigned from a higher precision data type to a lower precision data type like from an NSInteger to int.

In 64 bit runtime environment the size of NSInteger and int is different as stated in the table below:-

So whats changed:

The size of pointers,size,long integer,CFIndex and NSInteger is increased from 4 bytes to 8 bytes.

You can find my other posts about Objective-C onestopios.com

Further Reading:-

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-ID55

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID330

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID120

--

--