We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
Polymorphism: Subclasses can override methods and properties
of the superclass to provide specific behavior.
Identity: Class instances have identity, and you can use the === operator to check if two variables refer to the same instance. Deinitialization: Classes support a deinit method, which is called before an instance is deallocated. Mutability: Instances can modify their properties even if declared as a constant (let), as long as the properties are mutable (var). Automatic Reference Counting (ARC): Classes are managed by ARC, which tracks and releases memory for instances no longer in use. Use Cases: Best suited for scenarios involving shared references, object identity, inheritance, or lifecycle management. Thread Safety Issues: Race conditions can occur if multiple threads modify shared resources without proper synchronization. For example: o Thread 1 reads a value. o Thread 2 reads the same value. o Both threads modify it independently, leading to incorrect results. o To prevent this, use locks or queues to synchronize access to the shared resource. Structures: Value Type: Structures are value types, meaning each instance is unique. Changes to one instance do not affect others. No Inheritance: Structures do not support inheritance, making them lightweight and straightforward. Automatic Memberwise Initializers: Structures automatically generate a memberwise initializer if no custom initializer is provided. Immutable with let: When declared as a constant (let), the entire structure becomes immutable, even if its properties are declared as var. Stack Allocation: Structures are stored in the stack, making them faster for small, lightweight data. Copy-on-Write Behavior: When assigned to a new variable or passed to a function, they are copied, ensuring each instance is independent. Thread Safety: Structures are safer for concurrency because they do not share references between instances, reducing the risk of race conditions. No ARC Overhead: Structures do not rely on ARC for memory management, reducing performance overhead. Use Cases: Ideal for lightweight data models, immutable data, or objects that should be copied rather than shared.
Q: What are actors in Swift?
Actors: Concurrency-Safe Reference Type: Actors are a reference type introduced in Swift to provide thread-safety for mutable state. They prevent race conditions by ensuring that only one thread can access an actor’s mutable state at a time. Automatic Synchronization: Swift automatically handles the synchronization of access to an actor’s properties and methods, reducing the need for manual locking mechanisms (e.g., using NSLock or DispatchQueue).
Q: How are enums different?
Enums allow you to group related values and create flexible, type-safe types. Associated values enable you to store additional custom data with each enum case. Enums make code more manageable by enforcing type safety and facilitating pattern matching with switch.
Properties, Methods, and Initializers
Q: What are properties and methods in Swift? Properties: Variables or constants inside a type (var, let). Methods: Functions inside a type. Q: What are initializers? Special methods that set up initial property values when an instance is created.
Inheritance, Encapsulation, and Polymorphism
Q: What is inheritance? Allows a class to inherit properties and methods from another class. Q: What is encapsulation? Hiding implementation details by controlling access levels. Q: What is polymorphism? Using a parent class reference to call overridden methods in child classes. Example: class Animal { func sound() -> String { return "Some sound" } }
Q: What are generics in Swift? Generics allow you to write flexible and reusable code by letting types be parameters. Q: What are generic protocols? Protocols that use associated types for flexibility. Example: func swapValues<T>(_ a: inout T, _ b: inout T) { let temp = a a=b b = temp }