ObjectiveC Interview Questions and Answers
What is Objective-C?
- Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is used primarily for developing applications for Apple's iOS and macOS operating systems.
What are the differences between C and Objective-C?
- Objective-C is a superset of C, which means it includes all features of C but also adds object-oriented capabilities. While C is a procedural language, Objective-C introduces classes, objects, inheritance, and messaging.
What is a class in Objective-C?
- A class in Objective-C is a blueprint for creating objects (instances). It defines the properties (attributes) and methods (functions) that the objects of that class will have.
What is an object in Objective-C?
- An object is an instance of a class. It contains data (properties) and methods to manipulate that data. Objects are created by calling the alloc and init methods on a class.
What is the purpose of the @interface and @implementation keywords in Objective-C?
- @interface defines the interface of a class, which includes the properties and method declarations. @implementation provides the actual code for the methods declared in the interface.
What is the use of the @property keyword in Objective-C?
- The @property keyword is used to declare a property in a class. It automatically generates getter and setter methods for the property and ensures memory management rules like strong, weak, copy, etc.
What is the difference between a strong and a weak reference?
- A strong reference means the object is retained in memory, preventing it from being deallocated. A weak reference does not retain the object and allows it to be deallocated if no strong references exist.
What are categories in Objective-C?
- Categories allow you to add new methods to existing classes without subclassing them. They are useful for adding functionality to classes that you cannot modify directly (like system classes).
What are protocols in Objective-C?
- Protocols in Objective-C are similar to interfaces in other languages. They define a set of methods that can be adopted by classes. A class that conforms to a protocol must implement the methods defined in that protocol.
What is the difference between a class and a category in Objective-C?
- A class defines the main structure of an object, while a category allows you to add methods to an existing class. Categories cannot add instance variables, but they can add methods and properties.
What is an IBOutlet in Objective-C?
- IBOutlet is used to mark properties of a class that will be connected to a user interface element in Interface Builder. It establishes a connection between the code and the UI elements.
What is an IBAction in Objective-C?
- IBAction is used to mark methods that can be triggered by user interactions with the UI elements, like button taps. It is typically connected to UI elements in Interface Builder.
What is the difference between copy and retain in Objective-C?
- Both `copy` and `retain` are used to manage memory. `retain` keeps a strong reference to the object, while `copy` creates a new object if the original object is mutable. This ensures that the object is immutable.
What is the purpose of the @synthesize keyword in Objective-C?
- The @synthesize keyword is used to automatically generate getter and setter methods for the properties in a class. However, in modern Objective-C (after Xcode 4.4), this is automatically done, so @synthesize is often not required.
What is a Singleton in Objective-C?
- A Singleton is a design pattern that ensures a class has only one instance and provides a global point of access to that instance. It is commonly used for managing shared resources.
What is KVC (Key-Value Coding) in Objective-C?
- KVC is a mechanism that allows you to access an object's properties using string keys. It allows you to get or set the values of an object's properties indirectly.
What is KVO (Key-Value Observing) in Objective-C?
- KVO is a mechanism that allows an object to be notified when a property of another object changes. It is often used for responding to changes in the state of an object.
What are blocks in Objective-C?
- Blocks are self-contained chunks of code that can be passed around and executed later. They are similar to closures in other languages and can capture variables from their surrounding context.
What is the difference between a block and a function in Objective-C?
- Blocks are anonymous, inline code snippets that can capture variables from the surrounding scope, while functions are named, independent pieces of code that cannot capture local variables from the context.
What is the @autoreleasepool in Objective-C?
- @autoreleasepool is used to manage memory efficiently. It creates an autorelease pool where objects can be released at the end of the scope, ensuring proper memory management during a block of code execution.
What is the purpose of the - (instancetype)init method in Objective-C?
- The `init` method is used to initialize an object. It is typically called after alloc to set up the initial state of an object.
What is the difference between `alloc` and `new` in Objective-C?
- Both `alloc` and `new` are used to create new instances of a class, but `new` is shorthand for calling `alloc` and `init` together, while `alloc` just creates the object without initializing it.
What is the purpose of the @selector keyword in Objective-C?
- The @selector keyword is used to refer to the name of a method. It is commonly used in messaging, where you specify the method that you want to call.
What is the difference between `- (void)methodName` and `- (id)methodName` in Objective-C?
- - (void)methodName means the method does not return a value. - (id)methodName means the method returns a value of type id, which can be any object.
What is the difference between `@interface` and `@implementation`?
- @interface defines the properties and methods of a class. @implementation provides the code for the methods declared in the @interface section.
What is ARC (Automatic Reference Counting) in Objective-C?
- ARC is a memory management feature in Objective-C that automatically manages the reference count of objects. It ensures that objects are automatically retained and released when needed, reducing the chances of memory leaks and crashes.
What is the difference between a retained property and a weak property in Objective-C?
- A retained property holds a strong reference to an object, while a weak property does not retain an object. Weak references do not prevent an object from being deallocated.
What is `didReceiveMemoryWarning` method in Objective-C?
- `didReceiveMemoryWarning` is a method in the UIViewController class that is called when the application receives a memory warning. It provides an opportunity to release any resources that can be recreated later.
What is the purpose of `NSCoding` protocol in Objective-C?
- NSCoding is a protocol that allows objects to be encoded and decoded for storage or transmission. Objects that conform to NSCoding can be serialized and deserialized.
What is the use of `NSURLConnection` in Objective-C?
- NSURLConnection is a class used for making asynchronous HTTP requests to retrieve data from a URL. It handles data requests and responses in a background thread.
What is the purpose of `NSNotification` in Objective-C?
- NSNotification is used to send notifications across the app. It allows objects to communicate with each other without needing direct references to one another. The notifications can be broadcast and observed by any object.
What is `NSError` in Objective-C?
- NSError is an object that describes errors in the application. It provides information about the error code, localized description, and any additional context that can help with error handling.
What is the difference between `retain` and `assign` in Objective-C?
- `retain` creates a strong reference to an object, while `assign` does not retain the object. `assign` is used for primitive types (like int) or non-object references.
What is `dispatch_async` in Objective-C?
- `dispatch_async` is used to execute a block of code asynchronously on a background queue. It is typically used to offload work to a background thread to avoid blocking the main thread.
What is the `@autoreleasepool` block in Objective-C?
- `@autoreleasepool` is a memory management construct that creates an autorelease pool to release objects automatically when they are no longer needed.
What is `NSFileManager` in Objective-C?
- `NSFileManager` is a class that provides methods for managing the file system, including creating, reading, writing, and deleting files and directories.
What is `NSNotificationCenter` in Objective-C?
- `NSNotificationCenter` is used to post notifications and register observers for those notifications. It enables communication between objects without direct references.
What is the difference between `viewDidLoad` and `viewWillAppear`?
- `viewDidLoad` is called after the view controller's view is loaded into memory, while `viewWillAppear` is called just before the view becomes visible to the user. The former is used for initial setup, while the latter is used for updating the view.