Skip to content

[SR-10859] Private(set) property can be set from outside via Binding #53249

@swift-ci

Description

@swift-ci
Previous ID SR-10859
Radar None
Original Reporter ngocluu (JIRA User)
Type Bug
Environment

Swift 5.1. Xcode 11 Beta (11M336w).

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, PropertyWrappers
Assignee None
Priority Medium

md5: c9c1759159a05fd13bb298b4803f1838

Issue Description:

BindableObject class:

import SwiftUI
import Combine

final class UserData: BindableObject  {
    let didChange = PassthroughSubject<Void, Never>()
    
    // PRIVATE SET
    private(set) var showFavoritesOnly = false {
        didSet {
            print("CHANGED")
            didChange.send(())
        }
    }
}

SwiftUI view:

struct ContentView : View {
    @EnvironmentObject var userData: UserData
    
    var body: some View {
        VStack {
            Toggle(isOn: $userData.showFavoritesOnly) {
                Text("Favorites only")
            }
            Button(action: toggle) {
                Text("Change")
            }
        }
    }
    
    func toggle() {
        $userData.showFavoritesOnly.value.toggle()
    }
}

The property of BindableObject is private(set), but it can be set from the view via Binding.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfproperty wrappersFeature: property wrappers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions