Objective C Flashcards
Class
for representing objects It's defined by 2 files: class.h (=interface file) a class.m (=implementation file)
To represent an object in Objective C, we first need a Class.
OOP
Object Oriented Programming
Variable
most basic yet important concept in programming
int x = 10;
If the variable consists of two words, it should start with a lower case for the first word and a upper case for the second
Variable types
integer int x = 1;
char grade = ‘ ‘;
NSString *carBrand = @”Toyota”;
NSString variable data type
NextStep string
variable must have asterix (*) before name
NSString *name = @”John”
NSStrings are one of the most useful and commonly used data type in programming.
Constructors
- (id)initWithName:(NSString *)newName gender:(NSString *)newGender age:(int) newAge;
objeví se v souboru *.m
Methods
The functions we have introduced earlier, i.e. getStudentCategory, initWithName are know as methods. A method is a block of code that performs a specific task.
Method Arguments
A method can have no arguments like in the case of
- (NSString *)getStudentCategory;
A method can also have arguments like in the case of
- (id)initWithName:(NSString *)newName gender:(NSString *)newGender age:(int) newAge;
Note that the data type for each argument must be declared (in parenthesis). - instance method \+ class method: A class method is one that performs some operation on the class itself, such as creating a new instance of the class
Interface/Implementation
each method implemented in the *.m file, there should be a header declaration in the .h file
@interface class_name: NSObject { attributes}
@property …vlastnosti_atributu..
…
@end
Return Type of a method
The return type is the data type returned from the method. For example, the return type in the below method is NSString.
- (NSString *)getStudentCategory;
The return type below is id.
- (id)initWithName:(NSString *)newName gender:(NSString *)newGender age:(int) newAge;
If the method does not return any value, the return type is void like in the below example.
- (void)dealloc
Managing Multiple Objects
We can store objects in a NSMutableDictonary. It allows us easy access to: a) insert new objects, b) delete existing objects, c) update existing objects d) and view existing objects
Data in a NSMutableDictionary is stored in key-value pairs.
Summary of NSMutableDictionary Operations
Hence in short, we store an object into the NSMutableDictionary using
setObject: forKey:
eg. [studentStore setObject:jane forKey:@”123”];
and retrieve it with objectForKey:
”“eg. Student *temp = [studentStore objectForKey:@”123”];
To remove an object, we have to use the removeObjectForKey method.
If-Else Statements
if (score >= 50)
{NSLog(@”Passed”);}
else
{NSLog(@”failed”);}
Format specifiers In NSLog
Many format specifiers work with NSLog. If we have a floating-point number, we can use %f to print out a floating-point number, decimal point and all. Other format specifiers are: %i - int (same as %d) %lf - double %c - char %s - string (also %@ - string) %x - hexadecimal
Evaluating conditions
Evaluating Conditions
Other evaluation conditions can also be equal to, == not equal to, != more than, > more than or equal to >= less than, < less than or equal to, 69 && score < 80) NSLog(@”B grade”);
Eg. if score is less than 50 or absentWithoutExcuse is true
if (score < 50 || absentWithoutExcuse)
NSLog(@”Fail”);
Note that absentWithoutExcuse in this case is a boolean variable. A boolean value has only YES (true) or NO (false) values
Nested If-Else
In a If-Else block, there can be further nested If-Else blocks!
There can in fact be infinite nested If-Else blocks though a maximum of 4 levels is recommended as this makes your code unreadable.
If-Else-If
You can also extend your If-Else to If-Else-If-Else-If-Else…
Again, you can have infinite If-Else-If…
NSString Comparisons
To compare two NSStrings, we can use the isEqualToString method of the NSString class.
NSString Length
To check the length of a NSString, we use the length method of the NSString class
“NSString rangeOfString
To check if a certain text exists in a long NSString, we can use the rangeOfString.
To check if a certain text exists in a long NSString, and not consider the case, (eg. ABC is the same as abc), we add the options:NSCaseInsensitiveSearch argument.
Common filename extensions
Extension * Meaning .c C *language source file .cc, .cpp *C++ language source file .h *Header file .m *Objective-C source file .mm *Objective-C++ source file .pl *Perl source file .o *Object (compiled) file
Autorelease pool
autorelease pool is a mechanism that allows the system to efficiently manage the memory your application uses as it creates new objects.
@autoreleasepool {
statements
}