|
Hello I was looking for an example to limit the length of text I can enter into a TextField. I'm sure this has been covered here but doing so lead me to post this. The solution I found is shown before. Nicely puts my data in it's own model as a source of truth. It doesn't work and the comments on the page I found it say that it used to and is now broken in IOS15. Does anyone know if it should work, did work or is this just wrong. The debug code does print out a truncated version of the string. Regards
|
|
hi Matthew, i'd offer two comments, neither of which quite directly addresses your question of if it should work, did work or is this just wrong.
perhaps something like this would work better:
and then worry about the persistence problem (storing the value of someText in UserDefaults, if that's really of interest to you) later. hope that helps, DMG |
|
Hello Thanks for that though it doesn't seem to fix my issue. The @AppStorage seems to work fine in the data model. My gut feeling is that is where it should be. Everything I have read says seperate your view from your data. If the data is an observable object it can and maybe required in several different views so therefore cannot be stored from the view. I'm going on what I watched in https://developer.apple.com/videos/play/wwdc2020/10040/ I shall keep on trying. Regards |
|
Don't do that work in the view model. The view model should store what the limit and the text are, but the work of actually limiting the input text belongs in the Here's one way to do it...
|
|
Hello Thank you for that. Does exacty what I need it to do and I can relate how limiting the text length is treated as just another view modifier and the desired length enacpauslated into the data model too. Cheers |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
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.