Post not yet marked as solved
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?
}
Post not yet marked as solved
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 have a M2, 13" MacBook Pro
import Cocoa
import IOKit
import Foundation
var blob = IOPSCopyPowerSourcesInfo() // cannot find 'IOPSCopyPowerSourcesInfo' in scope
Post not yet marked as solved
Can I export a SwiftUI Color / UIKitUIColor / AppKit NSColor to a .clr file? How?
Bonus: Can I transfer them to each other?
Post not yet marked as solved
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
Post not yet marked as solved
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) {
}
}
Post not yet marked as solved
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.
Post not yet marked as solved
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?
I tried to implement ExpressibleByIntegerLiteral to my code and it has been terrible.
At last, it asked me to implement this init
// Cannot find type 'Builtin' in scope
init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
}
Post not yet marked as solved
I've just bought a M2 MacBook Pro, and my Timer app is giving me strange crash logs.
Here's my code:
struct ContentView: View {
@State var process: Any? = nil
@AppStorage("time") var time = 0
@State var timePassed = 0
@State var timer: Timer.TimerPublisher = Timer.publish(every: 1, on: .main, in: .common)
@State var cacheSecondsString: String = String(UserDefaults.standard.integer(forKey: "time"))
@State var startTheTimer: Bool = false
var body: some View {
ZStack {
TimerView(time: $timePassed, total: $time).padding()
VStack {
TextField("Time...", text: $cacheSecondsString)
.font(.system(size: 35, weight: .light, design: .rounded))
.multilineTextAlignment(.center)
.onSubmit {
time = Int(cacheSecondsString) ?? time
cacheSecondsString = String(time)
}
.textFieldStyle(.plain)
.padding()
.fixedSize()
Button {
withAnimation {
startTheTimer.toggle()
}
} label: {
Label(startTheTimer ? "Stop" : "Start", systemImage: startTheTimer "stop.fill" : "clock").foregroundColor(.accentColor)
}.buttonStyle(.plain).padding()
}
}.onChange(of: startTheTimer) { start in
if start {
process = timer.connect()
} else {
(process! as! Cancellable).cancel()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewDevice("iPad Air (5th generation)")
}
}
When I sort of "run" the preview (pressing on the play.circle button on top of my preview and press the button the second to deactivate my timer, a crash dialog appear, the standard one macOS uses when any process crashed, but the preview didn't crash.
Post not yet marked as solved
Because of some problems I turn again to Monterey
in this thread, @ekimo says that to use ad hoc you need to archive a iOS App
however:
what did I did wrong?
Post not yet marked as solved
I have a structure TodoItem
struct TodoItem: Codable, Hashable, Identifiable {
var id: UUID = UUID()
var item: String
var done: Bool = false
}
And I have an array of them, and I’d like to find out a way to calculate(in a array) how many are done; that is, myArray.count / itemsDone
.count is easy, itemsDone can be achieved like this:
extension Array where Element == TodoItem {
var itemsDone: Double {
var done: Double = 0
for i in self {
if i.done { done += 1 }
}
return done
}
}
but it didn’t.
struct ContentView: View {
@State var todos: [TodoItem]
…
ProgressView(value: todos.itemsDone / todos.count) // Error
My Playground won’t let me copy the error(no idea why) and my family’s iPad is in Chinese so one is Chinese. I’ll try it on Xcode and see if I can find out more. will update with the error.
EDiT
nor will this work:
ProgressView(value: todos.itemsDone, total: todos.count)
My code:
struct PopoverMessage: View {
var body: some View {
VStack(alignment: .leading) {
Text("You need to launch this game at Teminal.")
.font(.title)
.fontWeight(.light)
Divider()
HStack {
Text("Help me generate launch command ")
.fontWeight(.thin)
Image(systemName: "rectangle.portrait.and.arrow.right.fill")
.resizable()
.foregroundColor(.gray)
.frame(width: 10, height: 10)
}.padding(10)
}
}
}
works while
struct PopoverMessage: View {
var body: some View {
VStack(alignment: .leading) {
Text("You need to launch this game at Teminal.")
.font(.title)
.fontWeight(.light)
Divider()
HStack {
Text("Help me generate launch command ")
.fontWeight(.thin)
Image(systemName: "rectangle.portrait.and.arrow.right.fill")
.weigh
.resizable()
.foregroundColor(.gray)
.frame(width: 10, height: 10)
}.padding(10)
}
}
}
don't and displays a error message Trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure
Here's the text in the diagnostics sheet:
trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure
----------------------------------------
CompileDylibError: Failed to build ContentView.swift
Compiling failed: trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure
/Users/wangkeshijian/Documents/.myFolder/.system/TerminalAdventure/TerminalAdventure/ContentView.swift:12:37: error: trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure
VStack(alignment: .leading) {
^
SwiftUI.VStack:4:12: note: 'init(alignment:spacing:)' declared here
public init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil)
^
// my comment here: it seems that SwiftUI, under some sort of strange bug, did notrecognize my initializer
==================================
| BuildInvocationError
|
| /Users/wangkeshijian/Documents/.myFolder/.system/.Downloads/Xcode14-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -F /Users/wangkeshijian/Documents/.myFolder/.system/.Downloads/Xcode14-beta.app/Contents/SharedFrameworks -enforce-exclusivity=checked -DDEBUG -sdk /Users/wangkeshijian/Documents/.myFolder/.system/.Downloads/Xcode14-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -target x86_64-apple-macos12.4 -module-cache-path /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -Xfrontend -serialize-debugging-options -enable-testing -swift-version 5 -enable-bare-slash-regex -I /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Products/Debug -F /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Products/Debug -emit-localized-strings -emit-localized-strings-path /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/Objects-normal/x86_64 -c -j4 -serialize-diagnostics -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-generated-files.hmap -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-own-target-headers.hmap -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-all-target-headers.hmap -Xcc -iquote -Xcc /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-project-headers.hmap -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Products/Debug/include -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/DerivedSources-normal/x86_64 -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/DerivedSources/x86_64 -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/DerivedSources -Xcc -DDEBUG=1 -working-directory /Users/wangkeshijian/Documents/.myFolder/.system/TerminalAdventure /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/Objects-normal/x86_64/ContentView.1.preview-thunk.swift -o /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/Objects-normal/x86_64/ContentView.1.preview-thunk.o -module-name TerminalAdventure_PreviewReplacement_ContentView_1 -Onone -Xfrontend -disable-modules-validate-system-headers -gline-tables-only
Post not yet marked as solved
macOS Ventura 13 beta 2
I'm coming over with a really strange issue with my cursor. It'd move toward the right-down by itself one pixel a time: right down right down
It really freaked me out *laugh*
I'll try attaching a video. FB submitted.
I have a structure TodoItem
struct TodoItem: Codable, Hashable, Identifiable {
var id: UUID = UUID()
var item: String
var done: Bool = false
}
How can I use AppStorage on [TodoItem]?