Skip to main content
Getting your Trinity Audio player ready...

What is the difference between a State and a Binding in SwiftUI?

  • State is a property wrapper that allows you to read and write a value.
  • Binding is a property wrapper that allows you to read and write a value and also share it with other views.

What is the role of the body property in a SwiftUI view?

The body property is the main content of a view and it’s the only required property in a view. It defines what the view should display.

What is the difference between a Text and a Label in SwiftUI?

  • Text is a low-level text view that can be customized with various text styles and attributes.
  • 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?

  • List is a container view that presents rows of data arranged in a single column.
  • ScrollView is a container view that presents content in a scrollable viewport.

How do I use @State and @Binding in SwiftUI?

  • @State is used to create a state property that can be read and written to.
  • @Binding is used to share a state with another view.

How do I handle user input and gestures in SwiftUI?

SwiftUI uses the target-action pattern to handle user input and gestures. Examples include using the onTapGesture and onLongPressGesture modifiers on views for user input, and the gesture modifier for handling gestures.

How do I implement navigation in SwiftUI?

SwiftUI uses a navigation view to implement navigation. Use the navigationBarItems and navigationBarTitle modifiers on views to push a new view on the navigation stack.

What is the difference between a @StateObject and an @ObservedObject in SwiftUI?

  • Utilize @StateObject when the current view initializes the observed object to maintain consistency.
  • Employ @ObservedObject whenever you inject an observed object as a dependency.

How to implement data binding in SwiftUI?

Use the @Binding property wrapper and the $ operator to create a binding to a state property and pass it to another view.

What is the Combine framework?

The Combine framework is a reactive programming framework introduced by Apple, providing a declarative Swift API for processing events and data streams.

What are publishers and subscribers in the Combine framework?

  • Publishers emit a stream of values over time.
  • Subscribers receive and process those values, connected through a subscription.

How does Combine handle errors?

Combine uses a Failed publisher to emit errors, with subscribers handling errors by providing a catch block in the sink method.

What is the difference between merge and zip operators in Combine?

  • Merge combines multiple publishers into a single publisher by emitting all values as they are received.
  • Zip combines multiple publishers by emitting a tuple of the latest values from each publisher in the order they are received.

How can you debounce events in Combine?

Use the debounce operator with a dueTime parameter to wait before emitting the latest value.

How can you filter values in a stream using Combine?

Use the filter operator with a closure that returns a Boolean indicating whether the value should be included in the stream.

What is the assign(to:on:) operator in Combine?

This operator binds the output of a publisher to a property on an object, specifying the property and the object to bind to.

Can you explain the share() operator in Combine?

The share() operator allows multiple subscribers to attach to a single publisher, reducing redundant work like multiple network requests.

Leave a Reply