Basic functions of a programming language like Array,Strings and NSLog
In this post lets see about basic functions of a programming language like Array,Strings and NSLog.And in this program we are not displaying the output on the simulator ,but in a LOG report .Now create a new project in Xcode ,and read ahead .
NSLog is nothing but an printf statement in C or like the cout statement in C++ ,or like the System.out.println function in java. The letter NS stands for NextStep ,it is a company with unix-like family OS founded by Steve Jobs,and so objective C uses "NS" as an acronym in many of its keywords like NSObject,NSArray, NSString,NSLog etc.
Lets see an example of a sample program containing the above three functions.At first we must declare it in the .h file . If you are not sure what is an .h file ,please check my previous posts.
So my ViewController.h file looks like the one above. Here i have created an object for string and an object for array. NSString is an class which is called when a strings are used . Here the object for string i named is "str" ,and of course i have declared the object as an pointer object ,for fast processing . And again i declared an array object with NSArray class,with array object name as "ary". Now lets see the .m file
In a programming language an array must be allocated in a memory , the declared array object name "ary"must be allocated in the memory through this coding [NSArray alloc] .
And an array is an collection of well defined elements/objects . To insert the elements/objects/data into the array ,we use the coding [initWithObjects:@"data1",@"data2",nil]; .As i have told earlier we are going to show the output in the LOG report . So we are using the NSLog function.
To just print a statement we can simply write as NSLog(@"hi this is an statement"); but to print an data within an object ,like our objects str, ary .we write the code as NSLog(@"%@",str); for string object and NSLog(@"%@",ary); for array object. Now i have used "%@" in the NSLog statements .
The reason why i used is since the above to objects(str,ary) contains string statements,we need to mention in the NSLog statement the type of the data ,whether its string,numeric,float etc .similarly for numeric data ,we use "%d" ,for float "%f" .
so the syntax for NSLog goes like this NSLog(@"type",name of the object);
To insert a string data into the string object ,write the code as in the above picture
str=@"this is a string";
Now we have successfully created the project ,just hit the PLAY/RUN button .
The simulator opens ,but the output is displayed only in the LOG report ,so put the simulator away and click the up arrow found in the bottom of Xcode work area in .m file (the place where u code ) . You can see the up arrow mark in the image below
Click on the up arrow mark ,like u can see in the left side bottom of the above image.And it shows the LOG report with the output ,check the bottom image.
Try these simple basics stuffs , and try to create a program using the above three functions . Feel free to comment for an doubts . Cheers!!