<<

Creating a Custom

Class in

By Mark Mudri

March 28, 2014

Executive Summary:

Making an iOS application requires the use of Xcode, an integrated development environment (IDE) developed by Apple. Within the interface there are many premade classes and objects. When developing an application for a specific purpose, these premade tools may not perform exactly how the wants. Therefore, it is useful to a new class or object to suit the needs of the program. This is an overview of making custom Classes.

Keywords: Xcode, Custom Class, Objective –C Programming, Object. Table of Contents

Introduction……….3

Background………..3

Create a Class………4

Conclusion…………7

References…………8

2

Introduction

Xcode is the main tool for programming iOS applications. It is an integrated development environment (IDE) developed by Apple that has many user friendly attributes such as a storyboard interface, code composer, , and simulator. The used is objective-C. This is an object oriented language that allows for creative user interactions. Within Xcode and the objective-C language there are a number of predefined classes. While these are very useful, creating a unique application requires manipulation of these classes for the improvement of the functionality or appearance of the application.

Background

Before creating a custom class, some information is needed. An object is something that can be described by attributes. It can be an , storage or a tool for performing calculations. The attributes can describe the look and how the object act when interfacing with a user or another object. A classification groups objects with the same type of attributes. For example, a person named Bill can be described by his name and age. Every person is different, but they can be described with the same descriptors. In this example, a person is the class and Bill is an object of that class. Name and Age are attributes that can be used to describe any person. The attributes can be the same or different for another instance of the class person.

Classes in objective C can also have subclasses inside them. This means that they will be the main class called Super Class will contain basic data and subclasses. The subclasses will contain specific data than the main class. Continuing on the example started the Super Class person could contain the subclass for male and female specific data. The subclass inherits attributes from the superclass in a hierarchy form.

A few other terms that are important to know are an operation and a method. An operation is a procedure that an object performs or has performed to it. An operation of a specific class is a method.

3

Creating a Class

To make a new class in Xcode it is advised to make a new separate from the other code and call the class. This can be done by making a new file. To make a new file, click on File, and then New à File. A selection screen will come up, for an iphone app, select touch and objective C class. The next screen a name of the new class is entered and an existing class to inherit the attributes from is chosen from a drop down menu. For an object use NSObject.

On the main in Xcode a header and a source file are created and displayed for coding. A class needs two pieces: interface and implementation. The interface is up in the header file and this tells how exactly the object will interact with other objects and the user. The implementation is written in the source file. This will contain the code for each method declared in the interface. The header file is imported by the code #import ”mmPerson.h” the of the source file.

Now that there is a class, it needs the attributes to be declared. In the header file in between the line @interface mmPerson: NSObject and @end, the attributes name and age are declared. In objective C, these are declared by stating them as properties of an objective C class. The class for each kind of data is used. Name is declared as an NSString property. Age will be

4 declared as an NSNumber. The code looks like this “@property (nonatomic, strong) NSString *name;“ Also in the header file an initializer method will be created.

The code coming up makes use of the alloc and init methods. These methods are in contained in all classes. They are used for memory purposes. The alloc method is a class method that every class has. It returns a pointer to a new instance that needs to be initialized. An uninitialized instance may exist in memory, but it is not ready to receive messages. The init method is an instance method that every class has. It initializes an instance so that it is ready to work.

Also in the header file, methods can be declared. Putting in this line of code after the property declarations initializes an instance of the class person: “- (id)initWithName: (NSString *)name andAge:(NSNumber*)age;” More istances can be declared, but for simplicity and observation. The last line of code added in the header file is a method for our object that will describe the object as a string with the attributes. The actual actions will be added into the source file.

In the source file, repeat the initialization line from the header file only delete and add some curly . Inside we can put what happens when the object is called in this instance the file will return a variable called self that contains the name and age of the person. Below that we will add the actual function of the description call. This will return a string that says Person1, name is age. The code for this is [NSString stringWithFormat:@”Person1,%@ is

5

%@”,self.name, self.age]; This will work for our first class. Now the class is finished, we can make a call of the class and see the result.

For the display of the class, a viewcontroller is needed with a single that is big enough to hold the string that is returned from the mmPerson class. In a simple one view app open drag a label from the list of available objects. The code for the viewcontroller files are shown below. Make sure to import the class using #import at the top of the source file. Notice the instance of mmPerson entitled *person1. The code shown allows memory to store the name, Bill, and his age, 32. The line after this declared instance changes the text in the label to display the description of the object.

6

Conclusion

The output is as expected. The class can now be used throughout the application. Review of the process, Open a new class with NSObject as the basis for the new class. Declare the properties of the new class in the header file, and any actions that the object will perform. Then in the source file code in the actual actions and what the returns will be. That’ the simple version of how to create a Custom class.

7

1. Hillgass, Aaron and mikey Ward. Objective – C Programming: The Big Nerd Ranch Guide 2nd edition. 2. Keur, Christian, Aaron Hillgass and Joe Conway. iOS Programming: The Big Nerd Ranch Guide 4nd edition. 3. Apple Developer. “Programming with Objective-C”

8