|
I have an app that was done using swift storyboard. I'm redoing it using swiftui and add additional features. Basically, what the app does is it converts and interprets each character of a string from a textfield and displays the conversion of each character as it is being typed. I have a separate text for each vowel conversion count, one for consonant conversion count, and another one for total of both. It looks like this: Vowel Consonant Total 2 6 8 So when a user types, each character being typed will have a display of the combination above plus an interpretation of this combination as another text display. My challenge is my function involves the textfield value as the main variable. I can get one function to manipulate the input data in the body of the content view. But I need to recalculate it again based on the new result. I tried creating a dictionary to represent each character/string with an Int so I can add the digits. Is there a way to use this dictionary of String/Character, Int as the key value so I can refer to it and convert it to its value. For example when a user types a, it will show 1 0 1 because a has a value of 1 and it's a vowel and there's only one a. And 101 will have its own interpretation of what that means in a form of a text. Is there a better way of doing this? I thought swiftUI makes your app easier to code and shortens the process. If anyone here thinks this isn't possible, I'd love to know so I can just stick with just using swift. I just like swiftUI but for what I want it to do in this case, I can't seem to make it work which is unfortunate. Any advise would be very much appreciated. |
|
|
|
Hi Sally, have a look at the below code to see if it suits your needs -
This is just a basic view with a textfield to enter text and a Text View to display the converted text. Unless you need your counts split into 3 text views you can just do it in one like above. If you need the 3 different text views then it would have to be reworked. Below this view, or somewhere, place the following function -
This function will be passed the word that is being typed by the user. It will count the number of vowels and consants in the word and then pass them back as a string using string interpolation together with their total. I know this only addresses the initial problem i cant help with the interpretation of the totals e.g. 1 0 1 to another string without knowing how you want it converted. If you could let me know or post some code to help a little more with this. Hope this helps, Dave |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Hi Sally, Hopefully this answers your question and points you in the right direction but try the following to simplify what you are doing and to head towards achieving what you need -
So you can use a simple dictionary of the vowels as the keys and their individual values. Just declare it in your function like above. Use the compactMap function on the string passed into the deriveVowels function. The compactMap func will return all non-nil results. What this means if vos[String($0)] (and all we are doing is using each character from the name as the key value to look for) doesn't exist, then it wont return it. At the end you will have an array of Int with the value for each vowel in the vos array. Then you just use the reduce function with the parameters 0 and + which is saying basically 0 is the starting point and for each value in the Int array i want to add it to 0. At the end you will end up with the total of the values in the int array. Just convert it to String and return it. This will reduce your code a bit and hopefully it meets your needs. You will have to sort of do the same for the deriveConsants function etc but hopefully this points you in the right direction. Have to let you do some of the work. You could even reduce the above even more by doing the following -
but.. this obviously reduces readability and if your working with a lot of code it might be confusing but ill leave it up to you. I hope this all helps Sally. Dont hesitate to ask anything more. This is one solution and there may be others. Dave |
|
Just an update and a lil more help the following will be the deriveConstants function (I forgot to the add the .lowercased() on the vowels func but its on the below one) -
Dave |
|
To access the deriveVowels function etc you could just create a helper class with static functions like so -
That way you can call it anywhere within your project like so -
Again there are probably other ways but it may suit what you want. Dave |
|
Thank you, thank you, thank you so much Dave! I just read this and I'm excited to try it out now. I will let you know how it goes :) |
|
|
|
Hi Dave, You are amazing! Thank you once again. I have added new functions to display the raw VCT as the third hstack and underneath to display the interpolation of these three results and reduced to a single digit. I created a keypair value to display the interpretation of VCT but it's not displaying correctly. I have the code below. Is there another way to access a returned variable to create another function? Thank you.
|
|
Hi Sally From what i can see when you run a compactMap on the constant VCT in the deriveNameVibration this is why you might be getting the wrong result. Correct me if I am wrong, but it appears you are attempt to match the VCT string with a key in your interpretation dictionary? If this is the case using compact map on VCT wont work. What the compactMap is doing is iterating through each character in VCT. It is then taking that character and seeing if it matches a key in your interpretation dictionary. For example... Lets say we had a profile name which ends up with a VCT of "022". From what i can gather it should match against the key "022" in your interpretation dictionary and display the associated text. What will happen is that compactMap will grab "0" first and try and match it with a key and so on. So where you have the following code -
You want to replace it with this -
To access keys values in a dictionary in Swift you simply just access it with 'dictName[key]'. The result returned is an optional because that key may not exist in the dictionary hence why i have used a guard let to safely unwrap and return a default value. So the entire function should look like the following -
You will notice that the entire function looks a bit different to yours. I have refactored it a little. Its up to you if you choose to do so but this is my reasoning -
Hopefully this does the trick Sally and hope it all makes sense. Dave |
|
I was trying it out but the second row was giving a 0 result for the VCT. I realized I forgot to add if V > 9 then V = V % 9 I tried to edit the shortcut you gave me so I can add the if statement and I know this might be really simple but I can't seem to put it together properly.
May I offer you a numerology session? I'd love to offer you something as a token of my appreciation. |
|
Hey Sally, Because you are using a 'if let' you wont be able to change the values of V or C inside that body because they are let constants. If you change the 'if let' to an 'if var' you should be good to go. Like the following -
Hopefull that works. I think you may have got a bit lost with your curly braces with the if statements for checking if V and C are greater than 9. Wow Sally I really do appreciate the offer but I am happy helping out. I think as programmers we are constantly evolving and learning. It never stops. One of the most valuable tools is helping others because i feel we tend to learn ourselves whilst doing so. I have learnt a lot helping you and others and that in itself is rewarding enough for me :) Dave |
|
Yes I actually figured it out before you replied and I was just confused with the curly braces and separator. I'm glad you're learning too and I believe that's what makes you a master. I'm not sure why it's still adding up 1 and 8 as 0 as if it's still being calculated with modulus |
|
Oh im by no means a master Sally. Im flattered though, thankyou. One thing i find that helps me is to add print statements to your code in areas where your expecting certain results. That way you can follow what is happening and find where the calculation is incorrect. Give that a go and see what happens Dave |
|
Sorry I made a mistake with my arithmetic equation. I was able to fix it in case you'd like to see :) I'll go ahead and do the fun part now and finish up my app. If I get stuck, I hope it's ok to ask for help. Thank you thank you thank you Dave. I'm impressed with how short you were able to convert my long swift file to this swiftUI version. The help you give to people will come back to you sevenfolds.
|
|
Hey Sally Good to hear its sorted :) Please let me know how it goes. It was my pleasure helping and thankyou for the kind words. And by all means if you get stuck i will be more than willing to help. Might i suggest to have a look at Paul's book Swift Coding challenges. There are a lot of challenges that are helpful there that will help you with future coding problems :) Dave |
|
Thank you. Yes I will do them for sure. I also love watching Paul's videos on youtube. I love supporting other people as well. I will pay it forward. |
|
I've added rating to interpretation. How do I convert return expression of type 'Interpretation' to return type 'String'?
I have this separate loverating file from my previous swift app file with the code below but not sure where the best place to put in swiftui
|
|
Hey Sally In regards to returning your string, instead of -
just add the following -
As this is the property of type String that you need access to. In regards to your open class... Because it is of Type UIView im not sure you would be able to use it in SwiftUI unless you tweak it a little and make it conform to UIViewRepresentable. There would be a bit involved in getting that done. You will also run into some issues using UImages etc in SwiftUI as well. Have a look at Pauls 100 days of SwiftUI course and in there, there is a project... I think its InstaFilter where he talks about using UIImages in SwiftUI. Can i ask though, what did you use that class for in your original project? Dave |
|
Thank you Dave. I was thinking since we were able to show the "vct" with the interpretation, and all that's left was to show two images for each vct. I have this function below. So far I can get one image
|
|
Hi Sally, You can do the following -
All you are doing is changed your [String : String] dictionary to a [String : [String]] dictionary. So each key/value pair is a String key with a String array value. To access each image in your array value in your dictionary you just do the following -
Hope this is okay Dave |
|
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.