Struct और Class का अंतर
Structs किसी को आपके paper की एक photocopy देने जैसे हैं -- उन्हें अपनी खुद की अलग copy मिलती है -- जबकि classes किसी को बिल्कुल वही original paper देने जैसी हैं -- अब भी सिर्फ एक है।
Syntax
struct StructName { var property: Type } // value type: copied
class ClassName { // reference type: shared
var property: Type
init(property: Type) { self.property = property }
}
Value Semantics vs Reference Semantics
Core difference copying behavior है: एक struct instance assign या पास होने पर duplicate होता है, जबकि एक class instance reference से shared होता है।
उदाहरण: Value Semantics vs Reference Semantics
// Define a struct `ValueBox`
struct ValueBox { var content: Int }
// Define a class `ReferenceBox`
class ReferenceBox { var content: Int; init(content: Int) { self.content = content } }
// Declare a mutable variable `v1`, initialized to `ValueBox(content: 1)`
var v1 = ValueBox(content: 1)
// Declare a mutable variable `v2`, initialized to `v1`
var v2 = v1
v2.content = 2
// Print "Struct copies: v1=\(v1.content), v2=\(v2.content)" to the console
print("Struct copies: v1=\(v1.content), v2=\(v2.content)")
// Declare a constant `r1`, set to `ReferenceBox(content: 1)`
let r1 = ReferenceBox(content: 1)
// Declare a constant `r2`, set to `r1`
let r2 = r1
r2.content = 2
// Print "Class shares: r1=\(r1.content), r2=\(r2.content)" to the console
print("Class shares: r1=\(r1.content), r2=\(r2.content)")
Login to try C/C++/Java/PHP code in the editor
Choosing Between Struct and Class
बिना identity वाले simple data models के लिए आमतौर पर Structs पसंद किए जाते हैं, जबकि classes shared mutable state, reference identity, या inheritance चाहने वाले cases में सूट करती हैं।
Note:
एक अच्छा rule of thumb: एक struct से शुरू करें, और सिर्फ class में switch करें अगर आपको reference semantics चाहिए होने का कोई specific reason हो।
उदाहरण: Choosing Between Struct and Class
// Define a struct `Coordinate`
struct Coordinate { let lat: Double; let lon: Double }
// Declare a constant `home`, set to `Coordinate(lat: 40.0, lon: -75.0)`
let home = Coordinate(lat: 40.0, lon: -75.0)
// Print "Home at \(home.lat), \(home.lon)" to the console
print("Home at \(home.lat), \(home.lon)")
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. #}
आम गलतियां
- दूसरी languages की आदत से हर चीज़ के लिए default रूप से
classउपयोग करना; Swift और Apple की खुद की guidance structs पसंद करती है जब तक reference semantics या inheritance specifically ज़रूरी न हो। - यह न समझना कि structs inheritance की ज़रूरत के बिना classes की तरह ही protocols और extensions support करते हैं।
- यह भूल जाना कि सिर्फ classes किसी दूसरे class से inheritance support करते हैं; structs किसी दूसरे struct से inherit नहीं कर सकते।
चैप्टर सारांश
- Structs value types हैं (assignment पर copy होते हैं); classes reference types हैं (assignment पर shared होते हैं)।
- सिर्फ classes किसी दूसरे class से inheritance support करते हैं।
- Structs
deinitसे deinitialize नहीं हो सकते; सिर्फ classes कर सकती हैं, क्योंकि सिर्फ उनका lifecycle references से जुड़ा है। - Apple default रूप से structs पसंद करने की सिफारिश करता है, classes उपयोग करते हुए जब shared mutable state या inheritance genuinely ज़रूरी हो।
🔒
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: