Constants and variables

Now, we are ready to dive into the basics of the Objective-C language. Objective-C has constants and variables very similar to Swift but they are declared and worked with slightly differently. Let's take a look at declaring a variable in both Swift and Objective-C:

var number: Int
int number;

The first line should look familiar, as it is Swift. The Objective-C version doesn't actually look all that different. The important difference is that the type of the variable is declared before the name instead of after. It is also important to note that Objective-C has no concept of type inference. Every time a variable is declared, it must be given a specific type. You will also see that there is a semicolon after the name. This is because every line of code in Objective-C must end with a semicolon. Lastly, you should notice that we have not explicitly declared number as a variable. This is because all information is assumed to be variable in Objective-C unless specified otherwise. To define number as a constant, we will add the const keyword before its type:

let number = 10
const int number = 10;

Objective-C has value and reference types just like Swift. However, in Objective-C, the difference between them is more conceptual.

Value types

The number we declared above is a value type in both languages. They are copied if they are passed to another function and there cannot be more than one variable referencing the exact same instance.

It is actually easier to determine if a variable is a value type or a reference type in Objective-C because, as we will see, virtually all reference types are declared with an asterisk (*). If there is an asterisk, you can be safe to assume that it is a reference type.

Reference types

Objective-C actually allows you to make any type a reference type by adding an asterisk:

int *number;

This declares a reference to a number variable, more commonly referred to as a pointer. In a pointer declaration, the asterisk should always come after the type and before the name.

In Objective-C, reference types are actually loosely mixed with the concept of optional in Swift. All reference types are optional because a pointer can always point to nil:

int *number = nil;

A pointer can also always be tested for nil:

number == nil;

To access the referenced value, you must dereference it:

int actualNumber = *number;

You can dereference a pointer by adding an asterisk before it.

This is how pointers are similar to optionals in Swift. The difference is that there is no way to declare a non-optional reference type in Objective-C. Every reference type could technically be nil, even if you design it to never actually be nil. This can often add a lot of unnecessary nil checking and means every function you write that accepts a reference type should probably deal with the nil case.

Finally, the other difference between reference types in the two languages is that Objective-C is not very strict when it comes to what type the pointer is referencing. For example, Objective-C won't complain if we create a new double reference that points at the same thing as the int pointer:

double *another = (double *)number;

Now, we have two variables: number and another; they are pointing at the same value but assuming that they both are of different types. One of them is clearly going to be wrong, but Objective-C will happily try to use the same value as both a double and an int if you try. This is just one bug that Swift makes impossible by design.

So far, all of the Objective-C code we have looked at is actually strict C. We have not used any of the features that Objective-C added onto C. The main thing that Objective-C adds to C is its class system.

Lets take a look at our first actual Objective-C type called NSString compared to the Swift String type:

var myString = "Hello World!"
NSString *myString = @"Hello World!";

Just like in Swift, you can create a string instance using double quotes; however, in Objective-C you must put an @ sign before it.

One big thing to remember with the Objective-C class system is that it is not possible to create an instance of a class that is a value type. All instances must be referenced by a reference type. We cannot create a plain NSString. It must always be an NSString* pointer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset