Search Your Question

Difference between assign and retain in iOS

In the context of memory management in programming, particularly in Objective-C (though less relevant in modern Swift), "assign" and "retain" are related to the property attributes used in the declaration of object properties.

  1. Assign:

    • In the context of Objective-C, the assign attribute is used for simple assignments and is often associated with primitive types or non-Objective-C objects.
    • When you declare a property with the assign attribute, you're saying that the property will just take the value assigned to it without affecting the reference counting of the object.

    • @property (assign) NSInteger someInteger;

    • For objects, assigning does not increase the retain count, so if the assigned object is deallocated elsewhere, you might end up with a dangling pointer.

  2. Retain:

    • The retain attribute, on the other hand, is used when you want to claim ownership of an object. It increases the retain count of the object, indicating that you want to keep a reference to it.
    @property (retain) NSString *name;

    • When using retain, it's your responsibility to release the object when you're done with it. This is crucial to avoid memory leaks.

    • [name release];

    • In modern Objective-C and Swift, Apple introduced Automatic Reference Counting (ARC), which essentially automates the reference counting process. In Swift, you generally use strong instead of retain, and the memory management is handled by the ARC.

In summary, the key difference is that assign is used for non-object types and doesn't affect the reference counting of objects, while retain (or strong in Swift) is used for objects, and it does increase the retain count, indicating ownership. With ARC in Swift, the need to manually specify assign or retain has diminished, as ARC takes care of many memory management tasks automatically.AI.

Dependency injection in Swift

 Dependency injection (DI) is a design pattern in software development that promotes the separation of concerns by allowing components to depend on abstractions rather than concrete implementations. In Swift, dependency injection is commonly used to achieve loosely coupled and easily testable code.

Dependency injection in Swift

There are three main types of dependency injection:

  1. Constructor Injection: Dependencies are injected through the class's initializer.

  2. Method Injection: Dependencies are injected through methods of the class.

  3. Property Injection: Dependencies are injected through properties of the class.

Let's look at an example using constructor injection in Swift:

// Protocol defining the dependency protocol DataService { func fetchData() -> String } // Concrete implementation of the DataService protocol class RemoteDataService: DataService { func fetchData() -> String { return "Data from remote service" } } // Class that depends on DataService through constructor injection class DataManager { let dataService: DataService init(dataService: DataService) { self.dataService = dataService } func processData() -> String { let data = dataService.fetchData() return "Processed: \(data)" } } // Example of using the DataManager with dependency injection let remoteDataService = RemoteDataService() let dataManager = DataManager(dataService: remoteDataService) let result = dataManager.processData() print(result) // Output: Processed: Data from remote service

In this example:

  • DataService is a protocol that defines the contract for fetching data.
  • RemoteDataService is a concrete implementation of DataService.
  • DataManager is a class that depends on DataService through its initializer.

This setup allows you to easily switch the implementation of DataService without modifying DataManager. For example, you could create a LocalDataService implementing DataService and use it with DataManager without changing the DataManager class.

class LocalDataService: DataService { func fetchData() -> String { return "Data from local storage" } } let localDataService = LocalDataService() let dataManagerWithLocalData = DataManager(dataService: localDataService) let resultLocal = dataManagerWithLocalData.processData() print(resultLocal) // Output: Processed: Data from local storage

This flexibility is especially useful for testing, as you can easily substitute real implementations with mock implementations for testing purposes.AI.

Difference between App Store Binary Rejection and Metadata rejection

When submitting an application to the App Store, developers may encounter two types of rejections: App Store Binary Rejection and Metadata Rejection. Understanding the difference between these rejections is essential for iOS developers using Xcode to maximize their chances of successfully getting their apps approved.

Difference between App Store Binary Rejection and Metadata rejection

App Store Binary Rejection refers to the rejection of the compiled binary code that forms the application. This rejection can stem from numerous reasons, such as memory leaks or violations of Apple's App Store Review Guidelines. Memory leaks occur when an app fails to release dynamically allocated memory, resulting in wasted memory resources and potential crashes. To mitigate memory leaks, developers must carefully use memory allocation and deallocation mechanisms provided by iOS and Xcode, such as ARC (Automatic Reference Counting) or manual memory management.

