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

🚨 Important

What is SwiftUI ? Key difference from UIKit ?

  • Introduced in 2019
  • A framework for building UI in a declarative manner for Apple platforms
  • Difference is SwiftUI uses a declarative syntax whereas UIKit uses imperative syntax.

Supports iOS, iPadOS, watchOS, tvOS, visionOS and macOS.

Advantages & Drawbacks of SwiftUI

Pros :

  • It has a declarative syntax that makes it easy to write and understand the UI code.
  • It offers a live preview that lets you see the changes in your UI instantly without recompiling.
  • It supports automatic features such as Dynamic Type, Dark Mode, Localization, and Accessibility.
  • It is cross-platform which means that developers can use the same code to build user interfaces across all of Apple’s platforms. This is different from UIKit, which is specific to iOS.
  • It can be mixed with UIKit using UIHostingController for more flexibility and compatibility.

Cons:

  • limited compatibility with older iOS versions (< 13.0 unsupported).
  • framework that is still very young compared to UIKit*

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?

  1. 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.

  2. 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.

  3. Thread Safety: Since structs are immutable by default, they are inherently thread-safe, which can simplify concurrency management in SwiftUI apps.

  4. 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.

  5. 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.

@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