iOS Flashcards
Difference between messaging and function calling?
In the messaging structure (which evolved from Smalltalk, the origin of messaging) the runtime decides which code gets executed. With function calling, the compiler decides.
When Polymorphism is used in function calling, a runtime lookup as a virtual table is used. In messaging the compiler doesnt care about the type of object being messaged, that is looked up at runtime too.
Advantage of messaging: The App’s performance benefits of runtime updates directly, whereas a compile time language has to be recompiled to benefit from updates.
What is the runtime?
The runtime is a dynamic library that contains all the data structures and functions that are required for the object oriented features of Objective-C, like all the memory management methods. The code from the app is linked to that library of the runtime, which does the heavy lifting, instead of the compiler.
Describe Pointers
Obj C uses pointers to denote an object, so variables are holding references to an object.
The syntax: NSString * myString = @”Hello”
The pointers are allocated on the stack, and point to some memory, allocated on the heap, containing an NSString Object. All Objects must be allocated in the heap space, never on the stack.
If another variable points to the same memory, the object is NOT getting copied, rather both variables point to the same object:
NSString * anotherString = myString;
bitwise AND
&
Example: 0010110 & 1 = 0
Shows if the least significant bit is set.
Performs an AND on each bit position between 2
0101 (decimal 5)
AND 0011 (decimal 3)
= 0001 (decimal 1)
The operation may be used to determine whether a particular bit is set (1) or clear (0). For example, given a bit pattern 0011 (decimal 3), to determine whether the second bit is set we use a bitwise AND with a bit pattern containing 1 only in the second bit:
1010 AND
0010 (bitmask)
=
0010
Because the result 0010 is non-zero, we know the second bit in the original pattern was set. This is often called bit masking.
If we store the result, this may be used to clear selected bits in a register. Given the example 0110 (decimal 6), the second bit may be cleared by using a bitwise AND with the pattern that has a zero only in the second bit:
What is a: retain cycle?
Two objects having a strong pointer to each other and preventing the retain count ever reaching 0. This is a form of memory leak. It gets fixed by using weak references on the non-owning relationship. The weak attribute will set the property to nil if the reference gets destroyed.
Code for binary search:
binary_search(A, target):
lo = 1, hi = size(A)
while lo
What is Cocoa, what are its Frameworks?
Cocoa is an application environment, containing object oriented frameworks, a runtime system and a development environment. Frameworks in Cocoa are:
- Foundation
- AppKit
Describe Objective C
Objective C is a object oriented language, based on C. It is more dynamic than other languages, shifting the responsibility of symbol resolution from compile time to runtime, which gives the user more control.
Its Dynamism consists of:
· Dynamic typing—determining the class of an object at runtime
· Dynamic binding—determining the method to invoke at runtime
· Dynamic loading—adding new modules to a program at runtime
Differences between Obj-C and C++/C
· The Objective-C class allows a method and a variable with the exact same name. In C++, they must be different.
· Objective-C does not have a constructor or destructor. Instead it has init and dealloc methods, which must be called explicitly.
· Objective-C uses + and - to differentiate between class method (known as factory method in Java) and instance methods, C++ uses static to specify a factory method.
· Multiple inheritance is not allowed in Obj-C, however we can use protocol to some extent.
· Obj-C has runtime binding leading to dynamic linking.
· Obj-C has categories.
· Objective-C has a work-around for method overloading, but none for operator overloading. In Objective-C the message overloading is faked by naming the parameters. C++ actually does the same thing but the compiler does the name mangling for us. In Objective-C, we have to mangle the names manually. If you go in deep you will see a complete method names are as : addA:withB:
which is a selector and :
is used for parameter.
· One of C++’s advantages and disadvantages is automatic type coercion.
· Objective-C also does not allow stack based objects. Each object must be a pointer to a block of memory.
· Another feature C++ has that is missing in Objective-C is references. Because pointers can be used wherever a reference is used, there isn’t much need for references in general.
· Templates are another feature that C++ has that Objective-C doesn’t. Templates are needed because C++ has strong typing and static binding that prevent generic classes, such as List and Array.
AppKit
AppKit is a framework that contains all UI elements that are typically used to build an application interface, like buttons, scrollers, textfields, lists, windows, menus, panels. It is already optimized to clip on screen, draw efficiently, clear graphic buffers, respond to events and contains a default visualization.
It can either be setup visually by using Interface Builder, programmatically or subclassed for custom UI.
Foundation
Foundation is a base layer of Objective-C classes, that provides primitive classes and datastructures, a small set of utility classes, introduces consistent conventions for things such as deallocation, supports Unicode strings, object persistence, and object distribution, and provides a level of OS independence, to enhance portability.
Dynamic vs Static Typing
Languages that use static typing declare their objects' type at compile time, whereas languages like Obj-C with dynamic typing resolve their objects' class type at runtime. Declaring a type for a variable is only used for a build time error check but can be broken at runtime and also just declared as "id" which leaves the type open.
Describe Selectors
Selectors 1) refer to the name of a method when it’s used in a source-code message to an object.
2) It also refers to the unique identifier that replaces the name when the source code is compiled. Compiled selectors are of type SEL. All methods with the same name have the same selector.
You can use a selector to invoke a method on an object—this provides the basis for the implementation of the target-action design pattern in Cocoa.
[friend performSelector:@selector(gossipAbout:) withObject:aNeighbor];
is equivalent to:
[friend gossipAbout:aNeighbor];
Class introspection
· Determine whether an objective-C object is an instance of a class
[obj isMemberOfClass:someClass];
· Determine whether an objective-C object is an instance of a class or its descendants
[obj isKindOfClass:someClass];
· The version of a class
[MyString version]
· Find the class of an Objective-C object
Class c = [obj1 class]; Class c = [NSString class];
· Verify 2 Objective-C objects are of the same class
[obj1 class] == [obj2 class]
What is a Category?
import “Car+Maintenance.h”
A category allows you to add methods to an existing class without subclassing or changing the actual object —even to one for which you do not have the source.
They are often used to add methods to existing classes, such as NSString or your own custom objects, and in shared frameworks to preserve base classes and add functionality.
Categories work just like normal class definitions in that they are composed of an interface and an implementation. To add a new category to your Xcode project, create a new file and choose the Objective-C category template under iOS > Cocoa Touch.
The only restriction on category names is that they don’t conflict with other categories on the same class. The canonical file-naming convention is to use the class name and the category name separated by a plus sign, so you should find a Car+Maintenance.h and a Car+Maintenance.m in Xcode’s Project Navigator after saving the above category.
As you can see in Car+Maintenance.h, a category interface looks exactly like a normal interface, except the class name is followed by the category name in parentheses.
// Car+Maintenance.h #import "Car.h"
@interface Car (Maintenance)
At runtime, these methods become part of the Car class. Even though they’re declared in a different file, you will be able to access them as if they were defined in the original Car.h.
Of course, you have to implement the category interface for the above methods to actually do anything.
It’s important to note that a category can also be used to override existing methods in the base class (e.g., the Car class’s drive method), but you should never do this. The problem is that categories are a flat organizational structure. If you override an existing method in Car+Maintenance.m, and then decide you want to change its behavior again with another category, there is no way for Objective-C to know which implementation to use. Subclassing is almost always a better option in such a situation.
Saving properties in categories:
objc_getAssociatedObject and objc_getAssociatedObject require a key to store the object. Such key is required to be a constant void pointer. So in the end we just need a fixed address that stays constant over time.
It turns out that the @selector implementation provides just about what we need, since it uses fixed addresses.
We can therefore just get rid of the key declaration and simply use our property’s selector address.
So if you are associating at runtime a property like
@property (nonatomic, retain) id anAssociatedObject;
we can provide dynamic implementations for its getter/setter that look like
- (void)setAnAssociatedObject:(id)newAssociatedObject {
objc_setAssociatedObject(self, @selector(anAssociatedObject), newAssociatedObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)anAssociatedObject { return objc_getAssociatedObject(self, @selector(anAssociatedObject)); } Very neat and definitely cleaner than defining an extra static variable key for every associated object.
Traditional way:
.h-file
@interface NSObject (LaserUnicorn)
@property (nonatomic, strong) LaserUnicorn *laserUnicorn;
@end
.m-file
static void * LaserUnicornPropertyKey = &LaserUnicornPropertyKey;
@implementation NSObject (LaserUnicorn)
- (LaserUnicorn *)laserUnicorn { return objc_getAssociatedObject(self, LaserUnicornPropertyKey); }
- (void)setLaserUnicorn:(LaserUnicorn *)unicorn {
objc_setAssociatedObject(self, LaserUnicornPropertyKey, unicorn, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
Usage: Just like a normal property - accessible with dot-notation
NSObject *myObject = [NSObject new];
myObject.laserUnicorn = [LaserUnicorn new];
NSLog(@”Laser unicorn: %@”, myObject.laserUnicorn);