What is the 'some' keyword in SwiftUI?


The "some" keyword in SwiftUI is used to indicate that a type conforms with a protocol, but the exact conformance is not specified. The AnyView type, a type-erased view that can represent any view conforming to the View protocol, is commonly used in association with it.

SwiftUI defines some View as a type that, without identifying the specific view type, can represent any view that complies with the View protocol. This enables more generalized and adaptable code.

In other words, some keyword is used to declare Opaque types. In the Swift 5.1 version, this is introduced with the support of opaque return types.

What are opaque types?

When you use opaque type, it means you can declare the expected return type without defining an exact type.

For example, you can use some View to create a variable that can hold any view, without knowing the specific type of the view. Here is an example:

let anyView: some View = Text("Hello, World!")

This creates a variable named anyView that can hold any view conforming to the View protocol, and assigns it the value of a Text view displaying the string "Hello, World!".

In order to create a variable that can hold any conforming type to a protocol without knowing the particular type it will carry, some keyword is used as a type constraint.

Return opaque type without matching exact type

What will happen if we do not return the exact type? In this case, you will get an error like the one below.

Example

An example of code that contains the error looks as follows −

import SwiftUI
func makeHeaderView(isProUser: Bool) -> some View {
   if isProUser {
      return Text("You have a premium membership !!") // return type is Text
   } else {
      return VStack { // Return type is VStack)>>
         Text("Do you want to become a PRO user?")
         Button("Become PRO", action: {
            // write action code
         })
      }
   }
}

Output

This code will give you an error that looks like this:

error: function declares an opaque return type 'some View', but the return statements in its body do not have matching underlying types

As you can see in the above code, we are returning two types of data. Returning a Text for PRO users and a VStack for the non-premium users.

In this case, the compiler wants to know the underlying concrete type through the "some" keyword. In order to return different types within the same method scope, opaque types must be fixed for the scope of the value.

We could solve the above code by using a wrapping container, like a VStack

import SwiftUI
func makeHeaderView(isProUser: Bool) -> some View {
   VStack {
      if isProUser {
         Text("You have a premium membership !!") // return type is Text
      } else {
         Text("Do you want to become a PRO user?")
         Button("Become PRO", action: {
            // write action code
         })
      }
   }
}

We have added an additional container, which will only be needed if the isProUser property returns true. Instead of using result builders in the above code, it's better to rewrite it using the @ViewBuilder attribute −

import SwiftUI
@ViewBuilder
func makeHeaderView(isProUser: Bool) -> some View {
   if isProUser {
      Text("You have a premium membership !!") // return type is Text
   } else {
      Text("Do you want to become a PRO user?")
      Button("Become PRO", action: {
         // write action code
      })
   }
}

Conclusion

In conclusion, the “some” keyword in SwiftUI is used to tell the compiler the return type without telling the exact type of returned view. In this way, you can hide critical information.

In order to return the “some” type, you can use the wrapper as a container. Also, you can use the @ViewBuilder attribute instead of the result builder.

Updated on: 28-Feb-2023

990 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements