When it comes to developing software for iOS and Mac OS X, Objective-C is the primary programming language to use - Magora app developers know this for sure. Of course, you can use other languages and technologies like C++, PyObjC, pure C, MacRuby, MonoTouch or others to build iOS apps, but these are alternative variants that are mostly utilised for additional functionality or other secondary purposes, but we are going to tell about the language number one that you cannot do without when developing for iOS.
Frameworks are probably the most crucial instruments that allow you to build and manage iOS apps. WIthout frameworks, it would probably take years for the developers to design simple programs that run on Apple devices. Apple offers dozens of ready solutions that allow developers to easily solve such time-consuming tasks as code user interfaces, encrypt confidential data, write networking code, incorporate media players, draw graphical elements, take pics, view websites, save passwords and other information and so on.
A Plant class will most likely identify certain variables, for instance, the colour, the height and the species, as well as behaviour such as blooming, reproducing or photosynthesising. An object that belongs to a certain class is an instance of the class. Each instance has its own variables. For example, you can create 200 instances within the the Plant class and define different variables to each of them. Methods are behaviours of a class or of an instance within a class. They are accordingly called class methods and instance methods.
To create a new instance of a class you need to allocate a block of memory for it on the help. The
heap is the memory, that stores all the Objective-C objects and serves the purpose of dynamic
allocation. All Objects in Objective-C are pointers to the heap, which is denoted by an asterisk.
Let us show you an example of how to create an instance for the Plant class:
Plant *ourPlant = [[Plant alloc] init]
Now, let us make sure that you understand what it means. Do you know what the square brackets stand for? Be prepared to use them a lot in Objective-C because they enclose method calls. To the left you can see that we have created a new variable for the Plant* named ourPlant, then, to the right, we have done two method calls one within the other and defined what they return to ourPlant.
The stack provides storage for local data, which can be accessed without pointers.
If you work with variables that have a certain size at the stage of compilation, they are stored on the stack. For example, the stack stores primitive data types such as (boolean, long, byte, char, short, int, etc.) because they all have pre-defined maximum size.
The size of the objects in Objective-C is not restricted and can grow as much as you want, that is why they should be stored on the heap that enables to dynamically allocate their memory and use pointers. Pointers are memory addresses, and if you need to get an access to anything that the memory contains, you should dereference the pointer.
If you still do not clearly understand what is going on, let us take a closer look at the method calls. [Plant alloc] means that we are calling the class method +alloc on the Plant class. There is always a plus sign before a class method. It is used to return a generic object type of id. The usage of -init means that we are calling the instance method to initialise the memory in order to get access to use the new object. The indicator of instance methods is a minus sign that precedes it. You cannot use Objective-C objects without performing these two actions.
Working with object, you should always keep in mind that if you allocate a certain amount of memory, you should not forget to inform the runtime when the object is done being used and can be released. Objective-C uses a reference-counting system to track where each object is used at the moment, because one object can be used by different elements of the iOS app at once. When you create an object, it has only one reference. When a certain part of the iOS app is done using the object, it sends a -release message to the object, which results in the number of its references being decreased by one. The memory is freed when there are no references left to an object.
If you forget to release the memory of the object when they are no longer used, it may result in iOS app crashes and bad performance because the program will be using too much resources. In the case of Mac apps it will not only result in crashes but will also slow down the entire system, taking more and more memory. Without a doubt, you have experienced such problems, having left a Safari browser for a couple of days or a game running in the background. It happens because the iOS app uses exponentially more memory and leaves almost no resources to run other programs fast. Memory is one of the most important resources for iOS app developers, so use it wisely. Apple's Memory Management Guide is a great material that will help you get a deeper understanding of memory management in Cocoa.
Despite having behaviours, objects also can store data. For instance, we have a Smartphone object - what types of data attributes can it possess?
Now, we need to look into these attributes. The first one is the model name. It could be presented by a combination of numbers, for example, 1900738. The data type of this attribute is a number. There are different ways to represent a number in objective-C:
As you see, you have at least three choices. Now we will explain what they mean. The first options is the NSNumber class that is defined in the Foundation framework. The int variable type is a primitive data type from C that contains a number with a maximum value. And the third way is a primitive data type NSInteger that also comes from the Foundation framework and its size adjusts to the needs of the iOS program at the given moment. Foundation is one of the Apples frameworks for developing iOS apps.
You do not need to allocate memory for NSInteger or int because they are not objects, which means that their size is already defined. But you still need a pointer to get access to their content because they are created on the stack instead of the heap. Such elements as NSInteger, CGRect, CGSize, CGPoint and man other things stated in the Foundation Data Types Reference are not object as well, so they do not need a pointer in the shape of the asteriks too.
Let us get back to the first case of storing the model number 1900738 as an NSNumber object. When we
create it, it will look like this:
NSNumber *model = [NSNumber numberWithInt:1900738 ]
But if it is an instance of the NSNumber class, then why there is no -int? Whi there is no +alloc? You do not need to set the memory and returned it with this objects because they have convenient class methods that do it automatically.
We create high-quality iOS apps that stand out from the crowd thanks to their amazing designs, feature-rich functionality, and outstanding user experience. Magora developers are dedicated to our craft and over the years have delivered hundreds of successful B2C iOS programs, bespoke enterprise systems, MVPs for startups and beautiful websites across various industries.
At Magora, the cornerstones of our culture are seamless communication with the clients, full transparency of the process and strong team collaboration. Get in touch to discuss your ideas - we will be happy to hear you over the phone, read your email letter or see you at our London headquarters.