Type inference, operator overloading, protocol extensions, string interpolation, namespaces, tuples, optionals, playgrounds, guard, defer, enums with associated values
Back
When do we use NSArray and NSMutableArray?
Front
"NSArray creates static arrays that cannot be mutated. NSMutableArray creates arrays that can be mutated. "
Back
Why are Objective-C based classes often prefixed with letters like XCTestCase, MKMapView?
Front
"Objective-C has no namespacing. Where Swift allows two frameworks to declare classes with the same name, as they can be namespaced such as Framework1.MyClass and Framework2.MyClass, Objective-C cannot achieve this. This means identifiers have to be globally unique. Hence frequent prefixed letters."
Back
What is a global autoreleasepool?
Front
At any time a global autoreleasepool may pick up pointers when, say, receiving objects from methods but not allocating or creating. The global release pool can be drained from time to time. In Cocoa, it's after an NSEvent such as tap. When the release pool drains, references drop which can cause deallocation.
Back
Explain types of protocol.
Front
"Formal protocols act like Swift protocols except you can have optional methods. Otherwise a class must adopt the necessary methods. And subclasses inherit the protocol conformance. Informal protocols are categories on NSObject, which means that most Objects adopt them. Callers of informal protocol methods check that the instance implements it. "
Back
What does @ mean?
Front
It means treat everything coming up as Objective-C not C code. As it's not used in C or C++ as an identifier it's a way to strictly use Objective-C constructs instead, such as using Objective-C string type not C.
Back
What is the responder chain?
Front
"The responder chain is the chain of responders listening for UI events. For example, if you tap a UITextField it becomes the first responder. UIKit dynamically handles events and passes them to the first responder, if the first responder doesn't handle the event it passes it to the next responder. The predefined rules of UIKit tends to forward events from one view to its superview. "
Back
What happens before Objective-C code is built?
Front
Pre-processing. The pre-processer is used for header files and macros (which act like Swift constants but can be more complicated)
Back
What is the use of category in Objective-C?
Front
"A category is like a Swift extension. It extends the functionality of an existing class and you can add your own methods to it. Methods are inherited by subclasses and at like original methods of the class. They can be used to divide code clearly. The disadvantage is you cannot override methods from the original class or other categories. "
Back
What is KVC and KVO? Give an example of using KVC to set value.
Front
"KVC - Key Value Coding allows you to access the property of an object using a specific string. In Objective-C you can use valueForKeyPath to get properties. KVO - Key Value Observing is observing the properties of a given object and responding to those changes. In Objective-C you use addObserver forKeyPath to obsere properties. "
Back
Does Objective-C have function overloading?
Front
No
Back
Explain the difference between atomic and nonatomic synthesized properties.
Front
"Atomic is default Atomic is thread-safe. It means the synthesised getters and setters of a property guarantee a whole value will be returned, i.e. a thread calling a getter while another thread calls the setter, will wait for the set to finish. Nonatomic is not thread-safe but quicker. It means you could get garbage data returned. Ok to use if you can guarantee single thread acess."
Back
What is atomic and non-atomic in Objective C and which one is considered to be a default?
Front
"Atomic is default Atomic is thread-safe. It means the synthesised getters and setters of a property guarantee a whole value will be returned, i.e. a thread calling a getter while another thread calls the setter, will wait for the set to finish. Nonatomic is not thread-safe but quicker. It means you could get garbage data returned. Ok to use if you can guarantee single thread acess."
Back
What should you not do when managing memory?
Front
Dealloc explicitly, as deallocation should be managed by dropping reference count. If you dealloc manually you may end up with dangling pointers
Back
What does dispatch_once do? What is it used for?
Front
It guarantees that a block will only be executed once in the lifetime of the app. It is useful for initialisation of global create singletons in app.
Back
What are declared properties in Objective-C?
Front
"Declared properties are properties declared using the @property identifier. They are properties with getter and setter methods already implemented under the hood. You can set attritibutes such as read or write only, atomic or nonatomic, and strong, weak, retain, assign, copy"
Back
Explain what is @synthesize in Objective-C
Front
"Synthesize is used against properties in Objective-C in order to get automatic getter and setter for the underlying instance variable. If you declare a property in Objective-C Xcode automatically provides a instance varable with the same but a preceding _. For example, @property NString *name gives a property name, getter and setter to the underlying ivar that has the name ""_name"" Adding synthesize is unecssary unless you need custom getters and setters to your instance variable"
Back
Explain when to use NSArray and NSMutableArray. Which one is faster and threadsafe?
Front
"NSArray creates static arrays that cannot be mutated NSMutableArray creates arrays that can be mutated. It is not thread-safe and you have to implement a lock to get thread-safety. Speed is about the same however NSArray has fixed memory size whereas mutable array can change and take more memory. "
Back
What are blocks and how are they used?
Front
Blocks are the equivalent of Swift closures. They are independent blocks of code that can be executed and passed around. They are a first step to asynchronous programming in that you can pass a block of code to an operation that may happen asynchronously, then when that ends the block will be executed.
Back
What is the difference between #import and #include in Objective-C
Front
Import makes sure a file is only imported once, whereas include can include files multiple times.
Back
What is the difference when importing between quotation marks and angular brackets?
Front
"Quotation marks search you local files. Angular brackets search the system files."
Back
Hoe does message work in Objective C?
Front
Message is like method in Swift. You send messages not call methods.
Back
What happens to optionals in Objective-C?
Front
There is no concept of optionals in Objective-C. If you call a nil, nothing happens, so it's worthing checking things aren't null.
Back
How do you manage memory in Objective-C?
Front
"You can enable Automatic Reference Counting, just like Swift. Alternatively you manage it manually, in which case you must follow some rules. When creating you can alloc, copy or new - to create memory for the this object and increment it's reference count. When retain something you increment it's reference count. If you own references you must release them when finished with them.
Back
Mention whether NSObject is a parent class or derived class.
Front
"NSObject is a root object from which all other classes should inherit. Other objects expect to have acces to basic NSObject functionality on other objects, such as init, copy, or description. "
Back
Variables are _ by default and require the keyword _ to be constant?
Front
variable, const, constant
Back
What does @autoreleasepool mean?
Front
I'm going to be allocating lots of memory and when I am done please free it up.
Back
What is instanceType?
Front
Instancetype is a keyword that means `an instance of this class will be returned`. It was introduced to plug problems where initialisers were returning `id`, which is a general pointer to any swift object, that can break type safety. As the compiler won't warn about id being not the type you expected.
Back
What does * do?
Front
It indicates that a thing is a pointer to something in memory
Back
Switches differ to swift in what way?
Front
They have implicit fallthrough meaning you need to break everytime
Back
What is fast enumeration?
Front
It's pretty much a for loop in swift, where you loop through sequence and do things on each element.
Back
What would %@ do in an string in Objective-C?
Front
It is a format specifier. It takes an object and passes into that string to be shown, logged or printed. Remember Objective-C has no String Interpolation?
Back
Is it possible to use ARC and Non-ARC code together in a project?
Front
Yes, you can say to Xcode use ARC, but there is a flag you can set for particular classes or segments of code where you want to manage your own memory
Back
What is the purpose of the .h file in Objective-C?
Front
"This is the Header file. When creating classes in Objective-C you create both a header file and an implementation file. The header file exposes the interface. The implementation file contains the code. This means people can ship header files with `.a` files that contain the actual compiled code, without sharing implementation"
Back
Explain what is #import.
Front
Ensures a file is only ever imported once so you don't have a probeem with recursive imports
Back
What is id?
Front
"id is a pointer to any object in Objective-C. It was originally used to be the data type returned when initialising certain data types like dictionaries and arrays. You can call anything on it, string methods, array methods etc. and the compiler won't mind because of this. Which breaks type safety. Id was also the return type for some initialisers. InstanceType came to replace this so that initialisers could return their actual class type rather than id. "
Back
Can you write setter method for a retain property?
Front
Yes, if you want to write your custom setting logic.
Back
Explain how to call function in Objective-C.
Front
"Square bracket, Class, method name, (plus parameters if needed) close square bracket. Can use self in place of class to call own instance methods"
Back
Is Objective C, a dynamic language? True/False, explain.
Front
"It is dynamic. It is based on sending messages not calling methods. At runtime, the dispatcher sends and messages and only needs to know if the object responded. This means you can inject methods and swizzle methods at runtime. In contrast, Swift is static and compiler needs to know before build what objects can do what methods. @Objc provides a way to gain the dynamism. "
Back
What safety risks does Objective-C present?
Front
"No concept of optionals. Can force one data type into another. Can read array values that don't exist. Switches don't have to be exhaustive. Most things are variables rather than constants"
Back
What are the characteristics of a category?
Front
"A category is like a Swift extension. It extends the functionality of an existing class and you can add your own methods to it. Methods are inherited by subclasses and at like original methods of the class. They can be used to divide code clearly. The disadvantage is you cannot override methods from the original class or other categories. "
Back
Explain what is protocol in Objective-C.
Front
"Protocols are contracts that classes must conform to if they want to implement them. They allow us to abstract the implementation to detail and just worry about the interface. We can swap underlying instances while only exposing the protocol. Classes can conform to multiple protocols. Good for mocking"
Back
What are NSAutoreleasePool? When to use them?
Front
"An autorelease pool allows you to add objects to the pool and mark them as autorelease. There reference count is 1 until the pool is drained such as at the end of the runloop for the pool. They can be used to keep objects in memory while you may setup other objects to retain those objects."
Back
Why is explicitly checking reference count bad?
Front
Because autoreleasepool references don't count
Back
What is delegate? Can delegates be retained?
Front
"Delegates are objects that act on behalf or in coordination with another object when an event happens. For example, a UITextField has a delegate, it tells the delegate if text has started editing or finished editing so that it can do something. Delgates can be retained but typically aren't and are declared as weak to avoid strong reference cycles. Reason being often, object a owns object b, and object b delegates back to object a. So if they strongly referenced each other they'd never be release from memory. "
Back
What does alloc mean?
Front
It means allocate some memory for something
Back
What's the difference between #import <UIKit/UIKit.h> and #import "MyClass.h"?
Front
"The first says search system libraries and import this. The second says search system libraries and my own project and import this. "
Back
Does a thread created using performSelectorInBackground:withObject: creates its own autorelease pool?
Front
No, if you call performSelectorInBackground you spawn your own thread that you have to manage and configure. Such as creating your own autoreleasepool
Back
Explain what dot notation is in Objective-C
Front
"It is syntatic sugar for calling getters and setters of properties. For example, a class Person with property Name could be grabbed with person.name"
Back
What does # mean?
Front
It's a preprocesser directive, telling the preprocessor to do something. For example, it's used to import required headers such as Apple's Foundation
Back
Section 2
(22 cards)
How do you swizzle in Objective-C?
Front
You can use getInstanceMethod on class to get it's instance method than use the C syntax -> to replace that method for another
Back
Who calls dealloc method? Can we implement dealloc in ARC? If yes, what is the need to do that?
Front
"Dealloc is called by the runtime to deallocate your objects when the reference count drops to zero and memory is requited. We can override dealloc if we want to. We may do this if we want to also stop our class observing some notification for example, to prevent a memory leak. "
Back
What does @synchronized() do?
Front
It protects a block of code or object by implementing a lock. It makes that part thread-safe.
Back
What is an extension?
Front
It is a category without a name, also known as a anonymous category. The compiler will complain if you don't implement methods declared in an extension.
Back
What happens if you add your just created object to a mutable array, and you release your object?
Front
The mutable array becomes responsible for owning that object.
Back
Are id and instanceType same? If not, what are differences between them?
Front
"They are different as id is generic pointer to any object, where as instancetype tells the compiler an instance type will be returned. When used for a initialisers the compiler automatically gives you instancetype even if you say id, but this is not the case for methods. So you need to be sure when you want to return id or instance type in that case. "
Back
What are the CPU architectures supported by iOS devices?
Front
arm64 since iPhone 5s
Back
What is posing in Objective C?
Front
"Posing is when one class poses as another class. All messages sent to the first class are actually sent to the poser. Use the poseAs keyword"
Back
What happens when we invoke a method on a nil pointer?
Front
A message sent to a nil object is perfectly acceptable in Objective-C, it's treated as a no-op. There is no way to flag it as an error because it's not an error, in fact it can be a very useful feature of the language. It returns 0, nil, a structure filled with 0s, etc.
Back
Can we create Dynamic Classes in Objective C? If Yes, Explain how to create with a valid use case?
Front
Yes using NSClassFromString
Back
What happen if we send a message to an object which is released?
Front
Depends, if the object is deallocated then you can run into trouble as your pointer points at nothing. Message may work if object remains.
Back
What is the isa member?
Front
It is a pointer to the class that an object is a type of.
Back
What is toll-free bridging and when is it useful?
Front
Toll free bridging is when you can use two data types interchangeably, such as CFArray and NSArray?
Back
How to make a code snippet thread safe?
Front
The simplest way would be to use the @synchronized(Object) around an Object or snippet.
Back
What can't we put into an array or dictionary?
Front
nil
Back
What is the difference between underscore and self (i.e self.xx and _xx) ?
Front
Self.xx acesses a property through its getter and setter, _xx accesses the underlying variable / property directly
Back
What is the difference between retain, assign, copy, strong and weak?
Front
"Retain says the class should own the property, it should be released in the dealloc Copy says the class owns the property but it gets a copy of the property when the setter is called, the old value will be released, it should released in dealloc Assign is used by default, it does not increase reference count and is used for simple data types, it should be set to nil in dealloc Strong and weak work like self when ARC is used and retain becomes redundant."
Back
What is a process, thread?
Front
Both processes and threads are independent sequences of execution. The typical difference is that threads (of the same process) run in a shared memory space, while processes run in separate memory spaces.
Back
Reflection? Class Introspection? Is there anything in objc and cocoa?
Front
"Class introspection is the ability to find out information about classes or objects at runtime Reflection is the ability to swap, add or remove variables, methods and classes at runtime. Swizzling takes advantage of class introspection and reflection to swap method implementations at runtime"
Back
What happens to objects in an array when it is released?
Front
If the objects in the array are not referenced / retained by any other object, then they are also release.
Back
What is class extension? Why do we require them?
Front
"A class extension is similar to a category, but it requires you to have source code for the given class. You cannot class extend Foundation classes for example. A class extension basically extends your classes functionality. It is compiled at the same time as your class. You must implement your class extension methods and properties in the implementation of your class."
Back
When might you use a CFArray/Dictionary instead of a NSArray/Dictionary?
Front
CF means Core Foundation. They are C implementations. NS are bridges of Objective-C to the C. So an NSArray and CFArray are virtually interchangeable. We call this toll-free bridging.