Fundamentals Flashcards
Learn fundamentals of Objective-C
int
storing integers - %i or %d
char
storing characters - %c
float
storing numbers with decimals - %f
double
same as float but double accuracy - %e
Counting
Always start with zero
Conditionals
For making a decision
==
equality operator check
=
For assigning the first value to the second
x++
Increase by 1 for loops
While
Like “for” but conditional runs after block of code
Pointer
Point to a location (for computer memory)
int* foo = 123
OOP
Variable and Functions trade for Objects and attributes
Objects
Are instance of a class
Instant Method
One object in a class i.e. - (void) addGas;
Class Method
All objects in class i.e. All Cars
Method syntax
[recipient message];
who method
Nested ex. [NSString alloc] init];
best practice never do more than 2
Accessor Methoods
Get data
Setter Methods
Update attributes of objects - no need to return a value
- (void) setCaption: (NSString*)input;
Data Encapsulation
Data is contained by methods in classes
Classes Contain 2 Files
Implementation (.m) and Interface (.h)
Interface File (.h)
Defines instance variables and public methods
Implementation File (.m)
Actual code for methods in interface file including private methods
NS
Next Step
NSString
String text that is immuatable
NSMutableString
String that is mutable
NSArray
Array of objects that is immutable
NSMutableArray
Array of objects that is mutable
NSNumber
Holds numeric value
Inheritance
All classes inherit from parent class (ex. methods)
@property (weak)
Attributes is used as a reference. Attribute is not owned by the object
@property (strong)
Attribute is owned by the object and will be save in memory
ARC
Automatic Reference Counting used to make memory counting easier
NSArray
Holds a list of objects
NSDictionary
Holds a list of key/value object pairs
@
Common prefix for all Objective-C literals
Literals
NSString, NSArray, NSDictionary, etc.
Nil
Prints a null meaning no value
Value of an object pointer when it isn’t pointing to anything
*
Pointer to where information can be found
%p
Prints output
id
Generic container to put in any type of object (string, array, etc,)
No need for * because id is a pointer by definition
selector
SEL object to call during runtime - how you send in methods names as parameters
SEL firstNameSelector = @selector(firstName);
NSLog(@”[person firstName] = %a”, [person performSelector:@selector(firstName)]);
enums
list of predefined values / constants
structs
structure group of variables
unsigned int
integers with no negatives %u
void
Return nothing
dot syntax
Only for “getter” & “setter” methods (accessor)
method with input
method:input
Class Method
+ (NSString*) caption;
dealloc
Called on an object to remove it from memory
Use finalize method with garbage on
reference counting
alloc an object, retain an object, you should release for each occurrence
2 Reason to Create an Object
- To keep it as an instance variable
2. To use temporarily for single use inside a function
Memory Management inside a function
If you create an object with alloc or copy, send it a release or autorelease. Any other way, do nothing.
retain
For @property to retain input value
@synthesize
Automatically generates the setter and getters for us in Implementation file
@synthesize caption;
self.<var></var>
Syntax for setter method which includes memory management
categories
Allows you to add methods to an existing class without subclassing it or needing to know further details. Utilities is Category.
@interface NSString (Utilities)
- (BOOL) isURL;
@end
new line
/n
Precedence
Parenthesis, exponents, multiplication, division, add, subtract
Modulus
The remainder %
int m = 38 % 7;
return 3
new line
/n
@class
used when we only want to declare the object of any class..
@interface
Creates a class @interface Fraction: NSObject { int numerator; int denominator; }