How to Use Syntax and Properties in iOS Using Objective C?

There are plenty of ways to use the properties in objective c but today we will be using the simplest method to do it.

As we all know that iOS application has been made in two different languages i.e -Objective-C and Swift.

But in this tutorial, we will be focusing on the Objective-C syntax as it is one of the finest,

the simplest languages to develop the applications in ios.

objective-c

Semicolon:

In Objective-C Semicolon are known as statement terminators. It means that when we end some statement, then it must be ended with a semicolon,

For Example - Nslog(@"This is Semicolon");

Here in the above example, the semicolon, in the end, states that the statement has been completed or we can say terminated.

Comments:

As we know, Comments are like helping hands which are ignored by Compiler at runtime. For example, if we want to give some information about

the particular code.it starts with /* and ends with */

For Example - /* this is a line to show some data */

Here it defines a particular code that will show some data and it is ignored by the compiler as it contains the comments only.

Variables:

In Objective-C, Variables are used to give a particular name to a storage area.

Each variable in ios has a type, which tells the size and layout of variable’s memory.

Some of the different types of variables are,

Int, char, float.

if you want to define a variable then you can do this like the example below,

Int name – Here name has a data type which is Integer.

Decision Making:

As we all know that Objective-C works on the basis of oops concept and that’s why,

it supports all the decision making statements,

Example – For loop, if else statement, while loop, switch statement etc.

   NSString *string1 = @"sandeep";

    NSString *string2 = @"Yes this contains the name sandeep";

    

    if ([string2 containsString:string1]) {

        NSLog(@"yes");

    }

    else{

        NSLog(@"No");

    }
objective-c , ios application

Here in the above example, we can see that values have been compared using if else statement.

Properties:

In Objective-C, you can declare a property by adding @property before declaring an object or element.

Property can be of different types like (strong, weak, non-atomic, atomic,  retain, copy).

For example, if you want to declare a String then you can simply name it like this.

@property (nonatomic, strong) NSString *string1;

Array:

We have already learned about the basic terms like array in our institutes but here,

I am going to show you the syntax to use an array in objective-c.

As we know array is defined as the collection of objects of same data types.

In Objective-C, We use an array in the form of NSArray.

There are basically two types of Array in Objective-C.

NSArray – In this type, we can add objects into it once only, it means it is static or immutable.

NSMutableArray – This is opposite to NSArray, We can add or remove objects from it again n again.

NSArray *colors = @[@"Red", @"Yellow", @"Orange", @"Green", @"Blue", @"Violet"];


Here we cannot add objects to it again, It means it can only have 6 objects inside it.

NSMutableArray *sports = [NSMutableArray arrayWithObjects: @"Cricket", @"Football", @"Hockey", @"Table Tennis", nil];

Here we can add more objects inside this array as it is mutable.

We just need to call the name of the particular array and then,

we can add objects inside it like this.

[sports addObject:@"Basketball"];

array, objective-c

Now you can see in the above snapshot that the final output contains 5 elements inside it.

Strings:

Now if I ask you about string then you will not be able to tell me the proper answer.

As according to you a string must be a thread.

But in Objective-C, if you want to create a string object then NSString can be used.

For Example – if you want to write a string or a message, then you can do it like this.

NSString *String1 = @"My name is ABC";

This states that String1 will display a message.

If you want to print the particular string then you can use it as,

        NSLog(@"%@",String1);

Dictionary:

A Dictionary contains key and values pairs inside it.

We can use it to store a particular value for a particular key and then can access that value by using its key.

Similarly to an NSArray, it is also mutable and Non- Mutable.

NSDictionary can contain some key values pair which cannot be edit again and again.

NSMutableDictionary is opposite as we can add more key-value pair inside it in future which is mutable.

    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"Sandeep" forKey:@"name"];

Now it will store the object as Sandeep and its key will be named.

So if we want to access it in future then we can call the particular object by using its key only.

    NSLog(@"%@",[dictionary valueForKey:@"name"]);

This will print its value as Sandeep if you use this code in your Xcode playground to see its result.

For your reference, i am attaching the snapshot below to make it easy to understand.

nsdictionary, objective-c

UserDefaults:

Now, this is one of the most important terms which you can use in your projects.

Basically NSUserDefaults are used to store some data, In short, you can save your data inside user defaults,

and can access it within the whole application anytime.

Similarly to NSDictionary it also uses a key-value pair to store the data inside it and you can call its value any time by calling its key.

You can initialize them anywhere in the application but I recommend you to initialize it in your app delegate file.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

This will initialize the user-defaults and now you can save data inside it easily.

[defaults setInteger:9001 forKey:@"HighScore"];

Now, this will store the integer value 9001 inside the defaults and you can access it with the key “HighScore”.

One of the most important benefits of using defaults is, you don’t need to initialize it, again and again, you can save more and more objects inside it.

Summary:

I hope you found this tutorial useful, Remember this is only the basic of Objective-C and if you are learning this technology from few days then you will found,

this tutorial much easier as it contains all the basic syntax and properties in it.

 

Latest posts by Rahul Huria (see all)

Leave a Comment