SwiftUI Specific Questions
-
-
Save byJeevan/a13bcddd018bbc75de32540832ef6393 to your computer and use it in GitHub Desktop.
SwiftUI Questions
🎖️ Advanced
What are the different types of views available in SwiftUI?
- Basic views: These are the simplest and most common views that display text, images, shapes, colors, spacers, dividers, etc. (Text, Image, Circle, Color, Spacer, Divider, etc.)
- Control views: These are views that provide user interaction and control over some aspect of the app. (Button, Toggle, Slider, Picker, Stepper, TextField, etc.)
- Container views: These are views that can contain and arrange other views in various ways. (VStack, HStack, ZStack, LazyVStack, LazyHStack, LazyVGrid, LazyHGrid, List, Form, GroupBox, etc.)
- Navigation views: These are views that provide navigation and presentation options for your app. (NavigationView, TabView, NavigationLink, Popover, Sheet, Alert, ActionSheet, etc.)
- Drawing and animation views: These are views that provide drawing and animation capabilities for your app. (Path, Shape, GeometryReader, Canvas, DrawingGroup, AnimatableModifier, AnimationViewModifier, etc.)
6.Chart views: These are views that provide charting and data visualization capabilities for your app. Examples of chart views are LineChartViewStyle (Swift Charts), BarChartViewStyle (Swift Charts), PieChartViewStyle (Swift Charts), etc. - WidgetKit views: These are views that provide widget-specific functionality for your app. Examples of WidgetKit views are WidgetConfiguration (WidgetKit), WidgetView (WidgetKit), Link (WidgetKit), etc.
Why always declare state as private?
SwiftUI manages the storage of a property that you declare as state. When the value changes, SwiftUI updates the parts of the view hierarchy that depend on the value. Use state as the single source of truth for a given value stored in a view hierarchy.
struct PlayButton: View {
@State private var isPlaying: Bool = false
var body: some View {
Button(isPlaying ? "Pause" : "Play") {
isPlaying.toggle()
}
}
}
Don’t initialize a state property of a view at the point in the view hierarchy where you instantiate the view, because this can conflict with the storage management that SwiftUI provides. (?)
To avoid this, always declare state as private, and place it in the highest view in the view hierarchy that needs access to the value. Then share the state with any child views that also need access, either directly for read-only access, or as a binding for read-write access.
How VStack renders ? Explain in depth how it works in run time ?
- Rendering Cycle: SwiftUI's rendering engine follows a rendering cycle to manage the display of views.
- State Changes: When a state or data change occurs, SwiftUI triggers a re-rendering cycle.
- View Update: SwiftUI identifies the views affected by the state change and updates them.
- Diffing Algorithm: SwiftUI uses a diffing algorithm to minimize the number of changes needed to update the views. It compares the old and new states to decide what needs to change.
- Layout Updates: SwiftUI recalculates the layout of views if necessary, taking into account the changed state or data.
- Drawing: Once the layout is recalculated, SwiftUI issues draw commands to the underlying graphics system (Core Graphics or Metal) to render the views.
Interview questions
- https://medium.com/@shashidj206/this-insightful-medium-article-compiles-a-comprehensive-array-of-swiftui-interview-questions-a3fad8764d9a
- https://www.turing.com/interview-questions/swift
- Tutorial with Live action https://www.swiftuifieldguide.com/layout/hstack/#alignment
- Tutorials: https://getstream.io/blog/learn-swiftui/
SwiftUI Questions
🚨 Important
What is SwiftUI ? Key difference from UIKit ?
Advantages & Drawbacks of SwiftUI
Pros :
Cons:
What is the difference between a State and a Binding in SwiftUI?
A State is a property wrapper that allows you to read and write a value, while a Binding is a property wrapper that allows you to read and write a value and also share it with other views.
How do we share with other views?
You can use the $ operator to create a binding to a state property and pass it to another view.
Why SwiftUI uses struct for view and not class?
Immutable: Structs are immutable and copied when passed around, which helps prevent unintended sharing of mutable state. This makes it easier to reason about and manage the state of a SwiftUI view hierarchy.
Performance: Structs are generally more lightweight than classes because they are stack-allocated and don't require reference counting for memory management. This can lead to better performance, especially in the context of frequently redrawn UIs.
Thread Safety: Since structs are immutable by default, they are inherently thread-safe, which can simplify concurrency management in SwiftUI apps.
Ease of Use: Structs in Swift have automatic memberwise initializers, making them easy to initialize and use in SwiftUI. This reduces boilerplate code and enhances readability.
Predictability: The use of structs promotes a predictable data flow within SwiftUI applications, as changes to a view's state are confined to the view itself and don't affect other parts of the application.
What is the difference between a Text and a Label in SwiftUI?
A Text is a low-level text view that can be customized with various text styles and attributes, while a Label is a higher-level text view that uses a standard font and text color by default.
What is the difference between a List and a ScrollView in SwiftUI?
A List is a container view that presents rows of data arranged in a single column, while a ScrollView is a container view that presents content in a scrollable viewport.
What is the role of the environment in SwiftUI?
The environment is a way to pass data and settings down the view hierarchy in SwiftUI. You can use the environmentObject() and environment() modifiers to set values in the environment.
How to create a custom view in SwiftUI?
To create a custom view in SwiftUI, you can create a new struct that conforms to the View protocol and define its body property. You can also use the custom modifier to customize the appearance of a view.