What is the purpose of the @dynamic directive?

The @dynamic directive is used to tell the compiler that the setters and getters for a property are implemented not by the compiler, but at runtime. This is useful when you are using a library such as Core Data, which dynamically creates the getters and setters for you.

For example, if you have a property called “name” in your class, you could use the @dynamic directive to tell the compiler that the getter and setter for the property will be handled by Core Data at runtime:

@dynamic name;

What is the purpose of the @synthesize directive?

The @synthesize directive is used to generate getter and setter methods for a property in Objective-C. It is used to create the backing instance variable for a property and implements the getter and setter methods for that variable.

Synthesize example:

@synthesize name = _name;

This will create a backing instance variable with the name _name and will generate getter and setter methods for the property name.

How do you create a singleton class in Objective-C?

A singleton class is a class that can only have one instance of the class at any given time.

You can create a singleton class in Objective-C by using the following template:

// MySingleton.h

#import

@interface MySingleton : NSObject

+ (instancetype)sharedInstance;

@end

// MySingleton.m

#import “MySingleton.h”

@implementation MySingleton

+ (instancetype)sharedInstance {
static MySingleton *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[MySingleton alloc] init];
});
return sharedInstance;
}

@end

What is the difference between a protocol and a delegate?

Protocol: A protocol is a set of rules or guidelines for communication between two or more entities. For example, the Hypertext Transfer Protocol (HTTP) is a protocol used to communicate between web servers and web browsers.

Delegate: A delegate is an object in Objective-C that is used to pass data between two objects. For example, a delegate can be used to pass data from a view controller to a model object. The view controller can use the delegate object to pass data to the model object, and the model object can use the delegate object to pass data back to the view controller.

What is the difference between a category and a class extension?

A category is a way of adding additional methods to an existing class, without having to subclass it. For example, you could add a category to the NSString class to add a method that converts a string to a URL.

A class extension is a way of adding additional private methods and properties to an existing class. For example, you could add a class extension to the UIViewController class to add a private method that handles the loading of view data from a remote server.

What is the difference between a class method and an instance method?

A class method is a method that is defined in a class and can be called directly from the class without having to create an instance of the class first. An example of a class method is the __init__() method in Python.

An instance method is a method that is defined in a class and can only be called on an instance of the class. An example of an instance method is the __str__() method in Python.

What is the difference between an instance variable and a property?

An instance variable is a variable that is declared inside a class and is accessible from any method within that class. An example of an instance variable would be:

class Person {
private String name;

public Person(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

A property is a special type of variable that is used to access a class’s member variables from outside the class. An example of a property would be:

class Person {
private String name;

public Person(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

What is the difference between a protocol and a category?

A protocol is a set of rules that govern how computers communicate with each other. For example, the Transmission Control Protocol (TCP) is a protocol that allows two computers to communicate with each other over the internet.

A category is a way of organizing or grouping related items. For example, a website might have a category for books, and each book would be listed under that category.