Monday, December 31, 2012

Data Type and format specifier

DATA Types
Objective supports all data type which are in c.
Similar to c (bool) Objective C has  extra data type is BOOL used specially in COCOA.
BOOL is typedefed to signed char . which require 8 bit to store. Some time in c non zero value is consider as true so here take care if u are assigning nonzero value say 0x1230 to BOOL it will get last byte which is zero (means False or NO value).
BOOL use YES which is #defined to 1 and NO which is #defined to 0. Take care this condition also when programing comparing YES with non zero number.

Here imp is Non Zero value is not same as YES. (in c non zero is true ).
So to avoid this Its better to compare with NO (zero value) is always safe.

Forma Specifier
Objective c uses the same format specifier as the c .
There is one addition format specifier for displaying object details :- %@
when u pass %@ and object to NSLog  . NSLog calls description() method of that object which returns object details in NSString format.

XCode shortcut:- to run app use ⌘⇧R.



%@
For any object 
%%
Writes a % character.
%d,%D
signed 32-bit integer 
%u or %U
unsigned 32-bit integer 
%x
 signed int as a hexadecimal character 
%X
 unsigned int as a hexadecimal character 
%o,%O
 an unsigned 32-bit int in octal format.
%f
double value (64-bit floating-point number)
%e
64-bit floating-point double value in  exponent format.
%E
64-bit floating-point double value in uppercase  exponent format.
%g
double (64 bit floating point) in the %e format when the exponent is less than -4 or if greater or equal to the precision as %f.
%G
As the %g format but in uppercase (E).
%c
Output an unsigned char 
%C
unsigned 16-bit unichar character.
%s
Outputs an 8 bit null terminated unsigned character. 
%S
Unicode null terminted characters.
%p
void* pointer in hexadecimal format 
%a
 double also known as a 64-bit floating-point number. 
%A
as above but the exponential symbol is in uppercase.
%F
double in decimal notation

Introduction in Objective C

History
Steve jobs started NextStep to create Good UI on unix OS with Objective c language.
When apple  acquired Nextstep renamed to Cocoa and brought same on Macintosh.
Objective C is superset of C.

Objective C  is some extra feature addition on top of C.

Apple has provided XCode as IDE for  objective c.

1.Hello world Program


#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
     NSLog (@"Hello, Objective- C!");
     return (0);

In Xcode following are the extension with language code it contains
.m  contain objective C code( The .m  extension originally stood for “messages” when Objective- C was first introduced)
.c contain c code
.cpp contain cpp code

there is gcc compiler handles all these 3 languages.

Here we understand the hello world:-
#import :- It is same like #include only difference is #import includes header file only once does not matter how many time u used in one file. (Not require to use #pragma once or header guard in header file). This feature is added in gcc

Foundation.h header file in the Foundation framework.
 The Foundation framework handles features found in the layers below the user interface,
such as data structures and communication mechanisms.

Note:- Xcode uses all precompiled header file of framework to avoid time of build process.



main() is same as c .

NSLog() is COCOA function same like printf(). To print things on console.
Difference in NSLog() and printf() is  NSLog adds "\n" automatically. and NSLog() can be used to print object with %@.

All function with starting "NS" came from COCOA framework. NS is taken from NextSTEP.

When u add @ sign before string then that string will be treated as NSString which is from cocoa.
(in short NSString is class in used to manipulate and work with strings).

Some tips for Xcode:-
As Objective c is runtime language(every call to function decide at run time depend upon its object type.) so max compile time error  will shown as warning . There is setting in Xcode u can make treat Warnings as error.



About

Powered by Blogger.