Skip to main content

Swift API Manager -Alamofire-Refresh Token-With TestCases

  import Foundation import KeychainAccess enum APIError : Error { case accessTokenExpired case networkError // Add more error cases as needed } class APIManager { private let keychain = Keychain (service: "com.example.app.refreshToken" ) private let refreshTokenKey = "refreshToken" private var accessToken: String ? func callAPI < T : Codable >( urlString : String , method : String , parameters : [ String : Any ] ? , completion : @escaping ( Result < T , APIError >) -> Void ) { guard let url = URL (string: urlString) else { completion(.failure(.networkError)) return } var request = URLRequest (url: url) request.httpMethod = method // Add access token to the request headers if available if let token = accessToken { request.setValue( "Bearer \(token) " , forHTTPHeaderField: "Aut...

Control Flow Statements - statements used in swift


List out what is the control transfer statements used in swift

Control flow statements are used to control the flow of execution in a program. There are several types of control flow statements in Swift, including loop statements, branch statements, and control transfer statements. 
1.       Loop statements allow a block of code to be executed repeatedly
2.        Branch statements allow a certain block of code to be executed only when certain conditions are met
3.       Control transfer statements provide a way to alter the order in which code is executed.
4.       Swift provides a do statement to introduce scope, and catch and handle errors,
5.       Defer statement for running cleanup actions just before the current scope exits.



Loop Statements -

Loop statements allow a block of code to be executed repeatedly, depending on the conditions specified in the loop.
Swift there are three loop statements
·         For-in statement
·         While statement
·         Repeat-while statement
A for-in statement allows a block of code to be executed once for each item in a collection that conforms to the Sequence protocol.
for item in collection {
    statements
}
A while statement allows a block of code to be executed repeatedly, a condition remains true.
while condition {
    statements
}
A repeat-while statement allows a block of code to be executed one or more times, then condition true.
repeat {
    statements
} while

Branch Statements

Branch statements allow a certain block of code to be executed only when certain conditions are met. Swift have three branch statements
·         if statement
·         guard statement
·         Switch statement
Guard Statement
A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.
guard condition else {
    statements
}
Switch Statement
A switch statement allows certain blocks of code to be executed depending on the value of a control expression.
switch control expression {
case pattern 1:
    statements
case pattern 2 where condition:
    statements
case pattern 3 where condition,
     pattern 4 where condition:
    statements
default:
    statements
}

Control Transfer Statements

Control transfer statements can change (alter) the order in which code in your code is executed.
 Swift have five control transfer statements
·         break statement,
·         continue statement - A continue statement ends program execution of the current iteration of a loop statement 
·         fallthrough statement
·         return statement
·         throw statement

Comments

  1. Thanks for the useful information, give more updates like First time I visit your site really nice, here after a daily visit.
    ecommerce website development company in chennai

    ReplyDelete

Post a Comment