Sitemap
Meet Shah

Meet Shah

iOS Developer | Passionate about building intuitive and innovative mobile experiences. #EngineerByProfession #DeveloperByHeart

SwiftUI’s Best Kept Secret: The some Keyword 🔑

4 min readJan 8, 2025

--

When you start developing apps with SwiftUI, one keyword you’ll encounter very early is some. This magical little word might seem confusing at first, but it’s actually a game-changer that makes SwiftUI elegant, powerful, and fun to use.

👉 We all use some for creating SwiftUI views, but do we know why the some keyword is playing around here?

In this blog, I’ll break down what some does, why it’s essential, and how it helps you build beautiful apps with ease. Whether you're a Swift newbie or just dipping your toes into iOS development, this is for you! 🧑‍💻📱

What is some? 🤔

The some keyword is Swift’s way of saying:

I’ll give you a specific type that conforms to a protocol, but you don’t need to know exactly what that type is.

It’s like handing someone a gift-wrapped box. They don’t need to know what’s inside, but they trust that it’s exactly what they need.

For example:

var body: some View {
Text("Hello, SwiftUI!")
}

In this case, Swift knows body returns something that conforms to the View protocol, but the exact type (here, Text) is hidden from the outside world. This helps simplify your code while keeping things flexible.

But Why Do We Need some?

Press enter or click to view image in full size
Photo by TERRA on Unsplash

To understand the some keyword, let’s look at the problems it solves:

1. Protocols Without some

In Swift, you can’t directly return a protocol type like this:

var body: View {  // ❌ Error: Protocol 'View' can only be used as a generic constraint
Text("Hello!")
}

Why? Because protocols like View are not concrete types. Swift doesn’t know how much memory to allocate or what the exact structure of the type is.

2. Concrete Types Limit Flexibility

If you return a specific type like Text, your code becomes less flexible:

var body: Text {
Text("Hello!")
}

This works, but what if you later want to change the body to include an Image or a Button? You’d have to rewrite the return type every time. 🙄

Using some 🌟

With some, Swift can work with a specific type internally while hiding it from the outside:

var body: some View {
Text("Hello!")
}

Now, Swift knows the exact type internally but treats it as any View to the caller. You get both type safety and flexibility.

How some is Used in SwiftUI️?

SwiftUI uses the some keyword extensively, especially for views. Every SwiftUI view (like Text, Image, Button, etc.) conforms to the View protocol. When you create a view, you use some View to define its body.

Get Meet Shah’s stories in your inbox

Join Medium for free to get updates from this writer.

Here’s an example of a simple view:

struct ContentView: View {
var body: some View {
VStack {
Text("Welcome to SwiftUI!")
Button("Tap Me", action: buttonTapped)
}
}

func buttonTapped() {
print("Button was tapped!")
}
}

Let’s break it down:

  • body: This is the content of your view. It must return a single thing that conforms to View.
  • some View: You’re telling Swift, “I’ll return a specific view, but I don’t want to specify the exact type.”

This works beautifully because:

  1. You can mix and match different views (Text, Button, etc.).
  2. You can build complex UIs without worrying about the exact return type.

When to Use some? 🤷‍♂️

You’ll use some whenever you want to:

  • Hide Implementation Details: Keep the exact type private while exposing its behavior.
protocol Shape {
func draw()
}

struct Circle: Shape {
func draw() { print("Drawing a Circle") }
}

struct Square: Shape {
func draw() { print("Drawing a Square") }
}

func createShape() -> some Shape {
Circle()
}

The caller knows they’ll get a Shape, but they don’t know it’s a Circle.

  • Build UIs in SwiftUI: This is the most common use case. Every body in SwiftUI requires some View.

Benefits of some in SwiftUI 🚀

  1. Flexibility: Easily change your view’s structure without changing its type.
  2. Type Safety: Swift still knows the exact type internally, so you avoid runtime errors.
  3. Cleaner Code: Focus on building UIs without worrying about the technical details.

Conclusion 🏁

The some keyword is one of the reasons SwiftUI is so powerful and easy to use. It allows you to build dynamic, flexible UIs while keeping your code safe and efficient. For a beginner, understanding some might feel tricky at first, but with practice, you’ll see how it makes SwiftUI development a breeze.

So next time you see some View, remember—it’s Swift’s way of saying, “Don’t worry, I got this!” 🎉

--

Meet Shah
Meet Shah

Written by Meet Shah

iOS Developer | Passionate about building intuitive and innovative mobile experiences. #EngineerByProfession #DeveloperByHeart