<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/little-swiftui-pitfall-i-fall-in-state-initial-value-9lrdy8v2a" -->

---
title: Little SwiftUI Pitfall I Fall In: State Initial Value
description: A common SwiftUI gotcha is explained: assigning a default value directly to a @State property while also setting it inside init() can silently fail, because...
canonical: https://daily.dev/posts/little-swiftui-pitfall-i-fall-in-state-initial-value-9lrdy8v2a
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Little SwiftUI Pitfall I Fall In: State Initial Value | daily.dev
og:description: A common SwiftUI gotcha is explained: assigning a default value directly to a @State property while also setting it inside init() can silently fail, because...
og:url: https://daily.dev/posts/little-swiftui-pitfall-i-fall-in-state-initial-value-9lrdy8v2a
og:image: https://api.daily.dev/og/posts/9LRdy8v2A.png
og:image:alt: Little SwiftUI Pitfall I Fall In: State Initial Value
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Little SwiftUI Pitfall I Fall In: State Initial Value

**[Medium](https://daily.dev/sources/medium_js)** · 2 min read · 0 upvotes · 0 comments

## Summary

A common SwiftUI gotcha is explained: assigning a default value directly to a @State property while also setting it inside init() can silently fail, because directly setting the stored property backing @State in init doesn't properly initialize the underlying State storage. The fix is to use State(initialValue:) explicitly via the underscored property (e.g. self._topItem = State(initialValue: 20)) instead of assigning to the property name directly. The post also highlights a subtler variant with Optional @State properties, where forgetting that Optionals default to nil caused a scrollPosition binding to silently not scroll to the intended item on appear, since the optional was technically 'initialized' to nil despite looking unset.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://medium.com/@itsuki.enjoy/little-swiftui-pitfall-i-fall-in-state-initial-value-46f5f959cb74>

## Questions this post answers

### Why doesn't setting a SwiftUI @State property directly inside init() work correctly?

Assigning directly to a @State property's name inside init() can fail to properly set the initial value, because it doesn't go through the State property wrapper's storage initialization. The reliable fix is to assign to the underscored backing property using State(initialValue:), for example self._allItems = State(initialValue: [1,2,3,4]), instead of self.allItems = [1,2,3,4].

_Developers debugging SwiftUI state quirks like this one often compare notes and fixes on daily.dev._

### Why is my SwiftUI scrollPosition not scrolling to the correct item on appear even though I set an Optional @State property in init?

An Optional @State property defaults to nil automatically, so directly assigning it in init (rather than through State(initialValue:)) leaves it effectively nil even if the assignment looks correct, because Optionals are already 'initialized' by default. The fix is to explicitly use self._topItem = State(initialValue: 20) so the underlying State storage actually updates.

_Anyone chasing subtle SwiftUI scroll-position bugs can find similar debugging stories on daily.dev._

## Similar posts on daily.dev

- [Why Child @State Won't Update from Parent in SwiftUI](https://daily.dev/posts/why-child-state-won-t-update-from-parent-in-swiftui-o7btsoihp) · Fatbobman · 0 upvotes · 0 comments
- [LazyState 1.0: Now available to everyone](https://daily.dev/posts/lazystate-1-0-now-available-to-everyone-q0wi3l6dm) · Point-Free Pointers · 0 upvotes · 0 comments

---

Tags: [#ios](https://daily.dev/tags/ios), [#swift](https://daily.dev/tags/swift), [#swiftui](https://daily.dev/tags/swiftui)

[View this post on daily.dev](https://daily.dev/posts/little-swiftui-pitfall-i-fall-in-state-initial-value-9lrdy8v2a)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Little SwiftUI Pitfall I Fall In: State Initial Value","url":"https://daily.dev/posts/little-swiftui-pitfall-i-fall-in-state-initial-value-9lrdy8v2a","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/little-swiftui-pitfall-i-fall-in-state-initial-value-9lrdy8v2a"},"datePublished":"2026-09-16T02:23:00.715Z","dateModified":"2026-09-16T02:36:09.587Z","description":"A common SwiftUI gotcha is explained: assigning a default value directly to a @State property while also setting it inside init() can silently fail, because...","image":"https://media.daily.dev/image/upload/s--P4t4XyoV--/f_auto/v1722860399/public/Placeholder%2001","thumbnailUrl":"https://media.daily.dev/image/upload/s--P4t4XyoV--/f_auto/v1722860399/public/Placeholder%2001","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/little-swiftui-pitfall-i-fall-in-state-initial-value-9lrdy8v2a","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"ios,swift,swiftui","timeRequired":"PT2M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"Little SwiftUI Pitfall I Fall In: State Initial Value"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/little-swiftui-pitfall-i-fall-in-state-initial-value-9lrdy8v2a#faq","mainEntity":[{"@type":"Question","name":"Why doesn't setting a SwiftUI @State property directly inside init() work correctly?","acceptedAnswer":{"@type":"Answer","text":"Assigning directly to a @State property's name inside init() can fail to properly set the initial value, because it doesn't go through the State property wrapper's storage initialization. The reliable fix is to assign to the underscored backing property using State(initialValue:), for example self._allItems = State(initialValue: [1,2,3,4]), instead of self.allItems = [1,2,3,4]. Developers debugging SwiftUI state quirks like this one often compare notes and fixes on daily.dev."}},{"@type":"Question","name":"Why is my SwiftUI scrollPosition not scrolling to the correct item on appear even though I set an Optional @State property in init?","acceptedAnswer":{"@type":"Answer","text":"An Optional @State property defaults to nil automatically, so directly assigning it in init (rather than through State(initialValue:)) leaves it effectively nil even if the assignment looks correct, because Optionals are already 'initialized' by default. The fix is to explicitly use self._topItem = State(initialValue: 20) so the underlying State storage actually updates. Anyone chasing subtle SwiftUI scroll-position bugs can find similar debugging stories on daily.dev."}}]}
```

