Post

Replies

Boosts

Views

Activity

Reply to how do I convert my data to JSON format - health app
I tried to complement your code with missing parts. To make it work, you need to declare all "value" including sys and did as optional. Here is some code to show this running (in playground): let heartJSON = """ ["BloodPressure:" { "endDate" : "2020-01-25", "systolicValue" : "122", "diastolicValue" : "62" "startDate" : "2020-01-25" }, "HeartRate:" { "endDate" : "2020-01-25", "Value" : "78", "startDate" : "2020-01-25" }, "BMI:" { "endDate" : "2020-01-25", "Value" : "23", "startDate" : "2020-01-25" } ] } """ enum HealthDataType: String, Codable { case bloodPressure = "BloodPressure" case heartRate = "HeartRate" case bmi = "BMI" } struct HealtDataItem: Codable { let endDate: Date let value: Double? let systolicValue : Double? let diastolicValue : Double? let startDate: Date let type: HealthDataType } let end = Date() let bmiValue = 12.0 let start = Date() let bloodPressureItem = HealtDataItem(endDate: end, value: nil, systolicValue : 8.0, diastolicValue: 12.0, startDate: start, type: .bloodPressure) let bmiItem = HealtDataItem(endDate: end, value: bmiValue, systolicValue : nil, diastolicValue: nil, startDate: start, type: .bmi) let healthData = [bloodPressureItem, bmiItem] do { let data = try JSONEncoder().encode(healthData) let jsonDecoder = JSONDecoder() do { let decodedData = try jsonDecoder.decode([HealtDataItem].self, from: data) for extracted in decodedData { print(extracted.type, extracted.startDate, extracted.value ?? "-", extracted.systolicValue ?? "-", extracted.diastolicValue ?? "-") } } catch { print(Decoding error") } } catch { //error handling } Which gives (take care, values are totally phony): bloodPressure 2021-04-16 13:36:43 +0000 - 8.0 12.0 bmi 2021-04-16 13:36:43 +0000 12.0 - - You could avoid the need to declare all the missing values with: struct HealtDataItem: Codable { let endDate: Date var value: Double? = nil var systolicValue : Double? = nil var diastolicValue : Double? = nil let startDate: Date let type: HealthDataType } let end = Date() let bmiValue = 12.0 let start = Date() let bloodPressureItem = HealtDataItem(endDate: end, systolicValue : 8.0, diastolicValue: 12.0, startDate: start, type: .bloodPressure) let bmiItem = HealtDataItem(endDate: end, value: bmiValue, startDate: start, type: .bmi) let healthData = [bloodPressureItem, bmiItem] do { let data = try JSONEncoder().encode(healthData) let jsonDecoder = JSONDecoder() do { let decodedData = try jsonDecoder.decode([HealtDataItem].self, from: data) for extracted in decodedData { print(extracted.type, extracted.startDate, extracted.value ?? "-", extracted.systolicValue ?? "-", extracted.diastolicValue ?? "-") } } catch { } } catch { //error handling } Which gives: bloodPressure 2021-04-16 13:50:45 +0000 - 8.0 12.0 bmi 2021-04-16 13:50:45 +0000 12.0 - -
Topic: App & System Services SubTopic: General Tags:
Apr ’21
Reply to ios 14.3 battery drain
This forum is not the right place. It is not an official Apple support site. It is for developers (your question is not a developer's question). There are many other forums posts about this issue. Such as: https://discussions.apple.com/thread/252151864 You'd close this thread and contact Apple Support directly.
Topic: App & System Services SubTopic: Core OS Tags:
Apr ’21
Reply to Missing bracket??
Not error with the exact same code. Did you get the error message in code but did code compile normally ? If so, that's just a previous error that was not purged (clean build folder let clear all those annoying cached errors).
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Retrospective connection?
Not very clear what is the setup of all the views. My best guess: I assume the exploreViewController is in the navigation stack of navigation controller if so, you should probably reconnect the exploreViewController from the navigation controller. ControlDrag from the navigation controller (from its content of VC in IB) to exploreViewController and select Relationship segue: Rootview controller That will make exploreViewController the first VC in navigation stack.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Missing bracket??
Yes, exiting and reopening may clear it. Anyway, remember the option-clean build folder command any time you have some weird behaviour. It takes no time and may save a lot. Good continuation, and don't forget to close the thread.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to App Store Free Trials for Paid Apps Mechanics
At first I hoped this "Free trial" was a great solution for professional apps which cost more than a few bucks… But considering all the constraints that seem to be a poor solution. Is it authorised to have a free, unlimited in time demo version (of course limited in some aspects such as the size of the "problem" it can solve) available outside appstore ? That would be a much simpler mechanism.
Apr ’21
Reply to Assistant not opening the correct swift file
If you have file editor on the left and storyboard on the right: select the VC tou want in IB click on the 3rd icon at top (the one with 4 small squares) In the popup menu, on Automatic line, you have your VC in Top level objects, you have the VC and another .h file If you have only storyboard opened: select the VC in IB Assistant command should open the file.swift if not, click on the link icon (2 small intertwined circles) which should probably say Top level objects Then select Automatic in the popup menu.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21