Post

Replies

Boosts

Views

Activity

Reply to Optional and onTapGesture
it won't accept it because there is already the action in the first part of the Button? It depends on the internal implementation of SwiftUI. We cannot say why, but it is clear under the current implementation of SwiftUI on iOS, the closure passed to onTapGesture is not executed. Will it - the onTapGesture method - work for nfcWriteButton? UIViewRepresentable and hidden Coordinator are making things more complex, and it depends on how you define the word work. But as far as I tried, the closure passed to onTapGesture is called, but the target-action method is not. Generally, you should better not write such fragile code, meaning depending on the implementation detail of the frameworks. Do not put onTapGesture on Button and write all things to do in the action closure Add action closure to your nfcWriteButton (in Swift, type names should start with Capital letter) like Button
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Save List data to device (another expansion)
If you do not need Core Data features and want to save the whole list at once, you should better consider using Coding. With making CardInfo conform to Codable, you can easily convert whole Array of CardInfo into Data. And Data can be easily writable to a file. If you find some difficulty about this, please tell us what you think is difficult.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Save List data to device (another expansion)
I need to figure out how to convert cardsInfo to JSON, if at all, and if that's what I need to convert. Seems you know what you need. I do not know much about the Hacking with Swift pages, but in usual tips or tutorial sites, there are some links to related articles including the reverse conversion. Can't you find one?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to A potentially dumb questions about swift (and dateFormatters)
Does this make sense? YES. In your code, df is a stored property. So, the initial value of the declaration (DrawingCellView.dateFormatter) is called only once at the first time when df is used. On the other hand, dateFormatter is a computed property. You see there is no = between dateFormatter: DateFormatter and {. The body of a computed property is called at each time the property is used. If so, is this the recommended way to construct a runOnce static constant declaration/assignment? I would declare the dateFormatter as follows and use it: class MyClass { &#9;&#9;static let dateFormatter: DateFormatter = { //<- There is `=` here &#9;&#9;&#9;&#9;let result = DateFormatter() &#9;&#9;&#9;&#9;result.dateFormat = "yyyy-MM-dd"         result.locale = Locale(identifier: "en_US_POSIX") &#9;&#9;&#9;&#9;return result &#9;&#9;}() //<- And there is `()` &#9;&#9; &#9;&#9;func dateText() -> String { &#9;&#9;&#9;&#9;return MyClass.dateFormatter.string(from: Date()) &#9;&#9;} &#9;&#9;//... } With this declaration, dateFormatter becomes a stored property and the initial value ({ ... }()) is called only once. (You should better set locale of the DateFormatter when you use it with fixed format in dateFormat.)
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Unwanted frame animations in SwiftUI 2
Is this an intended change or a bug?  Not sure. You should better send a feedback of this issue as a bug, we do not expect animations that we do not write explicitly and not documented. Another workaround: struct MyView: View { &#9;&#9;@State var color: Color = .blue &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;Text("Hello, World!") &#9;&#9;&#9;&#9;&#9;&#9;.background(color) &#9;&#9;&#9;&#9;&#9;&#9;.onAppear { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;DispatchQueue.main.async { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;withAnimation(.easeInOut(duration: 2)) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;color = .red &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;} }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to i386 architecture depreciated. How to update ARCHS build?
You should better clarify the version of Xcode and which platform your app is targeting. iOS? MacOS? Or some other? Generally, choose Build Settings tab in the project editor and put ARCH in the search bar, then you can find a setting titled Architectures. But if your project includes such setting as i386, your project would be so old and you might find many difficulties even after you fixed it.
Jan ’21
Reply to Optional and onTapGesture
I may need to consolidate my code in order to make it cleaner and safer. We do not call something safer or cleaner when it does not work as expected or not work steadily. In case of NFCWriteButton, beginScan will not be called. Is that what you want? I will look how to do it with UIButton().addTarget method. Please share your solution when you solved it, or please show your latest code if you cannot solve it yourself.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Save List data to device (another expansion)
Not on there. This is what it's suggesting: Sorry, I spent some time on that site and you are right, that site lacks the exact reverse situation you found. And with the suggested search words, I could not reach the right solution checking the first page of the result. I though this sort of well-known solution would be found easily without practicing myself, apologies. I will write a reply on your new thread with my code sample.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Convert a variable into JSON (Lunch Card expansion)
Sorry for my poor suggestions which did not work well. But once found, it is very simple. A simple sample code: import Foundation struct CardInfo: Identifiable, Codable { //<- Make `CardInfo` conform to `Codable` &#9;&#9;var name: String = "" &#9;&#9;var id: String = "" &#9;&#9;var cname: String = "" &#9;&#9;var code: String = "" } class CardsInfo: ObservableObject { &#9;&#9;@Published var newCard: CardInfo = CardInfo() &#9;&#9;@Published var cards: [CardInfo] = [] &#9;&#9; &#9;&#9;func add() { &#9;&#9;&#9;&#9;cards.append(newCard) &#9;&#9;} } //... extension CardsInfo { &#9;&#9;var dataUrl: URL { &#9;&#9;&#9;&#9;FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] &#9;&#9;&#9;&#9;&#9;&#9;.appendingPathComponent("cards.json") &#9;&#9;} &#9;&#9; &#9;&#9;func saveCards() { &#9;&#9;&#9;&#9;do { &#9;&#9;&#9;&#9;&#9;&#9;//Convert Array of `CardInfo` to `Data` &#9;&#9;&#9;&#9;&#9;&#9;let data = try JSONEncoder().encode(cards) &#9;&#9;&#9;&#9;&#9;&#9;//Write `Data` to a file specified by URL &#9;&#9;&#9;&#9;&#9;&#9;try data.write(to: dataUrl, options: .atomic) &#9;&#9;&#9;&#9;} catch { &#9;&#9;&#9;&#9;&#9;&#9;print(error) &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9; &#9;&#9;func loadCards() { &#9;&#9;&#9;&#9;do { &#9;&#9;&#9;&#9;&#9;&#9;if FileManager.default.fileExists(atPath: dataUrl.path) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;return &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;//Read `Data` from a file specified by URL &#9;&#9;&#9;&#9;&#9;&#9;let data = try Data(contentsOf: dataUrl) &#9;&#9;&#9;&#9;&#9;&#9;//Convert `Data` to Array of `CardInfo` &#9;&#9;&#9;&#9;&#9;&#9;let cards = try JSONDecoder().decode([CardInfo].self, from: data) &#9;&#9;&#9;&#9;&#9;&#9;self.cards = cards &#9;&#9;&#9;&#9;} catch { &#9;&#9;&#9;&#9;&#9;&#9;print(error) &#9;&#9;&#9;&#9;} &#9;&#9;} } You need to call cardsInfo.saveCards(), where cardsInfo is the right instance of CardsInfo, maybe some action closure in CardsView.
Topic: App & System Services SubTopic: General Tags:
Jan ’21