Associated Values क्या हैं
एक associated value अतिरिक्त custom information है जिसे आप किसी enum के सिर्फ एक specific choice से attach कर सकते हैं, एक barcode number जैसा जो सिर्फ "scanned item" option से attached है।
Syntax
enum EnumName {
case caseName(label: Type)
case other(Type, Type)
}
let value = EnumName.caseName(label: data)
switch value {
case .caseName(let label): /* use label */
case .other(let a, let b): /* use a, b */
}
Declaring Associated Values
एक case parentheses में एक या ज़्यादा associated values declare कर सकता है, उस case के हर instance को अपना खुद का custom data रखने देते हुए।
उदाहरण: Declaring Associated Values
// Define an enum `NetworkResult` listing its possible cases
enum NetworkResult {
// Case `success`
case success(code: Int)
// Case `failure`
case failure(message: String)
}
// Declare a constant `result`, set to `NetworkResult.success(code: 200)`
let result = NetworkResult.success(code: 200)
// Switch on `result` and branch by case
switch result {
// Case: `.success(let code)`
case .success(let code):
// Print "Success with code \(code)" to the console
print("Success with code \(code)")
// Case: `.failure(let message)`
case .failure(let message):
// Print "Failed: \(message)" to the console
print("Failed: \(message)")
}
Login to try C/C++/Java/PHP code in the editor
Different Cases Carrying Different Data
किसी enum में हर case associated data की बिल्कुल अलग shape रख सकता है, उस specific case represent करने वाली चीज़ के अनुसार tailored।
उदाहरण: Different Cases Carrying Different Data
// Define an enum `Shape` listing its possible cases
enum Shape {
// Case `circle`
case circle(radius: Double)
// Enum case(s): `rectangle(width: Double, height: Double)`
case rectangle(width: Double, height: Double)
}
// Declare a constant `shapes`, set to `[Shape.circle(radius: 3), Shape.rectangle(width: 4, height: 5)]`
let shapes = [Shape.circle(radius: 3), Shape.rectangle(width: 4, height: 5)]
// Loop over `shapes`, binding each item to `shape`
for shape in shapes {
// Switch on `shape` and branch by case
switch shape {
// Case: `.circle(let radius)`
case .circle(let radius):
// Print "Circle with radius \(radius)" to the console
print("Circle with radius \(radius)")
// Case: `.rectangle(let width, let height)`
case .rectangle(let width, let height):
// Print "Rectangle \(width) x \(height)" to the console
print("Rectangle \(width) x \(height)")
}
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- Associated values (per-instance attached data, pattern matching से निकाला गया) को raw values (per case एक single fixed value,
.rawValueसे retrieved) के साथ confuse करना। - यह भूल जाना कि associated values निकालने के लिए आपको
let/varbindings के साथswitchयाif caseउपयोग करना चाहिए -- आप इन्हें एक normal property की तरह access नहीं कर सकते। - एक enum को उसी cases पर raw values और associated values दोनों देने की कोशिश करना; Swift एक enum पर दोनों को mix करने नहीं देता।
चैप्टर सारांश
- Associated values हर case को specified types का अपना extra data रखने देते हैं।
- Associated values निकालना pattern matching चाहता है, आमतौर पर
switchयाif caseके साथ। - उसी enum में अलग-अलग cases पूरी तरह अलग associated data रख सकते हैं।
- Associated values enums को variant data model करने में powerful बनाते हैं, जैसे network results।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: