Skip to content

Instantly share code, notes, and snippets.

@byJeevan
Last active July 14, 2026 12:59
Show Gist options
  • Select an option

  • Save byJeevan/a13bcddd018bbc75de32540832ef6393 to your computer and use it in GitHub Desktop.

Select an option

Save byJeevan/a13bcddd018bbc75de32540832ef6393 to your computer and use it in GitHub Desktop.
Notes written SwiftUI QnA / Interview format

SwiftUI Specific Questions

@byJeevan

byJeevan commented Apr 5, 2024

Copy link
Copy Markdown
Author

SwiftUI Questions

🎖️ Advanced

What are the different types of views available in SwiftUI?

  1. 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.)
  2. Control views: These are views that provide user interaction and control over some aspect of the app. (Button, Toggle, Slider, Picker, Stepper, TextField, etc.)
  3. 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.)
  4. Navigation views: These are views that provide navigation and presentation options for your app. (NavigationView, TabView, NavigationLink, Popover, Sheet, Alert, ActionSheet, etc.)
  5. 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.
  6. 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 ?

  1. Rendering Cycle: SwiftUI's rendering engine follows a rendering cycle to manage the display of views.
  2. State Changes: When a state or data change occurs, SwiftUI triggers a re-rendering cycle.
  3. View Update: SwiftUI identifies the views affected by the state change and updates them.
  4. 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.
  5. Layout Updates: SwiftUI recalculates the layout of views if necessary, taking into account the changed state or data.
  6. Drawing: Once the layout is recalculated, SwiftUI issues draw commands to the underlying graphics system (Core Graphics or Metal) to render the views.

@byJeevan

byJeevan commented Apr 17, 2024

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment