Post

Replies

Boosts

Views

Activity

Reply to TextField with NumberFormatter, bound to Decimal property with Xcode > 13 beta 2.
An interesting example. In my opinion, this is a bug of iOS 13 beta 3+ and you should better send a bug report soon. What must be changed in the code so that entered numerical values are stored in the Decimal property again? As far as I tried, creating a Binding String <-> Decimal seemed nearly working: private let decimalFormatter: NumberFormatter = { // let formatter = MyNumberFormatter() let formatter = NumberFormatter() formatter.numberStyle = .decimal formatter.generatesDecimalNumbers = true return formatter }() struct DecimalBindingView: View { @Binding var model: Model var stringBinding: Binding<String> init(model: Binding<Model>) { _model = model stringBinding = Binding(get: { decimalFormatter.string(for: model.wrappedValue.decimalQuantity) ?? "" }, set: { if let decimal = decimalFormatter.number(from: $0) as? NSDecimalNumber { model.wrappedValue.decimalQuantity = decimal as Decimal } }) } var body: some View { Form { Section { Label { Text("This text field does not work correctly. \nIt doesn't write the value back to the property when the field loses focus or you click Enter.") } icon: { Image(systemName: "xmark.circle").foregroundColor(Color.red).scaleEffect(1.4) } .lineLimit(10) TextField("Input a number of type decimal…", text: stringBinding) } Section { TextField( "Sample field, so you can leave the other field", text: $model.someText) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to Segmentation fault vs Variable ... used before being initialized
Is this a compiler bug in the newest version? why would we trade a compiler error for a runtime crash Isn't it clear it is a compiler bug? According to the accepted proposal of propertyWrapper (SE-0258Property Wrappers), declaring an @State variable is something similar to this: private var _player: State<AVPlayer> = State<AVPlayer>(wrappedValue: 1738) var player: AVPlayer { get { return _player.wrappedValue } set { _player.wrappedValue = newValue } } (Simplified to illustrate the problem.) As you see, the property player is a computed property, so, based on the initialization rule of Swift, it will be available only after all the stored properties are initialized. Swift compiler should mark the line self.player = AVPlayer() error. You can send a bug report to Apple or swift.org. By the way, creating a useless instance like AVPlayer() is not recommended. You should use Optional if you cannot use AVPlayer.init(url:) or AVPlayer.init(playerItem:) at the time of initialization.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to Current Xcode/SDK requirements?
You should better check the announcements of Apple by yourself: News and Updates As the time of this submission, this seems to be the latest: App Store submission update Starting April 26, 2021, all iPhone and iPad apps submitted to the App Store must be built with Xcode 12 and the iOS 14 SDK or later. Starting April 26, 2021, all watchOS apps submitted to the App Store must be built with Xcode 12 and the watchOS 7 SDK or later. You can still use Xcode 11 for some other purposes, such as learning coding, but you need to use Xcode 12 (or later) if you are planning to write an app for App Store.
Aug ’21
Reply to Checking for characters
When you show your code, please use the Code Block feature of this site: (And a good indentation would make the code far more readable, Ctrl-I in Xcode editor will do it for you.) let tenMostCommonPasswords = [ "123456", "password", "12345678", "qwerty", "12345", "123456789", "letmein", "1234567", "football", "iloveyou" ] let digits = "0123456789" let punctuation = #"!@#$%^&*(),.<>;'`~[]{}\|/?_-+= "# let password = "Pasword12!" for _ in tenMostCommonPasswords { if tenMostCommonPasswords.contains(password) { print("Please change your password") } else { for _ in digits { if digits.contains(password) { for _ in punctuation { if punctuation.contains(password) { print("You're good to go!") } else { print("Please add a punctuation") } } } else { print("Please add a digit.") } } } } The expression digits.contains(password) gets true when digits("0123456789") contains the string password ("Pasword12!"). But as you see, "0123456789" does not contain "Pasword12!". So the else-close will be executed. I guess you may want to do something like this: if tenMostCommonPasswords.contains(password) { print("Please change your password") } else { for digitChar in digits { if password.contains(digitChar) { for punctuationChar in punctuation { if password.contains(punctuationChar) { print("You're good to go!") } else { print("Please add a punctuation") } } } else { print("Please add a digit.") } } } You may find this code needs to be fixed a little more, but it might be a good habit to leave a room for learning by yourself.
Topic: Business & Education SubTopic: General Tags:
Aug ’21
Reply to Cannot use instance member 'retreiveFromArrayOfMeme' within property initializer
the following code in my class MemeMeViewModel which is an ObservableObject  You should better include the class header when you show some parts of a class. I have the following error : "Cannot use instance member 'retreiveFromArrayOfMeme' within property initializer; property initializers run before 'self' is available". As the error message is clearly stating, you cannot use the instance method retreiveFromArrayOfMeme within property initializer. One possible fix would be making it a static method in this case: @Published var memes: [MemeMeModel.Meme] = MemeMeViewModel.retreiveFromArrayOfMeme() static func retreiveFromArrayOfMeme() -> [MemeMeModel.Meme] { var temp = [MemeMeModel.Meme]() if let memes = UserDefaults.standard.object(forKey: "MemesList") as? [[Any]] { for meme in memes { temp.append(MemeMeModel.Meme(pngRepresentation: meme[0] as! Data, content: meme[1] as! String)) } } return temp } (In Swift, only type names start with Capital letter.) You may need to find other ways when you want use some other instance members in retreiveFromArrayOfMeme().
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’21
Reply to I'm trying to understand Combine Future
Please try something like this: import Foundation import Combine import SwiftUI let future = Future<Int, Never> { promise in print("Creating") DispatchQueue.global().asyncAfter(deadline: .now() + 2) { print("sending promise") promise(.success(1)) } } let cancellable = future //<- .sink(receiveCompletion: { print("receiveCompletion:\($0)") }, receiveValue: { print("receiveValue:\($0)") }) print("end") It seems the result of future.sink needs to be held in a strong reference until the operation is completed.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21