Post

Replies

Boosts

Views

Activity

Reply to An alternative Swift method to PHP `pack()` method
This is what I would write if I were given the PHP class Tag in your link: import Foundation public class MyTag { public let tag: UInt8 public let value: String public init(tag: UInt8, value: String) { self.tag = tag assert(value.utf8.count < 256, "The length of `value` needs to be less than 256.") self.value = value } public var length: Int { value.utf8.count } ///Use this instead of `__toString()`. ///Please remember, in PHP, `__toString()` may be called implicitly in many cases. ///You may need to replace all such cases to use this `getData()` explicitly. public func getData() -> Data { return toByteData(tag) + toByteData(UInt8(length)) + value.data(using: .utf8)! } ///Use this instead of `toHex()`. func toByteData(_ value: UInt8) -> Data { return Data([value]) } } Generally, it is hard to convert code of some language having a concept of binary string into Swift. You may have two ways: Use String and consider how you embed binary data into String. Use Data and when you need to concatenate it with String, convert the String to Data before concatenating. I would choose the latter. let myTag = MyTag(tag: 1, value: "abc") print(myTag.toByteData(1).base64EncodedString()) //-> AQ==
Topic: Programming Languages SubTopic: Swift Tags:
Nov ’21
Reply to ScrollView overlapping NavigationView bar
Thanks for updating. I (and possibly some other readers) will take some time to investigate. ... Sorry, but your code generates error Cannot find 'fruit' in scope and there may be more errors -- the definitions of FruitHeaderView or SourceLinkView are missing. Also you are not showing the root view (shown as < Fruits). Hard to find what's happening under the currently shown info.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’21
Reply to Axie infinity
NO, you cannot get such a thing here. The dev forums is not a place to request sort of invitation code of some specific app. Contact to the author of the app directly.
Nov ’21
Reply to What’s the difference between a Blank and an Xcode Playground?
I cannot find any documentation about this. It's a pity many of the templates do not contain enough documentations. As far as I tried, the template Xcode Playground will create a file package with extension .playground, which is compatible with Xcode Playground. On the other hand, the template Blank creates a file package of .playgroundbook, which is compatible with Swift Playgrounds for iPad and Swift Playgrounds for Mac. This is the common behavior as in most other templates. You may already know but Xcode Playground is a part of Xcode, Apple's official tool to develop apps for Apple's platforms. So, if you want to make something which needs to be compatible with Xcode, you should choose the template Xcode Playground. But, as there's Swift Playgrounds for Mac, you would rarely need the template Xcode Playground. By the way, you should better respond to the answers (or comments, possibly) you get. Many readers prefer to write answers to the questions of which the authors respond properly.
Topic: App & System Services SubTopic: Core OS Tags:
Nov ’21
Reply to Xcode hideous update time
How much resource does your MacBook Pro have? Storage? Memory? And the internet connection is stable enough while installing? Have you tried while Apple's server is seemingly free? It is quite usual it takes more than three hours to update Xcode, but have never heard nearly 24 hours.
Nov ’21
Reply to Subclassing a NSSearchField
Problem : the items are disabled and I don't know how to enable it : What's wrong ? To make menu items enabled, there needs to be a responder which can respond to the action. class ViewController: NSViewController { @IBOutlet weak var searchField: NSSearchField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let theMenu = NSMenu(title: "Filters") let theMenus = ["all", "artist", "title", "album"] for theName in theMenus { //↓ Specify `action:`. let theMenuItem = NSMenuItem(title: theName, action: #selector(menuItemSelected), keyEquivalent: "") theMenu.addItem(theMenuItem) theMenuItem.isEnabled = true } searchField.searchMenuTemplate = theMenu } //↓ Define the `action` method used in `NSMenuItem(title:action:keyEquivalent:)`. @objc func menuItemSelected(_ sender: Any) { print(sender, type(of: sender)) } //... } Generally, you should better show more context (especially your code), to get better responses sooner.
Topic: UI Frameworks SubTopic: AppKit Tags:
Nov ’21
Reply to Convert date "HH" to Int?
Why don't you do it as you describe? struct ContentView: View { @State var date: Date = Date() //@State... //var... let timer = Timer.publish(every: 1, on: .main, in: .common) .autoconnect() var body: some View { ScrollView{ VStack{ Text("Væskebalanse") .font(.title) Text(timeString(date: date)) } } .onReceive(timer) {_ in self.date = Date() let hh = timeString(date: self.date) let now = Int(hh)! let hoursSince0600: Int if now < 10 { //number of hours between 06:00 yesterday -> now hoursSince0600 = now + (24-6) } else { //number of hours between 06:00 -> now hoursSince0600 = now-6 } print(hoursSince0600) //... } } let timeFormat: DateFormatter = { let formatter = DateFormatter() formatter.dateFormat = "HH" formatter.locale = Locale(identifier: "en_US_POSIX") return formatter }() func timeString(date: Date) -> String { let time = timeFormat.string(from: date) return time } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’21
Reply to View pops automatically when in navigation view
For some reason the selection property becomes nil maybe. Your guess seems to be wrong. The most critical thing in your code is that a single selection: is shared between two NavigationLinks in different levels of navigation. When you execute this line of code: navigationHelper.selection = "View3" The NavigationLink in View1 gets inactive: //↓This NavigationLink gets inactive when `navigationHelper.selection` has any other value than `"View2"` NavigationLink(tag: "View2", selection: $navigationHelper.selection) { View2() } label: { EmptyView() }.isDetailLink(false) So, one possible workaround would be something like this: import SwiftUI class NavigationHelper: ObservableObject { @Published var view2IsActive: Bool = false @Published var view3IsActive: Bool = false } struct View1: View { @ObservedObject var navigationHelper:NavigationHelper = NavigationHelper() var body: some View { NavigationView{ VStack { Text("View1") .padding() Button { navigationHelper.view2IsActive = true } label: { Text("Go To View2") } NavigationLink.init(isActive: $navigationHelper.view2IsActive) { View2() } label: { EmptyView() }.isDetailLink(false) } }.environmentObject(navigationHelper) } } struct View2: View { @EnvironmentObject var navigationHelper:NavigationHelper var body: some View { VStack { Text("View2") .padding() Button { navigationHelper.view3IsActive = true } label: { Text("Go To View3") } NavigationLink(isActive: $navigationHelper.view3IsActive) { View3() } label: { EmptyView() } } } } struct View3: View { @EnvironmentObject var navigationHelper:NavigationHelper var body: some View { VStack { Text("View3") .padding() Button { navigationHelper.view2IsActive = false } label: { Text("Go To Root") } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’21