Posts

Showing posts from August, 2024

API Basic To Advance in 9 Steps. Become Pro.

 API Basic To Advance in 9 Steps. Code Like Pro. 1. Basic API Call A simple GET request using URLSession . import Foundation func basicGetRequest () { guard let url = URL (string: "https://jsonplaceholder.typicode.com/todos/1" ) else { return } let task = URLSession .shared.dataTask(with: url) { (data, response, error) in guard let data = data, error == nil else { print ( "Error:" , error ?? "Unknown error" ) return } if let httpResponse = response as? HTTPURLResponse { print ( "Status Code: \(httpResponse.statusCode) " ) } if let result = try? JSONSerialization .jsonObject(with: data, options: []) { print ( "Result:" , result) } } task.resume() } 2. Handling JSON with Decodable Use Codable for type-safe JSON parsing. import Foundation struct Todo : Codable { let userId:...

50 Swift iOS Most Asked Interview Question Answers.

  50 Swift iOS Most Asked Interview Question Answers. Basic Swift Questions What are the main differences between var and let in Swift? var declares a variable that can be changed, while let declares a constant that cannot be changed after initialisation . Explain the difference between struct and class in Swift. struct is a value type (copied when assigned), while class is a reference type (shared reference when assigned). What is an optional in Swift, and how do you unwrap it? An optional is a type that can hold either a value or nil . It is unwrapped using ! , if let , guard let , or optional chaining. What is the purpose of guard statement in Swift? guard is used for early exits in a function or method if certain conditions are not met. It ensures conditions are met before proceeding. How does Swift handle memory management? Swift uses Automatic Reference Counting (ARC) to manage memory, automatically deallocating objects when they are no longer needed. What is a tupl...