Friday, 5 June 2015

Variables in Objective C

Variables in objective C

Variables are used to store the values of content.Objective C is strongly typed language not like the Swift new programming language of apple. Which means that no need to write the Data types or nature of data explicitly.

int i; in objective c
var i; in swift

Objective C support all types of data types.

But it uses general NSInteger,NSArray,NSString etc.
These are easy to use rather then simple integer and array.

for example:
NSArray *i;

[i count];

As you can see it is easy to count the number of Elements in an array. Rather then writing all function of counting in c

EXAMPLE: main()
{
int a[10]={1,2,3,4,5};
for(i=0;i<=a.length;i++)
{
int a=i;
}
cout<<a;
return 0;
}

but now easy to use objective c with just one count method calling.

For NSString Case-
NSString *firststring=@"hello World";
NSString *SecondString=[firststring stringByAppendingString:@" This is good world"];

Now you can see that first line create string .
But stringByAppendingString Create new string with receiver string and append with arguemented string.

Its output will be Hello World This is a good world.

Nesting of string call also done.
[string1 stringByAppendingString:[string2 stringByAppendingString:@"helloworld"]];

first inner part solve then append with string1+inner part.

Now come up with stringWithFormat method.
NSString *string1=@"hello world";
NSString *string2=@"this is you";
NSString *string3=[NSString stringWithFormat:"%@%@%@",string1,string2,@"My name is suraj"];

Here you can see that stringWithFormat is used combine multiplestring with just "-" that specifier between various strings.

There are many other NSString classes avilable.




No comments:

Post a Comment