The Basics of iOS7 Programming - FREE Guru Level Training For Beginners (2014)

FREE Guru Level Training For Beginners (2014)

Chapter 1: The Basics of iOS7 Programming

“I think we're having fun. I think our customers really like our products. And we're always trying to do better”- Steve Jobs

In this chapter, you will learn about the basics of iOS7 programming, including:

· What is programming?

· Object-oriented programming

· Objects

· What are classes and instances?

· Making a class

· Variables

· Constructors

· How to create an object

· How to declare the source file

· Commands, directories and paths

· Using objective-C

· Why you need to know this?

Every device has an operating system. Currently, the two leading operating systems are the Android and the iOS. These platforms are used in mobile systems.

The iOS is an operating system that was made by Apple, exclusively for Apple devices. The Android, on the other hand, is used by companies like Sony Ericsson, Samsung and other devices manufactured by other companies.

The iOS7

As Apple comes up with new devices, it also upgrades its operating system. New devices have different and advanced features and processors installed in them. These can only be supported by the latest software.

The iOS7 is the latest operating system that was recently introduced by Apple.

What is programming?

Programming and coding are words that are used interchangeably. These enable us to command our devices and the command is then, applied by the computer .

Every device, software and platform operates by using a programming language. Apple uses Objective-C and this is also a form of Object Oriented Programming (OOP).

OOP, in laymen terms, is simply a number of objects that interact with each other. These objects are used to command the computer and to issue statements. That cannot be done without a blueprint.

The blueprint describes the object. For the object to perform certain tasks, one needs to create certain ‘instances’ that allow the object to do so. The blueprint is referred to as the “class”.

Object-Orient Programming

Objects are used for object-orient programming that, as the name suggests, relies on the use of objects. Various programmers use OOP to make different apps. No matter how complex or simple an app may be, the OOP is what enables a programmer to build, maintain and then, upgrade it when need be.

By way of example, using the iOS6, on an Apple device will causes certain apps to crash. To make these apps work you have to upgrade the iOS platform to prevent this problem from occurring, right? This is because app developers have programmed the app to function this way. This is just one example of how app developers use OOP.

Programmers find that using objects to create apps yields better results because the app is more reliable as opposed to other forms of programming. Python is an example of an OOP.

Objects

Objects are self-contained components that consist of various properties and methods that are used to make any app. Object properties are what the object knows to do while object methods refer to what the object is capable of doing. Object methods make the object workable and enable various tasks to be hidden. Methods also allow the object to be standardized for various types of objects.

An object could be a function, variable or data structure too. In object-oriented programing the object refers to a class (which is used to make the object) that is used to make a combination of variables, functions or data structures. Different objects work together so that a project can function in a particular manner. App developers use objects to make the app workable. Think of objects as you would of the 5 senses- it is because of these that any project is capable of functioning. Different objects have different functions and in totality these are combined to form the project.

When it comes to computer science there are variables and then, there are fixed/constant values. The variable consists of data and can be changed. Objects usually contain the variable of a class that the variable is an instance/object of. This is an example of a variable that you will come across later in this book:

/* Setup your scene here */

//2

