Post

Replies

Boosts

Views

Activity

Reply to SwiftUI Binding property from view model to a value outside of that view model
I made a mistake in my answer. The @ObservedObject property wrapper is for SwiftUI views. You can't use it in a view model. The following declaration: @ObservedObject var dataManager = DataManager() Should be something like @Published var dataManager = DataManager() I am not sure what you are asking in your second response. You described what you are trying to do but didn't ask a question.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’24
Reply to SwiftUI Binding property from view model to a value outside of that view model
Bindings are for SwiftUI views. Using @Binding in a view model isn't going to work. Creating a binding like Binding<Bool> in a view model isn't going to work either. For passing data from a view model to a SwiftUI view, you are on the right track with the following code in ContentViewModel: class ContentViewModel: ObservableObject { @ObservedObject var dataManager = DataManager() @Published var isOn: Bool = false } Have the view model conform to ObservableObject. Use @Published for any properties where you want the view to update when the property's value changes. The view that owns the view model uses the @StateObject property wrapper, like you have in your content view. @StateObject var viewModel = ContentViewModel() Use the @ObservedObject property wrapper to pass the view model from the content view to other views. I am not sure why the content view model needs its own isOn property. Can't it use the data manager's isOn property?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’24
Reply to .playground to .swiftpm
For the Swift Student Challenge, either create an App playground in the Swift Playgrounds app or create a Swift Playgrounds App project in Xcode. That will give you a playground or project that meets the challenge's eligibility requirements. https://developer.apple.com/swift-student-challenge/eligibility/
Feb ’24
Reply to Developing with an older Mac
Starting April 29 Apple is going to require Xcode 15 for App Store submissions. Xcode 15 requires macOS 13+. https://developer.apple.com/news/?id=fxu2qp7b The chances are high you won't be able to submit apps with that MacBook Air. I recommend buying a Mac with an ARM processor. ARM Macs are going to be supported longer than Intel Macs.
Topic: App & System Services SubTopic: Core OS Tags:
Feb ’24
Reply to URL (via .fileImporter) to a ReadConfiguration - How to?
The .fileImporter modifier isn't for opening your SwiftUI app's documents. It's for opening other types of files in your app. The document struct's init that takes a ReadConfiguration is for opening your app's documents, either from a document picker (iOS) or by choosing File > Open (Mac). Instead of using your document struct's init that takes a ReadConfiguration, write a function to process the JSON that takes a URL as an argument. Pass the URL the file importer gives you to the JSON processing function.
Topic: App & System Services SubTopic: Core OS Tags:
Feb ’24
Reply to support need for old swift Vs new swift
I'm trying to follow STORMVIEWS tutorial which was made with an old version of swift were viewed load was still available The viewDidLoad method is still available. Your problem is that you are using SwiftUI to follow a tutorial that uses UIKit. Start over by creating a UIKit project. Take the following steps to create a UIKit project in Xcode: In Xcode choose File > New > Project. Select iOS from the platforms at the top of the New Project Assistant. Select the App template from the list of iOS app templates. Click the Next button. Choose Storyboard from the Interface menu. Read the following article for more details and a screenshot: https://www.swiftdevjournal.com/xcode-11-missing-view-controllers/
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’24
Reply to Could not locate device support files
If the solutions in the following article don't work: https://www.swiftdevjournal.com/dealing-with-failed-to-prepare-device-for-development-error-message-in-xcode/ You won't be able to run and debug the project on your iPhone running iOS 17.3. You have the following options: Buy a new Mac. Use Open Core Legacy Patcher to update your current Mac to the latest version of macOS so you can use Xcode 15. Use an iOS device running iOS 16 or earlier to run your project.
Feb ’24
Reply to Xcode Showing Build Failed
The most likely cause of your problem is you are trying to place and run multiple small programs in one Xcode project. Each program has its own main function, but a C program can have only one main function. If you add three files to your project, intending each file to be its own program, and try to run the project, Xcode doesn't know you want to run only one of the files. It assumes you want to run all the files, sees you have three main functions, and generates a link error. Xcode is designed to have one program per project, no matter how small the program is. If you are learning C and writing multiple small programs, you have the following options: Create one Xcode project for each program. Create one Xcode project and add a target for each program. Use the jump bar in the Xcode toolbar above the code editor to choose the target to run. Create one Xcode project and use the target membership checkboxes on the right side of the project window to tell Xcode which files/programs you want to build and run. Use a text editor like VS Code, TextMate, or BBEdit to write your code instead of Xcode. I know TextMate comes with a bundle to run your programs from inside the editor. VS Code probably has an extension that allows this as well.
Feb ’24
Reply to support need for old swift Vs new swift
Your first example uses SwiftUI. Every SwiftUI view requires a body. var body: some View { // Code inside the body removed. } The body property must return a SwiftUI view. The code in your body property does not have a SwiftUI view in it so you are going to get an error when you build the project. You should move all the code you have in body into a separate function. I also recommend going through a book or course to learn SwiftUI. The site Hacking with Swift has a free 100 day course that teaches SwiftUI. https://www.hackingwithswift.com/100/swiftui
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’24
Reply to Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
The most likely cause of the error is that the outlets are not connected in the storyboard. Use the connections inspector for the storyboard to check the connections. Open your storyboard in Xcode and choose View > Inspectors > Connections to open the connections inspector. You may find the following article helpful: https://www.swiftdevjournal.com/fixing-the-thread-1-fatal-error-unexpectedly-found-nil-while-implicitly-unwrapping-an-optional-value-error/
Topic: UI Frameworks SubTopic: UIKit Tags:
Jan ’24
Reply to How to add an object to the screen via the add button
I can help you with adding a product to the cart from the sheet. The CartManager has the function to add a product to the cart. To add a product in your sheet, add a property for the cart manager to ProductDetailSheet. Because CartManager is a class, you should use the @ObservedObject property wrapper for the cart manager. @ObservedObject var cart: CartManager You have not shown the code for the view that opens the sheet. But in that sheet you must add a property with the @StateObject property wrapper for the cart manager. @StateObject var cart: CartManager Pass the cart to the sheet. When someone taps the Add button, call the addToCart function to add the product to the cart. Button("Add") { cart.addToCart(product: product) isSheetPresented = nil } I am not sure if this is enough to get the product adding working, but it should give you a start.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’24