Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am able to use StateObject in ios 13. Is that a bug or is it real?

Tags:

swiftui

StateObject

In the past, StateObject was only available in ios 14, but all of a sudden I can use it in ios 13.

However, I can't find anywhere that StateObject is available in ios 13. Even when I go to its definition, it says ios 14.

I am using xcode 13.2

like image 366
Arutyun Enfendzhyan Avatar asked Oct 31 '25 22:10

Arutyun Enfendzhyan


1 Answers

Update February 19th 2023

This appears to now be fixed in Xcode 14.3 Beta 1. When adding @StateObject a compilation error now occurs and a warning is displayed in Xcode stating:

'StateObject' is only available in iOS 14.0 or newer.

Add @available attribute to enclosing struct

enter image description here

Update July 14th 2022

I have checked to see if this is still occurring on Xcode 14 Beta 3 and unfortunately it is.

I have updated my feedback to Apple stating this.

Update June 28th 2022

Apple replied to my feedback that I submitted for this issue, asking if it still occurs while using Xcode 14 Beta 2, the unfortunate answer is yes. The issue is still occurring.

I have updated my feedback with the additional information they requested and hopefully they will be able to fix the lack of error produced when using StateObject in an iOS 13 project.

Original Answer Dec 2021

I think that this is a bug in the compiler.

Taking a very simple example application:

class ViewModel: ObservableObject {
    init() {}
}

struct ContentView: View {

    @StateObject var viewModel = ViewModel()

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

With the Targets Deployment target set to 13.0

enter image description here

and the Projects Deployment target also set to 13.0

enter image description here

If I build for an iPhone 13 on iOS 15 the application compiles and builds without a warning, and runs without issue.

However, if I build for an iPhone 6S on iOS 13 the application compiles and builds without a warning but when it runs it produces the following error:

dyld: lazy symbol binding failed: Symbol not found: _$s7SwiftUI11StateObjectV12wrappedValueACyxGxyXA_tcfC
  Referenced from: /Users/andrew/Library/Developer/CoreSimulator/Devices/F12BA8CD-671F-4EA2-94E4-784DA2EE1A1C/data/Containers/Bundle/Application/0A6FA610-FF19-47EB-A062-4AA7A296F464/Thirteen.app/Thirteen
  Expected in: /System/Library/Frameworks/SwiftUI.framework/SwiftUI

dyld: Symbol not found: _$s7SwiftUI11StateObjectV12wrappedValueACyxGxyXA_tcfC
  Referenced from: /Users/andrew/Library/Developer/CoreSimulator/Devices/F12BA8CD-671F-4EA2-94E4-784DA2EE1A1C/data/Containers/Bundle/Application/0A6FA610-FF19-47EB-A062-4AA7A296F464/Thirteen.app/Thirteen
  Expected in: /System/Library/Frameworks/SwiftUI.framework/SwiftUI

CoreSimulator 783.5 - Device: iPhone 6s (F12BA8CD-671F-4EA2-94E4-784DA2EE1A1C) - Runtime: iOS 13.0 (17A577) - DeviceType: iPhone 6s

And highlights the problem in Xcode

enter image description here

I would expect that the application should not compile and build if it is using an API that is only available for a newer SDK.

like image 190
Andrew Avatar answered Nov 04 '25 07:11

Andrew