NSLog(@"SKScene:initWithSize %f x %f",size.width,size.height

What are Classes and Instances?

The class is also referred to as the blueprint for an object because that is precisely what it is. What the class does is that it is designed and then, programmed to make an object. The app developer determines what properties and methods to assign to every object. In later chapters you would discover that app developers make several classes when making an app. This is because each class only has one responsibility.

Instances are specific objects that are made from particular types of classes. The process of making an object is called instantiation.

Making a Class

You can try making class on your own. Here’s a sample of a class-

public class Cat{

String breed;

int age;

String color;

void meowing(){

}

void eaten(){

}

void playing (){

}

}

Variables

Local variables: these are defined within methods, blocks and constructors. Variables defined within methods are destroyed after the method is complete.

Instance variables: such variables are usually within classes but they are outside the method(s). Once the class has been loaded it is possible for app developers to instantiate instance variables. One can access these from within a method, block or constructor of a specific class.

Class variables: these are declared within the class and outside of a method.

In the example given above meowing, eaten and playing are examples of methods.

Constructors

Classes have constructors and unless you specify and write the constructor that you intend on using for a class, the Java compiler will automatically build a default constructor for that specific class. Constructors usually have the same name as a class. Here’s an example of a constructor:

public class Cat{

public Cat(){

}

public Cat(String name){

// This constructor has one parameter, name.

}

}

How to create an Object

You can also make an object on your own. There are 3 steps to the process:

Declaration: A variable declaration that has a variable name and the object type.

Instantiation: A keyword is used in order to make a new object.

Initialization: The keyword calls to a constructor to initialize the new object.

public class Cat{

public Cat(String name){

// This constructor has one parameter, name.

System.out.printIn(“Passed Name is:” + name);

}

public static void main(String []args){

//Following statement would create an object myCat

Cat myCat= new Cat(“cotton”);

}

}

Once you run this program it would generate the following result:

Passed Name is :cotton


Here’s how you can access instance variables and methods:

/* First create an object */

ObjectReference = new Constructor();

/* Now call a variable as follows */

ObjectReference.variableName;

/* Now you can call a class method as follows */

ObjectReference.MethodName();

This is an example of how one can access all instance variables and the methods of a class:

public class Cat{

int catAge;

public cat(String name){

// This constructor has one parameter, name.

System.out.println("Passed Name is :" + name );

}

public void setAge( int age ){

catAge = age;

}

public int getAge( ){

System.out.println("Cat’s age is :" + puppyAge );

return catAge;

}

public static void main(String []args){

/* Object creation */

Cat myCat = new Cat( "cotton" );

/* Call class method to set cat’s age */

myCat.setAge( 1 );

/* Call another class method to get cat’s age */

myCat.getAge( );

/* You can access instance variable as follows as well */

System.out.println("Variable Value :" + myCat.catAge );

}

}

When you run this program, here’s what your result would be:

Passed Name is :cotton

Puppy's age is :1

Variable Value :1

How to declare the Source File

Source files are what app designers use when they have to design an app.

Various apps are designed differently and the source file contains information on the design which is why app developers are very careful about these. Once the design is prepared it is then, exported in the form of a jpg/png file. The source file also consists of the source code.

Each source file has only one public class though these can contain more than one non-public classes. The name of the source file needs to be the same as that of the public class name and at the end of the name you need to put .java. So if the class name is .public class Cat{} the source file should be entitled Cat.java.

Java packages make it easier for app developers to locate various interfaces and classes and if these are used, the package statement needs to be inserted as the first statement in your source file. import java.io.File; is an example of an import statement. This will call on the compiler so it can load classes in the following directory:

java_installation/java/io :


Commands, Directories and PATHS

Commands, directories and PATHS are important when it comes to making projects. Different directories need to be combined so that they can define the path for a project. When you command the computer to do something it uses the path, defined for it, to act on the command given to it.

Now, when it comes to app development you are required to define the path so you can use your computer to develop an app. If you do not do so it would be difficult for you to run various java codes for your apps. Without this making an app can be challenging. So, first of all, you need to change the path of your Mac OS X platform.

Here’s how you can do so:

1. Go into Utilities>Terminal windows.

2. Next use the following command: ~/.bash_profile; open ~/.bash_profile.

3. Go into Text Edit so you can open the file.

4. Add the following directory export PATH="$HOME/.rbenv/bin:$PATH"

5. Save the file, quit and then, execute it. Insert the following command- source ~/.bash_profile

6. Confirm the path and then, add the echo $Path command.

By the time you are done with this it would be possible for you to run any java code.

Using Objective-C

Objective-C is used to define a class. So, in simpler words, Objective-C could help app developers use the objects so that they can decide and determine what each object should be used for. This way app developers can decide what data to include in each variable and how to use it.

There are 2 classes- the .h file (header file) and the .m file (the source file). The header files help tell other classes what sorts of interactions are available to them. The source file is where the codes and logics are stored. The source file is important for every project.

Objective-C can be used to make your own string.

NSString is an example of a Class that can be used to store various strings. To familiarize yourself with the process create the NSString. For now make the following

string:

NSString*myString=@”crystal ball”;

Strings enable app developers to style and format the app’s text.

Why You Need to Know This

If you have bought this book, chances are you’re a beginner who has no idea what the basics of software development are. You need to know about the basics before you can go on to the next chapter. This chapter just covered the basics on what you need to know to about the iOS, iOS7 and iOS programming.

In the upcoming chapters you would learn about the tools you need to make your own app with the iOS7. You would also be presented with a step-by-step guide to help you make your very first app.