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
}
import
import says to import or include the information from that file into the program, exactly as if the contents of the file were typed into the program at that point.
main
main is a special name that indicates precisely where the program is to begin execution. The reserved word int that precedes main specifies the type of value main returns, which is an integer
int main (int argc, const char * argv[])
newline character
\n
is very similar in concept to the carriage return key on a typewriter
Choosing Names
- must begin with a letter or underscore ( _ ), and they can be followed by any combination of letters (uppercase or lowercase), underscores, or the digits 0 through 9
- class names begin with an uppercase letter, even though it’s not required
- To aid readability, capital letters are used inside names to indicate the start of a new word
The @interface Section
When you define a new class, you have to tell the Objective-C compiler where the class came from. That is, you have to name its parent class. Next, you need to define the type of operations, or methods, that can be used when working with objects from this class. And, as you learn in a later chapter, you also list items known as properties in this special section of the program called the @interface section.
@interface NewClassName: ParentClassName
propertyAndMethodDeclarations;
@end
The @implementation Section
the @implementation section contains the actual code for the methods you declared in the @interface section. You have to specify what type of data is to be stored in the objects of this class. That is, you have to describe the data that members of the class will contain. These members are called the instance variables. Just as a point of terminology, you say that you declare the methods in the @interface section and that you define them (that is, give the actual code) in the @implementation section.
@implementation NewClassName { memberDeclarations; } methodDefinitions ; @end