Curated iOS Interview Questions Series — Part 5

Rahul Goel
4 min readJun 20, 2024

--

Series aiming to cover as many Q&A’s as possible from iOS interview perspective.

Do check out Part-1, Part-2, Part-3 , Part-4 before proceeding further.

51. Parsing dynamic nested JSON in Swift, where it can have nested dictionary or nested array or Int or String.

import Foundation

// Function to parse and print all key-value pairs from dynamic JSON
func printDynamicJSON(jsonData: Data) {
do {
// Deserialize JSON data
let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: [])

// Process JSON object
if let jsonDict = jsonObject as? [String: Any] {
processJSONObject(jsonDict, prefix: "")
}
} catch {
print("Error parsing JSON: \(error)")
}
}

// Recursive function to process JSON object
func processJSONObject(_ jsonObject: [String: Any], prefix: String) {
for (key, value) in jsonObject {
if let nestedDict = value as? [String: Any] {
// Nested object, recurse
processJSONObject(nestedDict, prefix: "\(prefix)\(key).")
} else if let nestedArray = value as? [[String: Any]] {
// Nested array of objects, recurse for each element
for (index, element) in nestedArray.enumerated() {
processJSONObject(element, prefix: "\(prefix)\(key)[\(index)].")
}
} else {
// Leaf node, print key-value pair
print("\(prefix)\(key): \(value)")
}
}
}

// Example usage
let jsonString = """
{
"key1": "value1",
"key2": 123,
"key3": {
"nested_key1": "nested_value1",
"nested_key2": [1, 2, 3]
}
}
"""

if let jsonData = jsonString.data(using: .utf8) {
printDynamicJSON(jsonData: jsonData)
}

52. Create a thread-safe class in Swift with read and write operations.

import Foundation

class ThreadSafeArray<T> {
private var array: [T]
private let lock = NSLock()

init() {
array = []
}

func read(at index: Int) -> T? {
lock.lock()
defer {
lock.unlock()
}
guard index >= 0 && index < array.count else { return nil }
return array[index]
}

func write(_ value: T, at index: Int) {
lock.lock()
defer {
lock.unlock()
}
if index >= 0 && index < array.count {
array[index] = value
} else if index == array.count {
array.append(value)
} else {
fatalError("Index out of range")
}
}

func append(_ value: T) {
lock.lock()
defer {
lock.unlock()
}
array.append(value)
}

func count() -> Int {
lock.lock()
defer {
lock.unlock()
}
return array.count
}
}

// Example usage:
let threadSafeArray = ThreadSafeArray<Int>()

// Write operations
DispatchQueue.concurrentPerform(iterations: 10) { index in
threadSafeArray.write(index, at: index)
}

// Read operations
DispatchQueue.concurrentPerform(iterations: 10) { index in
if let value = threadSafeArray.read(at: index) {
print("Value at index \(index): \(value)")
} else {
print("Index \(index) is out of range")
}
}

53. Create UILabel programatically with UIkit.

import UIKit

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

// Create a UILabel
let label = UILabel()

// Set properties for the label
label.text = "Hello, World!" // Text to display
label.textAlignment = .center // Text alignment
label.textColor = UIColor.black // Text color
label.font = UIFont.systemFont(ofSize: 20) // Font size and style

// Set frame for the label
label.frame = CGRect(x: 50, y: 100, width: 200, height: 50)

// Add the label to the view
self.view.addSubview(label)
}
}

54. ViewController life cycle methods?

1. init(coder:) or init(nibName:bundle:)
2. loadView() (optional):
3. viewDidLoad():
4. viewWillAppear(_:):
5. viewDidAppear(_:):
6. viewWillDisappear(_:):
7. viewDidDisappear(_:):
8. deinit

55. Various App States of iOS App:

1. Not Running
2. Inactive
3. Active
4. Background
5. Suspended

56. Write Code for your own JSONSerialization in Swift:

import Foundation

enum SerializationError: Error {
case invalidObject
case invalidData
}

struct JSONSerialization {

static func serializeObjectToJSON(object: Any) throws -> Data {
guard JSONSerialization.isValidJSONObject(object) else {
throw SerializationError.invalidObject
}

let jsonData = try JSONSerialization.serializeObject(object: object)
return jsonData
}

static func deserializeJSONToObject(jsonData: Data) throws -> Any {
return try JSONSerialization.deserializeData(data: jsonData)
}

private static func serializeObject(object: Any) throws -> Data {
if let object = object as? Int {
return "\(object)".data(using: .utf8)!
} else if let object = object as? Double {
return "\(object)".data(using: .utf8)!
} else if let object = object as? Bool {
return "\(object)".data(using: .utf8)!
} else if let object = object as? String {
return try serializeString(string: object)
} else if let object = object as? [Any] {
return try serializeArray(array: object)
} else if let object = object as? [String: Any] {
return try serializeDictionary(dictionary: object)
} else {
throw SerializationError.invalidObject
}
}

private static func serializeString(string: String) throws -> Data {
let jsonString = "\"\(string)\""
return jsonString.data(using: .utf8)!
}

private static func serializeArray(array: [Any]) throws -> Data {
var jsonArray = "["

for (index, element) in array.enumerated() {
let jsonData = try serializeObject(object: element)
jsonArray += "\(String(data: jsonData, encoding: .utf8)!)"
if index < array.count - 1 {
jsonArray += ","
}
}

jsonArray += "]"
return jsonArray.data(using: .utf8)!
}

private static func serializeDictionary(dictionary: [String: Any]) throws -> Data {
var jsonObject = "{"

for (key, value) in dictionary {
let jsonData = try serializeObject(object: value)
jsonObject += "\"\(key)\":\(String(data: jsonData, encoding: .utf8)!)"
jsonObject += ","
}

jsonObject.removeLast()
jsonObject += "}"
return jsonObject.data(using: .utf8)!
}

private static func deserializeData(data: Data) throws -> Any {
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
return jsonObject
}
}

// Example usage:
do {
let dictionary = ["name": "John", "age": 30, "city": "New York"]
let jsonData = try JSONSerialization.serializeObjectToJSON(object: dictionary)

let deserializedObject = try JSONSerialization.deserializeJSONToObject(jsonData: jsonData)

print("Serialized JSON Data:", String(data: jsonData, encoding: .utf8)!)
print("Deserialized Object:", deserializedObject)
} catch {
print("Error:", error.localizedDescription)
}

Follow me Rahul Goel for further updates.

--

--

Rahul Goel
Rahul Goel

Written by Rahul Goel

Computer Science Enthusiast | 11+ Year of Software Evolution | @Sharechat, Groupon, Paytm, Myntra https://www.linkedin.com/in/therahulgoel/

No responses yet