Post

Replies

Boosts

Views

Activity

iPhone 14 Pro Series Simulator bug
I wrote a macOS app and I added iPhone support Now it looks like this: I thought it was some sort of .frame or sth like that I've accidentally added But then I started with a fresh macOS project and added iPhone support, and now it's still like that: I reckon that's because I've set the target os to iOS 15.4 because when I update to 16 there's no problem Is it because Xcode can't compile UI for a 14 pro's screen for below iOS 16? But then again, why does the preview work?
0
0
850
Oct ’22
Delayed Return in Swift
So I have an app that users can create utilities with some shell script I have a feature that the user can experiment with their scripts (a shell REPL) But if I type zsh in the REPL the whole app went stuck and the zsh shell outputs in Xcode: (This is my zsh theme) I've added detection for these: But what I really want is to let the process run in the background while the REPL output "Time out waiting for pipeline output after *** secs" after *** secs I've thought about letting them run in two separate asynchronous tasks so that if one task was completed it could first return the function but I just can't manage it: func run(launchPath: String? = nil, command: String) -> String { let task = Process() let pipe = Pipe() task.standardOutput = pipe task.standardError = pipe task.arguments = ["-c", command] task.launchPath = launchPath ?? "/bin/zsh/" task.launch() Task { let data = try? pipe.fileHandleForReading.readToEnd()! return String(data: data!, encoding: .utf8)! } DispatchQueue.main.asyncAfter(deadline: .now + 30) { return "Time out" // ERROR } } So How can I create two separate asynchronous tasks, one receiving the pipe output, and one, after a few seconds, returns the function?
3
0
1.7k
Sep ’22
Why?
I wrote an app that users can create utilities using terminal commands I'm experiencing a bug that's driving me crazy: I have an array of utilities, and by extension, I made Array<Utility> and Utility RawRepresantable: Part of the declaration for Utility public init?(rawValue: String) {     guard let data = rawValue.data(using: .utf8),           let result = try? JSONDecoder().decode(Utility.self, from: data)     else { return nil }     self = result } public var rawValue: String {     var encoder = JSONEncoder()     encoder.outputFormatting = .prettyPrinted     guard let data = try? encoder.encode(self),           let result = String(data: data, encoding: .utf8)     else {         return ""     }     return result } extension for Array<Utility> extension Array: RawRepresentable where Element: Codable {     public init?(rawValue: String) {         guard let data = rawValue.data(using: .utf8),               let result = try? JSONDecoder().decode([Element].self, from: data)         else { return nil }         self = result     }          public var rawValue: String {         let encoder = JSONEncoder()         encoder.outputFormatting = .prettyPrinted guard let data = try? encoder.encode(self),               let result = String(data: data, encoding: .utf8)         else {             return "[]"         }         return result     } } And now, because of some strange SwiftUI bug (FB11401294) When I pass a binding down a view hierarchy such as List or ForEach they update weirdly. I implement an "edit" feature for my utilities so I will not directly use binding but to call a function that takes an inout of Array<Utility>, the original item, and the item after being edited: func replace(_ sequence: inout [Utility], item: Utility, with replace: Utility) {     var raw = sequence.rawValue     print(raw)     let rawReplaced = raw.replacingOccurrences(of: item.rawValue, with: replace.rawValue)     print(item.rawValue, replace.rawValue)     print(rawReplaced)     sequence = [Utility].init(rawValue: sequence.rawValue.replacingOccurrences(of: item.rawValue, with: replace.rawValue))!     print(sequence) } And the problem is it works and after implementing another completely different feature it stopped working from my debugging, the input of the function is correct and the problem is inside the function. But, as the error seems to be on this line: sequence = [Utility].init(rawValue: sequence.rawValue.replacingOccurrences(of: item.rawValue, with: replace.rawValue))! Now i have completely no idea why this works last time and won't now This line of code works completely well in playgrounds and is now freaking me out Help anyone?
1
0
1.2k
Sep ’22
Enumerate the child view of a View
If we have this SwiftUI View: Text("Hello") Image("Some Image") We have two Views, and we refract them into a single one: struct MyView: View { var body: some View { Text("Hello") Image("Some Image") } } If we use it in most cases, it is ordered into an implicit VStack. But, if we do this: List { MyView() } SwiftUI will automatically split MyView into two separate Views. My question is that if I have this: @ViewBuilder func pageView(_ views: () -> View) -> View { } How can I enumerate the child Views of the variable view just like in List?
0
0
729
Aug ’22
SwiftUI Refreshing Mechanism
SwiftUI's Refreshing mechanism is driving me crazy. Say that I have this view: NavigationView { List($datasource) { $item in NavigationLink { SubView(item: $item) } label: { Text(item.someAttribute) } } } SubView: TextField("Placeholder", text: $item.someAttribute) And each time I edit the value in the SubView, the SwiftUI's navigation controller retreats the view to the home page(the initial view). Why?
3
0
1.1k
Aug ’22
Strange addition
// I have a object "CentralProcesser". I can get the user and system usage of the CPU. I want to get the total. let system: Double = CentralProcesser.current.usage.system // 10.8746757975 or something like that let user: Double = CentralProcesser.current.usage.user // 23.24123412424 or something like that // And now I add them together, right? let total: Double = system + user // nan Why?
1
0
774
Aug ’22
Get memory usage
I'm trying to get the memory usage of the entire system. // referance: https://www.jb51.cc/iOS/590624.html public func memsize() -&gt; UInt64 {     var taskInfo = mach_task_basic_info()     var count = UInt32(MemoryLayout&lt;mach_task_basic_info&gt;.size)     let kerr: kern_return_t = withUnsafeMutablePointer(to: &amp;taskInfo) {         $0.withMemoryRebound(to: integer_t.self,capacity: 1) {             task_info(mach_task_self_,task_flavor_t(MACH_TASK_BASIC_INFO),$0,&amp;count)         }     }       if kerr == KERN_SUCCESS {         return taskInfo.resident_size     }     return 0 } not working, apparently. When my system uses 15 GB it shows it's below 1 GB. Do anyone have other ways to get memory usage of the device (better macOS)
1
0
1.9k
Aug ’22
Keychain Access
I'm trying to get the password of the current user's wifi. I'm trying the Security framework mentioned by @eskimo here, but it's not working. I'm trying the CLI command public func getPassword(ssid: String) -> String? {     let command = "security find-generic-password -l \"\(ssid)\" -D 'AirPort network password' -w"     let result = shell(command)     return result } private func shell(_ command: String, lauchPath: String = "/bin/zsh") -> String? {     let task = Process()     let pipe = Pipe()          task.standardOutput = pipe     task.standardError = pipe     task.arguments = ["-c", command]     task.launchPath = "/bin/zsh"     task.launch()          let data = pipe.fileHandleForReading.readDataToEndOfFile()     let output = String(data: data, encoding: .utf8)?         .trimmingCharacters(in: .newlines)          return output } I'm now trying AppKit so I declared: @IBOutlet var passwordLabel: NSTextField! var password: String? var wifi: String? ... // applicationDidFinishLaunching(_ aNotification:) wifi = CWWiFiClient.shared().interface()?.ssid() .... password = getPassword(ssid: wifi ?? "Unknown") passwordLabel.stringValue = password ?? "ERROR" in applicationDidFinishLaunching(_ aNotification:) it worked. Note: on my dialog, there's only Deny and Allow, not Deny, Allow Once, Always Allow I added a refresh button next to the password label and it won't work when I press the button: @IBAction func RefreshPassword(_ sender: NSButton) {     wifi = CWWiFiClient.shared().interface()?.ssid()     password = getPassword(ssid: wifi)     print(password)     if let i = wifi {         DeviceWiFiLabel.stringValue = i         DeviceWiFiLabel.textColor = .systemGreen         passwordLabel.textColor = .textColor     } else {         DeviceWiFiLabel.stringValue = "None"         DeviceWiFiLabel.textColor = .systemOrange         passwordLabel.textColor = .red     } passwordLabel.stringValue = password ?? "ERROR" } The dialog still pops open but I can't enter text when the textfield is focused, that is, no text appears when I hit a key when textfield focused. When I dismiss using the "Deny" button, sometimes a message like these appears in the console: 2022-08-24 05:21:24.034479+0800 MyApp[22457:564086] Detected potentially harmful notification post rate of <some sort of float-point number that always changes and about 300) notifications per second I can't always reproduce the issue. Any idea why?
1
0
1.2k
Aug ’22
Battery level with IOPSCopyPowerSourcesList
I want to get battery level, estimated remaining time, battery health...etc via IOPSCopyPowerSourcesList Not sure how I'm not experienced with pointers...     func updateBatteryView() {         let battery = IOPSCopyPowerSourcesInfo().takeRetainedValue()         let info = IOPSCopyPowerSourcesList(battery).takeRetainedValue() // Now what?     }
1
0
1.3k
Aug ’22