Objective-C Flashcards
Objective-C Overview
- developed on top of C
- Small Talk features added to make it object-oriented
- initially developed by NeXT for its NeXTSTEP OS and taken over by Apple for its iOS and MacOS
- GCC compiler
Objective-C Object Oriented
- encapsulation
- data hiding
- inheritance
- polymorphism
Objective-C Foundation Framework
- extended data types like NSArray, NSDictionary, NSSET
- file, string functions
- URL handling, data formatting, data handling, error handling
Objective-C Program Structure
- preprocessor commands
- interface
- implementation
- method
- variables
- statements and expressions
- comments
Objective-C Hello World program
import
@interface SampleClass:NSObject
- (void)sampleMethod;
@end
@implementation SampleClass
- (void)sampleMethod {
NSLog(@”Hello, World! \n”);
}
@end
int main() { /* my first program in Objective-C */ SampleClass *sampleClass = [[SampleClass alloc]init]; [sampleClass sampleMethod]; return 0; }
Objective-C Tokens
a token is either a keyword, an identifier, a constant, a string literal, or a symbol
Objective-C Semicolons
statement terminator
Objective-C Comments
- ignored by the compiler
- start with /* and terminate with */
Objective-C Identifier
- name used to identify a variable, function or any other user-defined item
- starts with a letter or an underscore followed by zero or more letters, underscores and digits
- case-sensitive
- punctuation characters such as @, $ and % are not allowed
Objective-C Reserved Words
auto else long switch break enum register typedef case extern return union char loat short unsigned const for signed void continue goto sizeof volatile default if static while do int struct _Packed double protocol interface implementation NSObject NSInteger NSNumber CGFloat property nonatomic; retain strong weak unsafe_unretained; readwrite readonly
Objective-C Whitespace
totally ignored
Objective-C Data Types
- basic types
- -integer
- -floating point
- enumerated types
- -used to define variables that can only be assigned
- -certain discrete integer values
- void type
- -no value is available
- derived types
- -pointer
- -array
- -structure
- -union
- -function
Objective-C Integer Types
char
1 byte
-128 to 127 or 0 to 255
unsigned char
1 byte
0 to 255
signed char
1 byte
-128 to 127
int
2 or 4 bytes
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int
2 or 4 bytes
0 to 65,535 or 0 to 4,294,967,295
short
2 bytes
-32,768 to 32,767
unsigned
short
2 bytes 0 to 65,535
long
4 bytes
-2,147,483,648 to 2,147,483,647
unsigned long
4 bytes
0 to 4,294,967,295
Objective-C Floating-Point Types
float
4 byte
1.2E-38 to 3.4E+38
6 decimal places
double
8 byte
2.3E-308 to 1.7E+308
15 decimal places
long double
10 byte
3.4E-4932 to 1.1E+4932
19 decimal places
Objective-C void type
no value is available
-function returns as void void exit(Int status);
-function arguments as void int rand(void);
Objective-C Variables
- name given to a storage area
- name can be composed of letters, digits and the underscore.
- name must begin with either a letter or underscore
- char
- -typically one byte
- -integer type
-Int
- float
- -single-precision floating point value
- double
- -double precision floating point value
- void
- -represents the absence of type
- other types
- -enumeration, pointer, array, structure, union
int i, j, k;
char c, ch;
float f, salary;
double d;
variables can be initialized in their declaration:
extern int d = 3, f = 5; // declaration of d and f.
int d = 3, f = 5; // definition and initializing d and f.
byte z = 22; // definition and initializes z.
char x = ‘x’; // the variable x has the value ‘x’.
variable declaration provides assurance to the compiler that there is one variable existing with the given type and name. Uses the keyword extern.
extern int a, b;
extern int c;
extern float f;
Objective-C Lvalues and Rvalues
- lvalue
- -expression that refers to a memory location
- -may appear as either the left-hand or right-hand side of an assignment
- -variables are lvalues
- rvalue
- -data value that is stored at some address in memory
- -expression that cannot have a value assigned to it which means that an rvalue may appear on the right-hand but not the left-hand side of an assignment
- -numeric literals are rvalues
Objective-C Constants
- fixed values that may not be altered during program execution
- also called literals
- type can be integer, floating, character, string or enumeration
- integer literal can be a decimal, octal or hexadecimal constant
- floating point literals have an integer part, decimal part, fractional part and exponent part
- character constants are enclosed in single quotes
- string literals are enclosed in double quotes
- constants can be defined
- -using #define preprocessor
- -using const keyword
#define LENGTH 10 #define WIDTH 5 #define NEWLINE '\n'
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = ‘\n’;
Objective-C Operators
-arithmetic \+ - * / % \++ --
-relational == != > < >= <=
-logical
&&
||
|
-bitwise & | ^ ~ << >>
-assignment = \+= -= *= /= %= <<= >>= &= ^= |=
-misc
sizeof()
–returns the size of a variable
&
–returns the address of a variable
*
–pointer to a variable
?: --conditional expression if condition is true ? then value x: otherwise value y
Objective-C Loops
while loop
for loop
do..while loop
nested loops
loop control statements:
- -break statement terminates loop
- -continue statement skips remainder of body