← Back to Swift Course | Chapter 11: Error Handling | Lesson 3 of 6

Result Type की जानकारी

Result एक special box है जो हमेशा दो चीज़ों में से एक रखता है: या तो अंदर एक value वाली एक success, या अंदर एक error वाली एक failure।
Syntax
swift
func functionName() -> Result<SuccessType, ErrorType> {
    return .success(value)   // or .failure(error)
}

switch functionName() {
case .success(let value): /* use value */
case .failure(let error): /* handle error */
}

try functionName().get()

Creating and Switching on a Result

एक Result<Success, Failure> value या तो .success या .failure के रूप में बनाई जाती है, और एक switch से handle की जाती है जो हर case की associated value bind करता है।

उदाहरण: Creating and Switching on a Result

markup
// Define an enum `MathError` listing its possible cases
enum MathError: Error {
    // Case `divideByZero`
    case divideByZero
}
// Define the function `divide` taking `a`, `b` and returning a `Result<Int, MathError>`
func divide(_ a: Int, by b: Int) -> Result<Int, MathError> {
    // Check whether `b == 0`
    if b == 0 {
        // Return `.failure(.divideByZero)` from this function
        return .failure(.divideByZero)
    }
    // Return `.success(a / b)` from this function
    return .success(a / b)
}
// Declare a constant `result`, set to `divide(10, by: 2)`
let result = divide(10, by: 2)
// Switch on `result` and branch by case
switch result {
// Case: `.success(let value)`
case .success(let value):
    // Print "Result: \(value)" to the console
    print("Result: \(value)")
case .failure(let error):
    print("Error: \(error)")
}

Converting a Result to a Throwing Call

Result एक .get() method प्रदान करता है जो इसे वापस एक throwing expression में convert करता है, दोनों error-handling styles को bridge करते हुए।

उदाहरण: Converting a Result to a Throwing Call

markup
// Define an enum `MathError` listing its possible cases
enum MathError: Error {
    // Case `divideByZero`
    case divideByZero
}
// Define the function `divide` taking `a`, `b` and returning a `Result<Int, MathError>`
func divide(_ a: Int, by b: Int) -> Result<Int, MathError> {
    b == 0 ? .failure(.divideByZero) : .success(a / b)
}
do {
    // Declare a constant `value`, set to `try divide(9, by: 0).get()`
    let value = try divide(9, by: 0).get()
    // Print `value` to the console
    print(value)
} catch {
    // Print "Caught via get(): \(error)" to the console
    print("Caught via get(): \(error)")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि Result को दो generic types चाहिए -- success type और failure (error) type -- दोनों इसकी declaration में specified।
  2. किसी Result पर switch उपयोग करना लेकिन यह भूल जाना कि हर case अपनी खुद की associated value bind करता है (.success(let value) / .failure(let error))।
  3. किसी function के return type के लिए Result चुनना जब synchronous code के लिए एक simple throws function simpler होता -- Result outcomes को store या pass around करने के लिए ज़्यादा चमकता है, खासकर async callbacks में।
चैप्टर सारांश
  • Result<Success, Failure> या तो .success(value) या .failure(error) represent करता है।
  • यह उन outcomes represent करने के लिए विशेष रूप से उपयोगी है जिन्हें store या pass around करना है, सिर्फ तुरंत handle नहीं।
  • switch किसी Result के दोनों cases handle करने का natural तरीका है।
  • Result को इसके Failure type को Error conform करने की ज़रूरत है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.