|
Hello, i'm trying to subtract a value with the next value inside an array and append the difference into another array, i can't figure out how to do. With this code i get this error "Type '()' cannot conform to 'View'". Thank you for the help!
|
|
Swift thinks you're trying to do your calculations as part of your SwiftUI view rendering, which isn't what you mean at all. It's better to pull that logic out into a computed property (for example), and leave your SwiftUI |
|
Hi The other thing is you will need to watch out for
Result
For your information |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
hi, sorry if i get a little nerdy on this, but ideally what you want to do is take the numbers array without the last entry and "coordinate-wise" subtract the numbers array without the first entry. so you would want to form a hypothetical minus operation:
the following functional Swift code does this using
hope that might be of interest to some ... DMG |
|
|
|
Thanks for your reply, are very useful. I have another problem. I'm trying to sort, in a list, where each row contains the number and the difference from the previous value. The screenshot shows the result I am trying to achieve. https://ibb.co/CBQBnmk Do you think this is a good solution?
|
|
hi, now that i see where you want to go with this, i put together something ... but then i was confused by when you said "I'm trying to sort ..." so let me show you what i put together, which is in essence, not much different than what you have (except that i don't bother to compute all the differences, but just those that need to be displayed).
hope that moves the conversation forward ... DMG |
|
I am Italian, I am trying to explain in English my problem as best I can. With all your suggestion, i've tryed in different ways but i'm not able to solve. The code above was a simplified example of my real code, because i was thinking to find a solution and then adapt to my code but this don't happen. So, at this point, i think is better to share my project code. What I am trying to achieve is to have for each weight misuration the difference with the previos value. Actualy i'am able to add and delete misurations, and are sorted by date. https://youtu.be/ItTDJiewmJQ Thank you for your patience. 🙏
|
|
hi, i see your problem ... you construct the
and so on each misuration (a weight, date, and notes) you want to display these values, but then get stuck on figuring out the difference from the pevious misuration because you don't have its index. i see perhaps two options to consider. (1) rewrite the
you should then be able to compute that difference as suggested earlier using the the downside of using indexing, for me, is that the syntax becomes a little cumbersome (i think, anyway). (2) instead of structuring the
then the
now you need a computed variable
the nice thing about this idea is that it takes all the computation out of the view code; and should you add or delete any measurement and the view is redrawn, the hope that helps, DMG |
|
Misurations is Measurement in English, btw. |
|
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.