<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Busra Gunes on Medium]]></title>
        <description><![CDATA[Stories by Busra Gunes on Medium]]></description>
        <link>https://medium.com/@busraaguunes?source=rss-ce1d4928d8aa------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nl4V9DhiFiihdZJTIEiEJg.jpeg</url>
            <title>Stories by Busra Gunes on Medium</title>
            <link>https://medium.com/@busraaguunes?source=rss-ce1d4928d8aa------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 03:19:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@busraaguunes/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[SwiftUI: Property Wrappers]]></title>
            <link>https://medium.com/@busraaguunes/swiftui-property-wrappers-a76e69f91b83?source=rss-ce1d4928d8aa------2</link>
            <guid isPermaLink="false">https://medium.com/p/a76e69f91b83</guid>
            <category><![CDATA[switui]]></category>
            <category><![CDATA[property-wrapper]]></category>
            <category><![CDATA[state]]></category>
            <category><![CDATA[binding]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Busra Gunes]]></dc:creator>
            <pubDate>Tue, 27 Jun 2023 06:45:25 GMT</pubDate>
            <atom:updated>2023-07-07T12:17:47.397Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*J2R95PZjRJ08vPIsg90pJQ.png" /></figure><p>SwiftUI is a powerful framework used for creating user interfaces for iOS, macOS, and other Apple platforms. It offers a declarative approach and includes many features that make data management in your applications easier. One of these features is a structure called “property wrappers.” In this article, we will explore how to use property wrappers in SwiftUI and how they can enhance data management.</p><p>In SwiftUI, property wrappers can be particularly useful for managing state, observing changes, and validating data. They allow you to define properties with additional functionality without cluttering your view code. Let’s take a look at a few examples.</p><p><strong>Frequently Used Property Wrappers</strong></p><ol><li><strong>@ State</strong></li><li><strong>@ Binding</strong></li><li><strong>@ ObservedObject</strong></li><li><strong>@ EnvironmentObject</strong></li><li><strong>@ FetchRequest</strong></li><li><strong>@ StateObject</strong></li><li><strong>@ Environment</strong></li></ol><p><strong>@State:</strong></p><p><strong>@State</strong> is used in SwiftUI to manage the state of a variable. It is used within a view’s scope and is commonly used for sharing data between views. A variable marked with @State is automatically monitored by SwiftUI, and the view is redrawn whenever its value changes. For example, in the following code, a counter value is maintained using the<strong> @State</strong> property wrapper:</p><pre>struct ContentView: View {<br>    @State private var counter = 0<br>    <br>    var body: some View {<br>        VStack {<br>            Text(&quot;Counter: \(counter)&quot;)<br>            Button(&quot;Increment&quot;) {<br>                counter += 1<br>            }<br>        }<br>    }<br>}</pre><p>In the above code, the counter variable is marked with @State, allowing SwiftUI to track its changes. When the &quot;Increment&quot; button is tapped, the counter value is increased, and SwiftUI automatically updates the view to reflect the new value.</p><p><strong>@Binding:</strong></p><p>The concept of <strong>@Binding </strong>in the Swift programming language refers to connecting or associating one value with another. It enables two-way synchronization of values, meaning that when one value is changed, the other value is automatically updated.</p><p>Binding is commonly used to establish a connection between user interface components and data models. For example, the value of a text field in a user interface can be bound to a variable in a data model, and they can be synchronized in both directions.</p><pre>import SwiftUI<br><br>struct ContentView: View {<br>    @State private var username: String = &quot;&quot;<br><br>    var body: some View {<br>        VStack {<br>            TextField(&quot;Username&quot;, text: $username)<br>            Text(&quot;Entered username: \(username)&quot;)<br>        }<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kTUU1hpZ88jZtYnUKJ2Xwg.png" /></figure><p>In this example, a String variable named “username” is defined. The @State attribute indicates that this variable is a state variable, meaning that as its value changes, the user interface is automatically refreshed.</p><p>TextField is a component that allows the user to enter text. The text parameter connects the text entered into the TextField to the username variable. So, if the user enters some text, the username value is automatically updated.</p><p>The Text component is used to display the username. The expression $username indicates that there is a connection to the username variable. Therefore, when the username value changes, the text is automatically updated.</p><p>In this code, there is a binding relationship between the TextField and Text components. This allows the text entered by the user to be automatically reflected in the Text component.</p><p><strong>@ObservedObject:</strong></p><p>@ObservedObject is used to work with externally observed objects. When an object is marked with @ObservedObject property wrapper, SwiftUI automatically detects changes to the object and updates the relevant views. It is commonly used in the MVVM (Model-View-ViewModel) architecture. For example, in the following code, a model class is observed using the <strong>@ObservedObject</strong> property wrapper:</p><pre>class UserModel: ObservableObject {<br>    @Published var name = &quot;John Doe&quot;<br>    @Published var age = 30<br>}<br><br>struct ContentView: View {<br>    @ObservedObject var user = UserModel()<br>    <br>    var body: some View {<br>        VStack {<br>            Text(&quot;Name: \(user.name)&quot;)<br>            Text(&quot;Age: \(user.age)&quot;)<br>        }<br>    }<br>}</pre><p>In the above code, the UserModel class is marked as an observed object using the @ObservedObject property wrapper. The ContentView observes changes to the user object. Whenever the name or age properties of the UserModel are modified, SwiftUI automatically updates the view to reflect the changes.</p><blockquote><em>ObservedObject can be used with classes only since ObservableObject protocol supports only class.</em></blockquote><p><strong>@EnvironmentObject:</strong></p><p>@EnvironmentObject is a property wrapper used to access shared data throughout an application. With this feature, data can be automatically passed from a higher level to all lower levels.</p><p>For example, we can use @EnvironmentObject to track the user session status within an application. Here&#39;s an example code:</p><pre>class UserAuth: ObservableObject {<br>    @Published var isLoggedIn = false<br>}<br><br>struct ContentView: View {<br>    @EnvironmentObject var userAuth: UserAuth<br>    <br>    var body: some View {<br>        if userAuth.isLoggedIn {<br>            // View for logged in user<br>            VStack {<br>                Text(&quot;User Sign In&quot;)<br>            }<br>        } else {<br>            // View for non logged in user<br>            VStack {<br>                Text(&quot;User is not logged in&quot;)<br>            }<br>        }<br>    }<br>}</pre><p>In the example above, the ContentView view injects the UserAuth object with the @EnvironmentObject attribute and accesses it with the userAuth property. This property can now be used inside the view as a local variable and accessed properties such as the<strong> userAuth.isLoggedIn</strong> property.</p><p>Finally, let’s add it to the App, that is, where the application is initialized, in order to share it in all views.</p><pre>@main<br>struct SampleSwiftUIApp: App {<br>    @StateObject var userAuth = UserAuth()<br>        <br>    var body: some Scene {<br>        WindowGroup {<br>            ContentView()<br>                .environmentObject(userAuth)<br>        }<br>    }<br>}</pre><p><strong>@FetchRequest</strong></p><p>It is used to retrieve data stored in Core Data of @FetchRequest Property Wrapper in SwiftUI.</p><pre>import SwiftUI<br>import CoreData<br><br>struct ContentView: View {<br>    @FetchRequest(entity: Task.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Task.createdAt, ascending: true)])<br>    var tasks: FetchedResults&lt;Task&gt;<br><br>    var body: some View {<br>        List(tasks, id: \.self) { task in<br>            Text(task.title ?? &quot;Untitled&quot;)<br>        }<br>    }<br>}</pre><p>In the above code, we fetch all the tasks from the database using the <strong>@FetchtRequest </strong>property. We specify which entity will be retrieved with the entity parameter, the sorting criteria are specified with the sortDescriptors parameter. Finally, a variable of type FetchedResults is created and named tasks.</p><blockquote><em>If you liked this article then please appreciate it with claps and comments. This will really encourage me to write more!!!!</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a76e69f91b83" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>