HI THIS TOUTORIAL WILL HELP FULL FOR STARTUP IN IOS CREATED BY NAVEENRAJU
IOS BASICS EXPLINATIONS BY NAVEENRAJU
OBJECTIVE C
Primitive Data Types and Operators:Primitive Data Types:Data Types
Data type represents what kind of data that is represented and store it in memory of our pc.
data types and the memory that is needed:
C standard types
char. Is the basic unit within a Objective-C and C program. All others are derived from the size of char. char actually is a single character like 'a' or '3'. It's size is defined in limits.h, which should reside in /usr/include. In my limits.h file it says:
Derivative types
Objective-C data types
int Data Type
The Objective-C int data type can store a positive or negative whole number (in other words a number with no decimal places).
The actual size or range of integer that can be handled by the int data type is machine and compiler implementation dependent.
Typically the amount of storage allocated to int values is either 32-bit or 64-bit depending on the implementation of Objective-C on that platform or the CPU on which the compiler is running.
Example size ranges for signed and unsigned int :
Unsigned int values
32-bit os-----------> maximum range of int is 0 to 4294967295.
64-bit os-----------> maximum range of int is 0 to 18,446,744,073,709,551,615. .
signed int values
32-bit os----------->−2,147,483,648 to +2,147,483,647
64-bit os-----------> −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
Syntax:
int myoctal = 024;
Similarly, an int may be expressed in number base 16 (hexadecimal) by preceding the number with 0x, for example:
int myhex = 0xFFA2;
char Data Type
The Objective-C char data type is used to store a single character such as a letter, numerical digit or punctuation mark or space character. For example, the following lines assign a variety of different characters to char type variables:
char myChar = 'w'; char myChar = '2'; char myChar = ':'; Special Characters/Escape Sequences
Commonly used special characters supported by Objective-C are as follows:
\a - Sound alert
\b - Backspace \f - Form feed \n - New line \r - Carriage return \t - Horizontal tab \v - Vertical tab \\ - Backslash \" - Double quote (used when placing a double quote into a string declaration) \' - singlecode (used when placing a double quote into a string declaration) float Data Type
The Objective-C float data type is used to store floating point values, in other words values containing decimal places up to 7 digits.
For example,
523.312 would be stored in a float data type.
In practice, all floating point values are stored as a different data type (called double) by default.
We will be covering the double data type next, but if you specifically want to use a floatdata type, you must append an f into the end of the value.
For example:
float myfloat = 123.432f
For example, we can express 23.31 x 104 in Objective-C as:
float myfloat = 23.31e4
double Data Type
The Objective-C double data type is used to store larger values than can be handled by the float data type(9 decimal places).
The term double comes from the fact that a double can handle values twice the size of a float.
Example: double=10.234455545;
id Data Type
Objective-C is an object oriented language. As such much of the way a program will be structured is in the form of reusable objects.
These objects are called upon to perform tasks and return results.
Often, the information passed into an object and the results returned will be in the form of yet another object.
The id data type is a general purpose data type that can be used to store a reference to any object, regardless of its type.
Example:
id s = [[NSString alloc] initWithString:@"Hello, World"];
weakly typed and requires use of square brackets.
s-------> Reference Created for NSString
BOOL Data Type
Objective-C, like other languages, includes a data type for the purpose of handling true or false (1 or 0) conditions. Such a data type is declared using either the _Bool or BOOLkeywords. Both of the following expressions are valid:
_Bool flag = 0;
BOOL secondflag = 1; Objective-C Data Type Qualifiers
So far we have looked at the basic data types offered within the context of the Objective-C programming language. We have seen that data types are provided for a number of different data declaration and storage needs and that each data type has associated with it some constraints in terms of what kind of data it can hold. In fact, it is possible to modify some of these constraints by using qualifiers. A number of such qualifiers are available and we will look at each one in turn in the remainder of this chapter.
long
The long qualifier is used to extend the value range of a data type. For example, to increase the range of an integer variable, the declaration is prefixed by the qualifier:
long int mylargeint;
The amount by which a data type's range is increased through the use of the long qualifier is system dependent, though on many modern systems int and long int both have the same range, making use of the qualifier unnecessary. The long qualifier may also be applied to the double data type. For example:
long double mydouble; long long
It is safe to think of the long long qualifier as being equivalent to extra long. In the case of an int data type, the application of a long long qualifier typically will change the range from 32-bit up to 64-bit:
long long int mylargeint; short
So far we have looked at qualifiers that increase the storage space, and thereby the value range, of data types. The short qualifier can be used to reduce the storage space and range of the int data type. This effectively reduces the integer to 16-bits in width, limiting the signed value range to −32,768 to +32,767:
short int myshort; signed / unsigned
By default, an integer is assumed to be signed. In other words the compiler assumes that an integer variable will be called upon to store either a negative or positive number. This limits the extent that the range can reach in either direction. For example, a 32-bit int has a range of 4,294,967,295. In practice, because the value could be positive or negative the range is actually −2,147,483,648 to +2,147,483,647. If we know that a variable will never be called upon to store a negative value, we can declare it as unsigned, thereby extending the (positive) range to 0 to +4,294,967,295. An unsigned int is specified as follows:
unsigned int myint;
Qualifiers may also be combined, for example to declare an unsigned, short integer:
unsigned short int myint = 10;
Note that what using unsigned, signed, short and long with integer values, the int keyword is optional. The following are all valid:
short myint; long myint; unsigned myint; signed myint; Video Demo on PrimitiveDataTypes:Code For above Video Demo:ViewController.h#import "ViewController.h"
#import "Myclass.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Myclass *mc=[[Myclass alloc]init];
id ref;
ref=mc;
[ref status];
int num1=23;
float num2=23.31f;
double num3= 23.34567;
char c='a';
char *c1="Hello";
//char c1[]="Hello";
NSString *str=@"Naveen";
int sum1=num1+num2;
float sum2=num1+num2;
float mul1=num2*num3;
double mul2=num2*num3;
NSLog(@"the Integers sum of two numbers %i + %f =%i",num1,num2,sum1);
NSLog(@"the Float sum of two numbers %i + %f =%f",num1,num2,sum2);
NSLog(@"the Float Mul of two numbers %f + %lf =%f",num2,num3,mul1);
NSLog(@"the Double Mul of two numbers %f + %lf =%lf",num2,num3,mul2);
NSLog(@"%s %@",c1,str);
NSLog(@"%c",c);
NSLog(@"%s",c1);
bool b1=0;
bool b2=1;
bool b3=2;
bool b4=3;
NSLog(@"%d",b1);
NSLog(@"%d",b2);
NSLog(@"%d",b3);
NSLog(@"%d",b4);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
MyClass.h
//
// Myclass.h
// PrimitiveDataTypes
//
// Created by apple on 01/02/13.
// Copyright (c) 2013 apple. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Myclass : NSObject
{
NSString *n;
int i1;
}
-(void) status;
@end
MyClass.m
//
// Myclass.m
// PrimitiveDataTypes
//
// Created by apple on 01/02/13.
// Copyright (c) 2013 apple. All rights reserved.
//
#import "Myclass.h"
@implementation Myclass
-(void)status
{
int i1=233138;
NSString *n=@"Hello World";
NSLog(@"The int value is--------->%i",i1);
NSLog(@"The Nsstring value is--------->%@",n);
}
@end
Operators:
Operator is a Symbol it performs some specific task.
What is an Expression?
The most basic expression consists of an operator, two operands and an assignment. The following is an example of an expression:
int myresult = 1 + 2;
In the above example the (+) operator is used to add two operands (1 and 2) together. The assignment operator (=) subsequently assigns the result of the addition to an integer variable named myresult. The operands could just have easily been variables (or a mixture of constants and variables) instead of the actual numerical values used in the example.
In the remainder of this chapter we will look at the various types of operators available in Objective-C.
The Basic Assignment Operator
We have already looked at the most basic of assignment operators, the = operator. This assignment operator simply assigns the result of an expression to a variable. In essence, the = assignment operator takes two operands. The left hand operand is the variable to which a value is to be assigned and the right hand operand is the value to be assigned. The right hand operand is, more often than not, an expression which performs some type of arithmetic or logical evaluation, the result of which will be assigned to the variable. The following examples are all valid uses of the assignment operator:
int x; // declare the variable x = 10; // Assigns the value 10 to a variable named x x = y + z; // Assigns the result of variable y added to variable z to variable x x = y; // Assigns the value of variable y to variable x
Assignment operators may also be chained to assign the same value to multiple variables. For example, the following code example assigns the value 20 to the x, y and z variables:
int x, y, z; x = y = z = 20; Objective-C Arithmetic Operators
Objective-C provides a range of operators for the purpose of creating mathematical expressions. These operators primarily fall into the category of binary operators in that they take two operands. The exception is the unary negative operator (-) which serves to indicate that a value is negative rather than positive. This contrasts with the subtraction operator (-) which takes two operands (i.e. one value to be subtracted from another). For example:
int x = -10; // Unary - operator used to assign -10 to a variable named x x = y - z; // Subtraction operator. Subtracts z from y
The following table lists the primary Objective-C arithmetic operators:
Note that multiple operators may be used in a single expression.
For example:
x = y * 10 + z - 5 / 4;
Whilst the above code is perfectly valid it is important to be aware that Objective-C does not evaluate the expression from left to right or right to left, but rather in an order specified by the precedence of the various operators. Operator precedence is an important topic to understand since it impacts the result of a calculation and will be covered in detail the chapter entitled Objective-C 2.0 Operator Precedence.
Compound Assignment Operators
In an earlier section we looked at the basic assignment operator (=). Objective-C provides a number of operators designed to combine an assignment with a mathematical or logical operation. These are primarily of use when performing an evaluation where the result is to be stored in one of the operands. For example, one might write an expression as follows:
x = x + y;
The above expression adds the value contained in variable x to the value contained in variable y and stores the result in variable x. This can be simplified using the addition compound assignment operator:
x += y
The above expression performs exactly the same task as x = x + y but saves the programmer some typing.
Numerous compound assignment operators are available in Objective-C. The most frequently used are outlined in the following table:
Increment and Decrement Operators
Another useful shortcut can be achieved using the Objective-C increment and decrement operators (also referred to as unary operators because they operate on a single operand). As with the compound assignment operators described in the previous section, consider the following Objective-C code fragment:
x = x + 1; // Increase value of variable x by 1 x = x - 1; // Decrease value of variable x by 1
These expressions increment and decrement the value of x by 1. Instead of using this approach it is quicker to use the ++ and -- operators. The following examples perform exactly the same tasks as the examples above:
x++; Increment x by 1 x--; Decrement x by 1
These operators can be placed either before or after the variable name. If the operator is placed before the variable name the increment or decrement is performed before any other operations are performed on the variable. For example, in the following example, x is incremented before it is assigned to y, leaving y with a value of 10:
int x = 9; int y; y = ++x;
In the next example, however, the value of x (9) is assigned to variable y before the decrement is performed. After the expression is evaluated the value of y will be 9 and the value of x will be 8.
int x = 9; int y; y = x--; Comparison Operators
In addition to mathematical and assignment operators, Objective-C also includes set of logical operators useful for performing comparisons. These operators all return a Boolean (BOOL) true (1) or false (0) result depending on the result of the comparison. These operators are binary operators in that they work with two operands.
Comparison operators are most frequently used in constructing program flow control logic. For example an if statement may be constructed based on whether one value matches another:
if (x == y) // Perform task
The result of a comparison may also be stored in a BOOL variable. For example, the following code will result in a true (1) value being stored in the variable result:
BOOL result; int x = 10; int y = 20; result = x < y;
Clearly 10 is less than 20, resulting in a true evaluation of the x < y expression. The following table lists the full set of Objective-C comparison operators:
Boolean Logical Operators
Objective-C also provides a set of so called logical operators designed to return boolean true and false. In practice true equates to 1 and false equates to 0. These operators both return boolean results and take boolean values as operands. The key operators are NOT (!), AND (&&), OR (||) and XOR (^).
The NOT (!) operator simply inverts the current value of a boolean variable, or the result of an expression. For example, if a variable named flag is currently 1 (true), prefixing the variable with a '!' character will invert the value to 0 (false):
bool flag = true; //variable is true bool secondFlag; secondFlag = !flag; // secondFlag set to false
The OR (||) operator returns 1 if one of its two operands evaluates to true, otherwise it returns 0. For example, the following example evaluates to true because at least one of the expressions either side of the OR operator is true:
if ((10 < 20) || (20 < 10)) NSLog (@"Expression is true");
The AND (&&) operator returns 1 only if both operands evaluate to be true. The following example will return 0 because only one of the two operand expressions evaluates to true:
if ((10 < 20) && (20 < 10)) NSLog (@"Expression is true");
The XOR (^) operator returns 1 if one and only one of the two operands evaluates to true. For example, the following example will return 1 since only one operator evaluates to be true:
if ((10 < 20) ^ (20 < 10)) NSLog (@"Expression is true");
If both operands evaluated to be true or both were false the expression would return false.
The Ternary Operator
Objective-C uses something called a ternary operator to provide a shortcut way of making decisions. The syntax of the ternary operator (also known as the conditional operator) is as follows:
[condition] ? [true expression] : [false expression]
The way this works is that [condition] is replaced with an expression that will return either true (1) or false (0). If the result is true then the expression that replaces the [true expression] is evaluated. Conversely, if the result was false then the [false expression] is evaluated. Let's see this in action:
int x = 10; int y = 20; NSLog(@"Largest number is %i", x > y ? x : y );
The above code example will evaluate whether x is greater than y. Clearly this will evaluate to false resulting in y being returned to the NSLog call for display to the user:
2009-10-07 11:14:06.756 t[5724] Largest number is 20 Bitwise Operators
In the chapter entitled Objective-C 2.0 Data Types we talked about the fact that computers work in binary. These are essentially streams of ones and zeros, each one referred to as a bit. Bits are formed into groups of 8 to form bytes. As such, it is not surprising that we, as programmers, will occasionally end up working at this level in our code. To facilitate this requirement, Objective-C provides a range of bit operators. Those familiar with bitwise operators in other languages such as C, C++, C# and Java will find nothing new in this area of the Objective-C language syntax. For those unfamiliar with binary numbers, now may be a good time to seek out reference materials on the subject in order to understand how ones and zeros are formed into bytes to form numbers. Other authors have done a much better job of describing the subject than we can do within the scope of this book.
For the purposes of this exercise we will be working with the binary representation of two numbers. Firstly, the decimal number 171 is represented in binary as:
10101011
Secondly, the number 3 is represented by the following binary sequence:
00000011
Now that we have two binary numbers with which to work, we can begin to look at the Objective-C's bitwise operators:
Bitwise AND
The Bitwise AND is represented by a single ampersand (&). It makes a bit by bit comparison of two numbers. Any corresponding position in the binary sequence of each number where both bits are 1 results in a 1 appearing in the same position of the resulting number. If either bit position contains a 0 then a zero appears in the result. Taking our two example numbers, this would appear as follows:
10101011 AND
00000011 ======== 00000011
As we can see, the only locations where both numbers have 1s are the last two positions. If we perform this in Objective-C code, therefore, we should find that the result is 3 (00000011):
int x = 171; int y = 3; int z; z = x & y; // Perform a bitwise AND on the values held by variables x and y NSLog(@"Result is %i", z); 2009-10-07 15:38:09.176 t[12919] Result is 3 Bitwise OR
The bitwise OR also performs a bit by bit comparison of two binary sequences. Unlike the AND operation, the OR places a 1 in the result if there is a 1 in the first or second operand. The operator is represented by a single vertical bar character (|). Using our example numbers, the result will be as follows:
10101011 OR
00000011 ======== 10101011
If we perform this operation in an Objective-C example we see the following:
int x = 171; int y = 3; int z; z = x | y; NSLog(@"Result is %i", z); 2009-10-07 15:41:39.647 t[13153] Result is 171 Bitwise XOR
The bitwise XOR (commonly referred to as exclusive OR and represented by the caret '^' character) performs a similar task to the OR operation except that a 1 is placed in the result if one or other corresponding bit positions in the two numbers is 1. If both positions are a 1 or a 0 then the corresponding bit in the result is set to a 0. For example:
10101011 XOR
00000011 ======== 10101000
The result in this case is 10101000 which converts to 168 in decimal. To verify this we can, once again, try some Objective-C code:
int x = 171; int y = 3; int z; z = x ^ y; NSLog(@"Result is %i", z);
When executed, we get the following output from NSLog:
2009-10-07 16:09:40.097 t[13790] Result is 168 Bitwise Left Shift
The bitwise left shift moves each bit in a binary number a specified number of positions to the left. As the bits are shifted to the left, zeros are placed in the vacated right most (low order) positions. Note also that once the left most (high order) bits are shifted beyond the size of the variable containing the value, those high order are discarded:
10101011 Left Shift one bit
======== 01010110
In Objective-C the bitwise left shift operator is represented by the '<<' sequence, followed by the number of bit positions to be shifted. For example, to shift left by 1 bit:
int x = 171; int z; z = x << 1; NSLog(@"Result is %i", z);
When compiled and executed, the above code will display a message stating that the result is 342 which, when converted to binary, equates to 101010110.
Bitwise Right Shift
A bitwise right shift is, as you might expect, the same as a left except that the shift takes place in the opposite direction. Note that since we are shifting to the right there is no opportunity to retain the lower most bits regardless of the data type used to contain the result. As a result the low order bits are discarded. Whether or not the vacated high order bit positions are replaced with zeros or ones depends on whether the sign bit used to indicate positive and negative numbers is set or not and, unfortunately, on the particular system and Objective-C implementation in use.
10101011 Right Shift one bit
======== 01010101
The bitwise right shift is represented by the '>>' character sequence followed by the shift count:
int x = 171; int z; z = x >> 1; NSLog(@"Result is %i", z);
When executed, the above code will report the result of the shift as being 85, which equates to binary 01010101.
Compound Bitwise Operators
As with the arithmetic operators, each bitwise operator has a corresponding compound operator that allows the operation and assignment to be performed using a single operator:
Video Demo on Operators:Code For above Video Demo:ViewController.m
//// ViewController.m// OpperDemo//// Created by apple on 01/02/13.// Copyright (c) 2013 apple. All rights reserved.//
|
No comments:
Post a Comment