On the other hand, Metadata Rejection deals with issues related to the app's information and presentation on the App Store. It primarily focuses on the textual and visual components, including the app's name, description, screenshots, keywords, and other metadata. Common reasons for metadata rejection can include misleading app descriptions or inappropriate screenshots that do not accurately represent the actual app functionalities.

To avoid both types of rejections, it is imperative for developers to pay attention to detail during the development and submission process. After addressing potential memory leaks by employing proper memory management practices, developers must also ensure that the app's metadata accurately reflects its functionalities and adheres to Apple's guidelines. By debugging their code thoroughly using instruments provided by Xcode, developers can identify and resolve any memory leaks, while adhering to Apple's App Store Review Guidelines can prevent metadata rejection.

In conclusion, developers using Xcode to create iOS apps must be aware of the distinction between App Store Binary Rejection and Metadata Rejection. By addressing memory leaks, adhering to Apple's guidelines, and carefully preparing app metadata, developers can increase the chances of getting their apps approved on the App.AI.

Difference between normal function and function ending throw

Function ending with throw can throw error but normal function can not throw error it can just return value.

Lets take example :

1. Custom error enum

enum ErrorsToThrow: Error {
    case fileNotFound
    case fileNotReadable
    case fileSizeIsTooHigh

2. Make function which can throw error

func readFiles(path:Stringthrows  ->String {
        if path == "" {
            throw ErrorsToThrow.fileNotFound
        return "Data from file"


3. Calling readFiles function
   do {
            let dataFromString = tryreadFiles(path: "")
        } catch ErrorsToThrow.fileNotFound {
            print("error generated1")
        } catch ErrorsToThrow.fileNotReadable {
            print("error generated2")
        } catch ErrorsToThrow.fileSizeIsTooHigh {
            print("error generated3")
        } catch {

Now, let's analysis.

1. If in #2, function is not ending with throws can not throw error. But our function can throw error.
2. In #3, we have used try?, so if readFiles function return nil instead of throwing error. So in our case print(dataFromString) statement executed and it will nil means printing nil.

3. If try! is written and if function throw error, then fatal error will be occured and program will be crashed as we ensure that error will not occur by putting ! .
4. So if we want to execute catch statement then we have to use only try . try always used with do-catch.
5. try? and try! does not need do-catch block.

Write a program to define closure which accepts string and return integer.

 Write a closure that accepts string and return length of string(which is integer) :

let simpleClosure:(String) -> (Int) = { name in 

    return name.count


let result = simpleClosure("Hello World")


Identifying Memory Leaks using the Xcode

Identifying Memory Leaks in Xcode with Memory Graph Debugger is a great way to make application leakage proof. When it comes to detecting memory leaks in iOS development using Xcode, there are a few steps you can follow. Memory leaks can lead to performance issues, crashes, and an overall degraded user experience. Therefore, it is crucial to address and resolve them.

Identifying Memory Leaks in Xcode with Memory Graph Debugger.

1. Enable the Memory Debugging Tools:
   - Open your Xcode project and go to the "Product" menu.
   - Select "Scheme" and then click on "Edit Scheme."
   - In the left sidebar, select "Run" and then navigate to the "Diagnostics" tab.
   - Enable the "Memory Management" option and close the scheme editor.

2. Run the App with the Leaks Instrument:
   - Build and run your iOS app in the Xcode simulator or on a physical device.
   - Go to the "Product" menu and select "Profile."
   - Choose the "Leaks" instrument.
   - Observe the instrument while interacting with your app to detect any memory leaks.
   - If any leaks are found, it will be indicated in the "Allocations List" and "Leaks List" panels.

3. Analyze the Detected Leaks:
   - Click on the leaks listed in the "Leaks List" panel to review them in detail.
   - The "Allocations Summary" panel will show you the memory allocations and deallocations for each object.
   - Analyze the call stack to identify the source of the leak.
   - Pay attention to any objects that are not being deallocated when they should be.

4. Diagnose the Memory Leak:
   - Understanding the code responsible for the memory leak is key to finding a solution.
   - Use the call stack information and Xcode's debugging tools to trace back to where the leaked object is created or retained.
   - Review variables and references that may be keeping the object from being released.
   - Common causes of memory leaks include strong reference cycles, forgotten timers, delegates not set as weak, or improper use of closures.

5. Fix the Memory Leak:
   - Once you've identified the source of the memory leak, take appropriate actions to fix it.
   - Ensure that objects are properly deallocated by removing any unnecessary strong references.
   - Consider using weak or unowned references where appropriate.
   - Review your code for strong reference cycles and break them using capture lists or weak/unowned references.
   - Validate that the memory leak is resolved by re-running the app and using the Leaks instrument again.

By following these steps, you can effectively detect and fix memory leaks in your Xcode project. Regularly checking for and resolving memory leaks will result in a more stable and efficient iOS .

Explain collections in Swift

Following are different types of collections in Swift:


Let's understand one by one : 

1. Array

  • Most common and Widely used data type. 
  • Ordered collection of similar types of data. 


var normalArray = [Int]() // declaring an empty                        Int array like this

  • The array can be any type in swift. 
  • We can also declare with some initial values. 
  • Swift is a strongly typed language so we don’t have to tell the type when we declare an array but we can also specify the type

var normalArray = [2,3,4,5]
var normalArray: [Int] = [2,3,4,5]

You also can declare an array with some repeated values or using one or more arrays

var normalArray = Array(repeating: 1, count: 3) //initialize normalArray with three ones var normalArray = arrayOne + arrayTwo //initialize normalArray with addign arrayOne & arrayTwo
  • Insert elements into an Array: We can insert an element at the end of the array or in a valid position.
normalArray.append(12) //append element at the end of array
normalArray += [100.0] //append at the end
normalArray.insert(12, at: 0) //insert element at 0 position. This position value should be n to n-1 if array have n number of elements.
  • Access Elements of Array: We can access the element of an array using a subscript syntax, passing the index value into it. Also swift provided a list of methods to get values and check different parameters.
normalArray[2]  //return second element from array
normalArray.count // return the number of elements in array
normalArray.isEmpty // return false if array is empty
normalArray.capacity // return the number of elements in array
normalArray.first //get the first element from array
normalArray.last //get the last elements from array
normalArray.max() //get the max element from array
normalArray.min() //get the min element from array
normalArray.sorted() // sort the array
normalArray.reversed() //reverse an array
normalArray.shuffled() //shuffle the elements of an array
  • Replace elements of Array: Array value can set or replace in a specific index or for a range of the index.
normalArray[2] = 1 // replace the value in second index of arraynormalArray[1...3] = [12, 23, 34] // set first to third position value as 12, 23 &34
  • Remove elements from Array: Array elements can remove from different ways & different positions.
normalArray.removeAll() // remove allelement from array
normalArray.removeFirst() // remove first element from array
normalArray.removeLast() // remove last element from array
normalArray.remove(at: 2) //remove second element from arraynormalArray.removeLast(1) // remove the second element from the end index of arraynormalArray.removeFirst(2) // remove the thirdelement from the start index of the array
  • Iterate over the array: Every element of an array can be traverse using loop
for i in 0..<normalArray.count{
for item in normalArray{

2. Dictionary

  • Can hold multiple unordered data in key-value pairs. 
  • Each value is associated with a unique key and any value from the dictionary can access with the associated key with it.

  • Declaration: 
var normalDictionary = [Int: String]() 
// an empty "normalDictionary" with Int type key & String type value

  • The key-value pair of a dictionary can be any type in swift. 
  • We can also declare with some initial values. 
  • Swift is a strongly typed language so we don’t have to tell the type of key-value pair when we declare a dictionary but we can also specify the type

var normalDictionary = [1: "John", 2: "Doe"]
var normalDictionary : [Int: String] = [1: "John", 2: "Doe"]
  • Insert elements into a Dictionary: We can insert a value into the dictionary using a key. If the key already exists it will replace the previous value for this key elsewhere it will add a new value.
normalDictionary[3] = "Mihalos"  // Add a value "Mihalos" for key "3"
  • Access Elements of Dictionary: We can access the elements of a dictionary by using the key. Also swift provided a list of methods to get values and check different parameters.
normalDictionary[2] //return the value of second key-value pair in dictionary normalDictionary.count // return the number of elements in dictionarynormalDictionary.isEmpty  // return false if dictionary is emptynormalDictionary.capacity  // return the number of elements in dictionarynormalDictionary.first  //get the first element from dictionarynormalDictionary.max { a, b in a.value < b.value}  // return key-value pair for maximum valuenormalDictionary.min()  { a, b in a.value < b.value}  // return key-value pair for minimumvaluenormalDictionary.sorted( by: { $0.0 < $1.0 }) // reurn an sorted array of dictionarynormalDictionary.reversed()  //return key-value pair in reverse ordernormalDictionary.shuffled()  //shuffle the key-value pair of dictionary
  • Replace elements from a Dictionary: A value for a key is simply replaced by putting a new value for that key. We also can use “updateValue” method to replace the value for any key. But Remember “updateValue” method returns the previous value as output.
normalDictionary[3] = "Mike"
normalDictionary.updateValue("Duke", forKey: 3)
  • Remove elements from Dictionary: Dictionary elements can be removed in a different way.
normalDictionary[3] = nil  // remove key-value pair for key "3"
normalDictionary.removeAll() // remove all element from dictionary arraysimpleDictionary.removeAll(keepingCapacity: true) remove all elements with keeping the capacity or not
  • Iterate over the Dictionary: Every key-value pairs of a dictionary can be traverse using loop
for (key, value) in normalDictionary {
print(key, value)

Instead of key-value pair we also can access only key or value from dictionary like this

for key in normalDictionary.keys{
for value in normalDictionary.values{

We also can get the keys and values of a dictionary as an array.

let keys = [Int](normalDictionary.keys)  // return all the keys as an array
let values = [String](normalDictionary.values) //return all the values as an array

3. Set

  • Set is an unordered collection of the same type of data. 
  • The main characteristic of the set is, set can not have any duplicate elements.

  • Declaration: You can declare an empty Int type set like this
var normalSet = Set<Int>() // an empty Int type set

  • The set can be any type in swift. 
  • We can also declare with some initial values. 
  • Swift is a strongly typed language so we don’t have to tell the type when we declare a set but we can also specify the type

var normalSet = Set([10, 20, 30, 40, 20])
varnormalSet: Set<String> = ["John", "Doe", "Hi", "John"]

You also can declare a set with some repeated values but you will get a single value for those repeated value cause set didn’t store any duplicate element.

var normalSet = Set(repeatElement(1, count: 3)) // Initialize set with three ones but set will have only one into it.
  • Insert elements into a set: We can insert an element at the end of the array or in a valid position.
normalSet.insert(12) // //append element at the end of set
  • Access Elements of a set: Like array you cannot access set elements using subscript syntax but you can check an element is in the set or not. Also swift provided a list of methods to get values and check different parameters.
normalSet.contains(2)  // check an element is in the set or not
normalSet.count // return the number of elements in set
normalSet.isEmpty // return false if set is 
empty normalSet.capacity   // return the number of elements in setnormalSet.first //get the first element from set
normalSet.max() //get the max element from set
normalSet.min() //get the min element from set
normalSet.sorted() // sort the set
normalSet.reversed() // reverse set
normalSet.shuffled() // shuffle set elements
  • Remove elements: Set elements can remove from different way & from different positions also.
normalSet.removeFirst() // remove first element from set
normalSet.remove(1) // remove from a specific index
normalSet.removeAll() // remove all elements from set
normalSet.removeAll(keepingCapacity: true) // remove all elements with keeping the capacity or not
  • Iterate over the set: Every element of a set can be traverse using loop
for data in normalSet {

The set type didn’t have any order so if you want to traverse in any order you can do it by swift provided methods

for data in normalSet.sorted() {
for data in normalSet.reversed() {
  • Different set operations: You also can perform different set operations within two sets and each of those operations will create a new set.
normalSet2.union(normalSet3).sorted()  // perform union opertion within normalSet2 & normalSet3
normalSet2.intersection(normalSet3) // perform intersection within two set
normalSet2.subtracting(normalSet3) // perform subtracting
normalSet2.symmetricDifference(normalSet3) // perform symmetricDifference
normalSet2.isSubset(of: normalSet3) // check one set is subset of snother or not
normalSet2.isSuperset(of: normalSet3) // check superset or not
normalSet2.isDisjoint(with: normalSet3) // check disjoint or not

There are some more interview questions answers for collections in this site. Visit here