Рет қаралды 97
#Closures-In-Swift|@escaping-and-non-escaping-completion-handlers|in-Urdu
1: When to use #closures ?
Asynchronous code: Closures are often used to handle asynchronous operations, such as network requests or file I/O. By using closures, you can specify what should happen when the operation completes, without having to block the main thread.
Sorting and filtering: Closures can be used to sort or filter arrays or other collections based on specific criteria. This can be particularly useful when dealing with complex data types or when sorting or filtering needs to be done dynamically at runtime.
Callbacks: Closures can be passed as parameters to functions or methods and used as callbacks to handle events or respond to user actions. This can be useful in many different types of applications, from mobile apps to web services.
Capturing values: Closures can capture and manipulate variables from their surrounding context. This can be useful when you need to modify or access variables that are outside the scope of the closure.
2 : Difference between #escaping and #nonescaping completion handlers.
A completion handler, on the other hand, is a specific type of closure that is used to notify the caller of an asynchronous operation when it is complete. A completion handler is typically passed as a parameter to an asynchronous function, and is called when the function has finished its work.
#swift #forbeginners #callbacks @escaping, @non-escaping, completion handlers