Post

Replies

Boosts

Views

Activity

Thread Error with @Model Class
I have a @Model class that is comprised of a String and a custom Enum. It was working until I added raw String values for the enum cases, and afterwards this error and code displays when opening a view that uses the class: { @storageRestrictions(accesses: _$backingData, initializes: _type) init(initialValue) { _$backingData.setValue(forKey: \.type, to: initialValue) _type = _SwiftDataNoType() } get { _$observationRegistrar.access(self, keyPath: \.type) return self.getValue(forKey: \.type) } set { _$observationRegistrar.withMutation(of: self, keyPath: \.type) { self.setValue(forKey: \.type, to: newValue) } } } Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1cc165d0c) I removed the String raw values but the error persists. Any guidance would be greatly appreciated. Below is replicated code: @Model class CopingSkillEntry { var stringText: String var case: CaseType init(stringText: String, case: CaseType) { self.stringText = stringText self.case = case } } enum CaseType: Codable, Hashable { case case1 case case1 case case3 }
2
0
185
3w
Implementing multiple Model Containers
Hi, When using just one model container, I have the following code: let modelContainer: ModelContainer init() { do { entriesModel = try ModelContainer(for: Model.self) } catch { fatalError("Could not initialize ModelContainer") } } I am attempting to implement two like so: let model1: ModelContainer let model2: ModelContainer init() { do { model1 = try ModelContainer(for: Model1.self) model2 = try ModelContainer(for: Model2.self) } catch { fatalError("Could not initialize ModelContainer") } } var body: some Scene { WindowGroup { ContentView() .modelContainer(model1) .modelContainer(model2) } } However, when in the Views that I'd like to implement I don't know how to declare the models. I tried this: @Environment(\.model1) private var model1 @Query private var data: [Model1] But I get the error: Cannot infer key path type from context; consider explicitly specifying a root type How do I implement multiple model containers in multiple different views?
2
0
398
4w
AppStorage extension for Bool Arrays
Hi, As AppStorage does not support arrays, I found an extension online that allows for handling arrays of strings with AppStorage. However, in my use case, I need to handle arrays of boolean values. Below is the code. Any help would be greatly appreciated. extension Array: RawRepresentable where Element: Codable { public init?(rawValue: String) { guard let data = rawValue.data(using: .utf8), let result = try? JSONDecoder().decode([Element].self, from: data) else { return nil } self = result } public var rawValue: String { guard let data = try? JSONEncoder().encode(self), let result = String(data: data, encoding: .utf8) else { return "[]" } return result } }
1
0
280
Aug ’24
Condensing HTTP request functions
Hi, For HTTP requests, I have the following function: func createAccount(account: Account, completion: @escaping (Response) -> Void) { guard let url = URL(string: "http://localhost:4300/account") else { return } var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") guard let encoded = try? JSONEncoder().encode(account) else { print("Failed to encode request") return } request.httpBody = encoded URLSession.shared.dataTask(with: request) { (data, response, error) in guard let data = data else { return } do { let resData = try JSONDecoder().decode(Response, from: data) DispatchQueue.main.async { completion(resData) } } catch let jsonError as NSError { print("JSON decode failed: \(jsonError)") } }.resume() } Because various HTTP requests use different body structs and response structs, I have a HTTP request function for each individual HTTP request. How can I condense this into one function, where the passed struct and return struct are varying, so I can perhaps pass the struct types in addition to the data to the function? Any guidance would be greatly appreciated.
0
0
192
Aug ’24
.onAppear print not working
Hi, I have the following code: struct Loading: View { var body: some View { Text("Loading...") .onAppear { print("test") } } } The view is on the screen, however the print statement doesn't run. Why would this be? Any help would be greatly appreciated.
1
0
270
Aug ’24
Undefined symbol and linker command failed errors
Hi, I was searching and replacing code where all of the sudden I got the following errors: Undefined symbol: AppName.Client.init(id: Swift.Int, name: Swift.String, StructVariable: Swift.Int?, structVariable: [Struct]?) -> AppNameStruct Linker command failed with exit code 1 (use -v to see invocation) I replaced the names in the first error message for privacy reasons. I tried quitting Xcode and restarting my computer but the error persisted. Any help on resolving this would be greatly appreciated.
1
0
420
Jul ’24
Efficiently accounting for different iOS versions
I one project, using .onChange(), I get an error: 'onChange(of:initial:_:)' is only available in iOS 17.0 or newer The solution provided is to wrap the code in if #available(iOS 17.0, *) { }, but I'm only able to wrap the whole view inside of it, like so: if #available(iOS 17.0, *) { VStack () { // view with .onChange } } else { VStack () { // view with older solution } } The view is quite lengthy so it would be very redundant to have the same view twice, (the same view for each component of the if/else statement). Is there a way to implement an if statement so that I would only need the code for the view once (something like the code below)? VStack { } if #available(iOS 17.0, *) { .onChange() { } } else { // older solution } Additionally, in a newer project, I don't have to include the if #available(iOS 17.0, *) { }, so I'm guessing the build is made for a newer iOS. If that's the case, would it not be compatible with older iOS versions? Any help would be greatly appreciated.
3
0
425
Jul ’24
Incorrect JSON Format with JSONEncoder
Hi, I have a struct that I'd like to encode with JSON and send in the body of an HTTP request: struct AccountCreationData: Codable, Hashable { var name: String var email: String var phone: String var password: String private enum CodingKeys: String, CodingKey { case name case email case phone case password } } I encode it with this code and set the HTTP request body to the encoded data: guard let encoded = try? JSONEncoder().encode(account) else { print("Failed to encode request") return } request.httpBody = encoded But in my backend, when I receive the data from the HTTP request, the JSON data is formatted as such: '{"phone":"0000000000", "password":"password", "email":"email",", "name":"name"}': ''} Instead of: { "phone": "0000000000", "password":"password" "email":"email" "name":"name" } So the JSON parser in my backend isn't properly parsing the data because of the format that the JSON is in. I was wondering if this is an issue with the way I'm encoding on the front end (Swift) or my backend. I have similar projects using the same code to encode the data so I'm unsure as to what the issue is. Any help would be greatly appreciated. Thank you.
6
0
428
Jul ’24
Most efficient way to call a function upon variable state change
Hi, I have a button view that is used in many different ways in an app that I'm working on. One of the ways it is used is in an account creation form. Since the button is in a child view, the action: { } button function isn't available in the view where the input field variables are. I could import the class with the function to the button view but I don't want to pass the input field variables into the button view. I tried binding a boolean variable to the button view and checked for it's state change in the parent view using .onChange(), but the use case for that I found was depreciated and I'm unable to revert the state of the variable in the .onChange function. To reiterate, in a main view, I need to call a function in a given class and pass variables to it, upon a button being pressed in a child view. Any help would be greatly appreciated.
2
0
518
Jul ’24
RFID connection between iPhones
Hi, I'm inquiring in regards to the feasibility or possibility of making a connection from iPhone to another, such that one iPhone uses Apple wallet, and another iPhone would detect for the signal within the app. Please let me know if this is feasible or if any more information would be necessary to assist. If this is possible, what documentation would be relevant? Thank you.
1
0
394
Jul ’24
Passing binding variable to function with optional parameter
Hi, I have a view that takes an optional binding boolean variable. I'd like to pass a variable to it from certain views only. The problem I'm having is when I pass a variable, I get the error "Cannot convert value of type 'Binding' to expected argument type 'Binding<Bool?>'", and when I don't, I get the error "Missing argument for parameter 'toggle' in call". Below Is the code I have. Thank you. Heading(text: "Heading1") // error: Missing argument for parameter 'toggle' in call Heading(text: "Heading2", toggle: $newEntry) // Cannot convert value of type 'Binding<Bool>' to expected argument type 'Binding<Bool?>' struct Heading: View { @State var text: String @Binding var toggle: Bool? var body: some View { VStack { Spacer() .frame(height: 25) HStack { Text(text) .font(.system(size: 36)) Spacer() if text == "str" { Button(action: { toggle!.toggle() }) { Image(systemName: "plus") .foregroundColor(.black) .font(.system(size: 36)) } } } Spacer() .frame(height: 25) } } }
0
0
538
Apr ’24
Getting started with App Store Server API and programmatically cancelling a user's subscription
Hello, I'm inquiring in regard to information on handling user's subscriptions, specifically if it's possible to cancel a subscription with a line of code. For example: if condition { // cancel user's subscription } I'm not familiar with the App Store Server API yet, but I glanced at the documentation and I didn't see any information in regard to the question I have. That being said, I'm also wondering where to start with configuring a subscription model for my app. Any insights would be greatly appreciated. Thank you.
1
0
687
Feb ’24