I had registered a seat in the online Shanghai App Accelerator lecture about Swift Charts, tomorrow in the morning
I just found out that I had a class at the same time... Can I cancel my registration?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I've restarted my ColorProposer app and renamed it Chroma and the app suggests a color for a string. My main function is here:
import Foundation
import NaturalLanguage
import CoreML
func predict(for string: String) -> SingleColor? {
var model: MLModel
var predictor: NLModel
do {
model = try ChromaClassifier(configuration: .init()).model
} catch {
print("NIL MDL")
return ni
}
do {
predictor = try NLModel(mlModel: model)
} catch {
print("NIL PREDICT")
return nil
}
let colorKeys = predictor.predictedLabelHypotheses(for: string, maximumCount: 1) // set the maximumCount to 1...7
print(colorKeys)
var color: SingleColor = .init(red: 0, green: 0, blue: 0)
for i in colorKeys {
coor.morphing((ColorKeys.init(rawValue: i.key) ?? .white).toColor().percentage(of: i.value))
print(color)
}
return color
}
extension SingleColor {
mutating func morphing(_ color: SingleColor) {
self.blue += color.blue
self.green += color.green
self.red += color.red
}
func percentage(of percentage: Double) -> SingleColor {
return .init(red: slf.red * percentage, green: self.green * percentage, blue: self.blue * percentage)
}
}
struct SingleColor: Codable, Hashable, Identifiable {
var id: UUID {
get {
return .init()
}
}
var red: Double
var green: Double
var blue: Double
var color: Color {
get {
return Color(red: red / 255, green: green / 255, blue: blue / 255)
}
}
}
enum ColorKeys: String, CaseIterable {
case red = "RED"
case orange = "ORG"
case yellow = "YLW"
case green = "GRN"
case mint = "MNT"
case blue = "BLU"
case violet = "VLT"
case white = "WHT"
}
extension ColorKeys {
func toColor() -> SingleColor {
print(self)
switch self {
case .red:
return .init(red: 255, green: 0, blue: 0)
case .orange:
return .init(red: 255, green: 125, blue: 0)
case .yellow:
return .init(red: 255, green: 255, blue: 0)
case .green:
return .init(red: 0, green: 255, blue: 0)
case .mint:
return .init(red: 0, green: 255, blue: 255)
case .blue:
return .init(red: 0, green: 0, blue: 255)
case .violet:
return .init(red: 255, green: 0, blue: 255)
case .white:
return .init(red: 255, green: 255, blue: 255)
}
}
}
here's my view, quite simple:
import SwiftUI
import Combine
struct ContentView: View {
@AppStorage("Text") var text: String = ""
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@State var color: Color? = .white
var body: some View {
TextField("Text...", text: $text).padding().background(color).onReceive(timer) { _ in
color = predict(for: text)?.color
print(color)
}
}
}
But the problem of not updating the view still persists. In prints, I discovered a really strange issue: The line of
print(colorKeys)
is always the same.
Topic:
Developer Tools & Services
SubTopic:
Xcode
Tags:
Debugging
Machine Learning
Core ML
Create ML
OK I'm trying to make a sprite(model.Emmiter) that shoots balls(EnergyBalls) and the balls wont emit at the touch location:
import SpriteKit
import GameplayKit
class Sprites {
var Emmiter: SKSpriteNode = .init(imageNamed: "Emmiter")
}
class GameScene: SKScene {
var model: Sprites = .init()
var Emmiter = Sprites().Emmiter
var playableRect: CGRect = .zer
var lastTouch: CGPoint = .zero
override func didMove(to view: SKView) {
Emmiter.position = CGPoint(x: size.width / 2, y: size.width/* view.frame.minY + 100 */)
print(Emmiter.position)
self.addChild(Emmiter)
}
func touchDown(atPoint pos : CGPoint) {
lastTouch = pos
let rotation = -atan2(
lastTouch.x - Emmiter.position.x,
lastTouch.y - Emmiter.position.y
)
Emmiter.run(
.rotate(
toAngle: rotation,
duration: 0.25
)
)
fireEnergyBall(atPoint: lastTouch)
}
func touchMoved(toPoint pos : CGPoint) {
}
func touchUp(atPoint pos : CGPoint) {
}
func fireEnergyBall(atPoint location: CGPoint) {
let EnergyBall = SKSpriteNode(imageNamed: "Energy")
EnergyBall.position = Emmiter.position
print(EnergyBall.position)
let fly: SKAction = .run {
EnergyBall.run(.move(to: location, duration: 1))
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
EnergyBall.un(.sequence([.scale(to: 0, duration: 0.125), .removeFromParent()]))
}
}
EnergyBall.run(fly)
self.addChild(EnergyBall)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let rotation = -atan2(
touches.first!.location(
in: self
).x - Emmiter.position.x,
touches.first!.location(
in: self
).y - Emmiter.position.y
)
Emmiter.run(
.rotate(
toAngle: rotation,
duration: 0.25
)
)
fireEnergyBall(atPoint: touches.first!.location(in: self.view))
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: TimeInterval) {
}
}
I'm trying to make an app that will let the user put their phone down for a moment. How can I keep the phone locked in the same interface for a certain amount of time, no matter what method the user uses? I've seen lots of apps doing that. (I'm using SwiftUI)
PS Sorry can't think of an appropriate tag
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?
}
how can I get
CPU cores and type
GPU core and type
storage and memory
current wifi, recieved&sent packages
etc
in swift on macOS?
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?
I'm trying to get the memory usage of the entire system.
// referance: https://www.jb51.cc/iOS/590624.html
public func memsize() -> UInt64 {
var taskInfo = mach_task_basic_info()
var count = UInt32(MemoryLayout<mach_task_basic_info>.size)
let kerr: kern_return_t = withUnsafeMutablePointer(to: &taskInfo) {
$0.withMemoryRebound(to: integer_t.self,capacity: 1) {
task_info(mach_task_self_,task_flavor_t(MACH_TASK_BASIC_INFO),$0,&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)
// 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?
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?
MacBook Pro 13.3" - Y4QVXL9FGW
M2 8-core
24GB Unified memory
1T Solid Disk
MacOS 12.5.1 (21G83)
The screen flash pink and restarted again. No kernel crash log or warning anyhow.
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 xxx secs" after xxx 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?
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?
I'm trying to integrate AR into my app, and I want to be able to present a the UIViewController containing the ar view after pressing a sprite in SKScene. I'm trying to look for a way to present this view controller in touchesBegan however I'm not able to find any reference on this.
I'd really appreciate any help.