Posts

Post not yet marked as solved
1 Replies
153 Views
Im using Notions API to print out some data from one of my own pages in notion and im using URLSession to make the request then parsing the unwrapped data but nothing is being returned to my console and I know my endpoint and API key is correct. I've gone through the notion API documentation can't can't seem to find anything in it that I am not doing or doing wrong. Ill provide my code as well as the documentation I've been consulting: https://developers.notion.com/reference/intro import Foundation struct Page: Codable { let id: String let title: String } let endpoint = URL(string: "https://api.notion.com/v1/pages/8efc0ca3d9cc44fbb1f34383b794b817") let apiKey = "… redacted …" let session = URLSession.shared func makeRequest() { if let endpoint = endpoint { let task = URLSession.shared.dataTask(with: endpoint) { data, response, error in if let taskError = error { print("could not establish url request:\(taskError)") return } if let unwrapData = data { //safely unwrapping the data value using if let do { let decoder = JSONDecoder() //JSONDecoder method to decode api data, let codeUnwrappedData = try decoder.decode(Page.self,from: unwrapData) //type: specifies its a struct, from: passes the data parmeter that contains the api data to be decoded } catch { print("could not parse json data") } } if let httpResponse = response as? HTTPURLResponse { if httpResponse.statusCode == 200 { if let apiData = data { print(String(data: apiData, encoding: .utf8)!) } } else { print("unsuccessful http response:\(httpResponse)") } makeRequest() } } task.resume() } }
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
1 Replies
257 Views
I have an async function where im using the withUnsafeContinuation method and closure but for some reason Xcode is not letting me pass either 'error' or 'Error' for continuation.resume(throwing:). I get the error "Cannot convert value of type 'any Error'" public func parseResponseData(data: Data) async throws -> TextResult { return try await withUnsafeContinuation { continuation in Task { do { let decoder = JSONDecoder() let result = try await decoder.decode(TextResult.self, from: data) continuation.resume(returning: result) } catch { continuation.resume(throwing: error) } } } }
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
1 Replies
190 Views
I have some c code that returns memory usage of a current task on my machine and recently redacted it to use the proc_getallinfio struct so I can instead retrieve systemwide memory usage. im calling that code in swift however im getting the error "Initializer 'init(_:)' requires that 'proc_taskallinfo' conform to 'BinaryInteger'" and im not sure what the appropriate field is to pass that works with proc_getallinfo struct. resident_size does not work in this context. import IOKit import Foundation @_silgen_name("kernMem") func kernMem(storeMemData: UnsafeMutablePointer <proc_taskallinfo>) -> kern_return_t @main struct MacStatAppApp: App { @State public var printMemory: String = "" //dynamic state object to store data that will be passed to swiftUI var body: some Scene { WindowGroup { ContentView(printMemory: $printMemory) //binding for printMemory to pass data to contentview .onAppear { var storeMemData = proc_taskallinfo() //define pointer let result = kernMem(storeMemData: &storeMemData) if result == KERN_SUCCESS { let memoryUsage = Double(storeMemData) / (1024.0 * 1024.0 * 1024.0) //conversion for GB, 1024 to the power of 3 print(String(format: "memory usage: %.2f GB", memoryUsage)) } else { print("failed to obtain memory usage data:\(result)") } } } } }
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
1 Replies
321 Views
Im honestly a bit lost and looking for general pointers. Here is the general flow of my project. I have an Xcode project where I want to return and convert the temperature values accessed from the apple smc and I found a GitHub repo with all the smc key sensors for the M3Pros/Max chips: https://github.com/exelban/stats/issues/1703 basically, I have all these keys stored in an array in obj-c like so: NSArray *smcKeys = @[ @"Tp01", @"Tp05", @"Tp09", @"Tp0D", @"Tp0b", @"Tp0f", @"Tp0j", @"Tp0n",@"Tp0h", @"Tp0L", @"Tp0S", @"Tp0V", @"Tp0z", @"Tp0v", @"Tp17", @"Tp1F", @"Tp1J", @"Tp1p", @"Tp1h", @"Tp1R", ]; I am passing all these keys by passing 'smcKeys' in a regular C code file I have here that is meant to open, close and read the data shown here: #include "smc.h" #include <mach/mach.h> #include <IOKit/IOKitLib.h> #include "smckeys.h" io_connect_t conn; kern_return_t openSMC(void) { kern_return_t result; kern_return_t service; io_iterator_t iterator; service = IOServiceGetMatchingServices(kIOMainPortDefault, IOServiceMatching("AppleSMC"), &iterator); if(service == 0) { printf("error: could not match dictionary"); return 0; } result = IOServiceOpen(service, mach_task_self(), 0, &conn); IOObjectRelease(service); return 0; } kern_return_t closeSMC(void) { return IOServiceClose(conn); } kern_return_t readSMC(char *smcKeys, SMCVal_t *val) { kern_return_t result; uint32_t keyCode = *(uint32_t *)smcKeys; SMCVal_t inputStruct; SMCVal_t outputStruct; inputStruct.datasize = sizeof(SMCVal_t); inputStruct.datatype = 'I' << 24; //a left shift operation. turning the I into an int by shifting the ASCII value 24 bits to the left inputStruct.data[0] = keyCode; result = IOConnectCallStructMethod(conn, 5, &inputStruct, sizeof(SMCVal_t), &outputStruct, (size_t*)&inputStruct.datasize); if (result == kIOReturnSuccess) { if (val -> datasize > 0) { if (val -> datatype == ('f' << 24 | 'l' << 16 | 't' << 8 )) { //bit shifting to from 32bit operation associated with the ASCII charecters'f', 'l', and 't', sets datatype field. double temp = *(double *)val -> data; return temp; } } } return 0.0; } Which I am then then calling the functions from this file in a swift file and converting the values to Fahrenheit but no data is being printed in my console: import IOKit public class getTemperature { public struct SMCVal_t { var datasize: UInt32 var datatype: UInt32 var data: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) } @_silgen_name("openSMC") func openSMC() -> kern_return_t @_silgen_name("closeSMC") func closeSMC() -> kern_return_t @_silgen_name("readSMC") func readSMC(key: UnsafePointer<CChar>?,val: UnsafeMutablePointer<SMCVal_t>) -> kern_return_t func convertAndPrintTempValue(key:UnsafePointer<CChar>?,scale: Character, showTemp: Bool ) -> kern_return_t { let openSM = openSMC() guard openSM == 0 else { print("Failed to open SMC: \(openSM)") return kern_return_t() } let closeSM = closeSMC() guard closeSM == 0 else { print("could not close SMC: \(closeSM)") return IOServiceClose(conn) } func convertAndPrint(val: SMCVal_t) -> Double { if val.datatype == (UInt32("f".utf8.first!) << 24 | UInt32("l".utf8.first!) << 16 | UInt32("t".utf8.first!) << 8) { let extractedTemp = Double(val.data.0) return( extractedTemp * 9.0 / 5.0 + 32.0 ) } return 0.0 } let smcValue = SMCVal_t(datasize: 0, datatype: 0, data: (0,0,0,0,0,0,0,0)) let convertedVal = convertAndPrint(val: smcValue) print("Temperarure:\(convertedVal)F°") return kern_return_t() } } I know this is a lot but I am honestly looking for any tips to fill in any gaps in my knowledge for anyone who's built a similar application meant to extract any sort of data from Mac hardware.
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
0 Replies
326 Views
I have an Xcode project with an obj-c .c file and a .h file aswell as a .swift file where I am calling functions from those obj-c files with a bridging header but when I build my project I get a duplicate symbols error and Xcode doesn't show where. here is .h header file: #define smc_h #include &lt;stdint.h&gt; #include &lt;mach/mach.h&gt; #include &lt;IOKit/IOKitLib.h&gt; typedef struct { uint32_t datasize; uint32_t datatype; uint8_t data[8]; } SMCVal_t; io_connect_t conn; kern_return_t openSMC(void); kern_return_t closeSMC(void); kern_return_t readSMC(char *key, SMCVal_t *val); double convertToFahrenheit(SMCVal_t *val); #endif /* smc_h */ my .c implementation file: #include "smc.h" kern_return_t openSMC(void) { kern_return_t result; kern_return_t service; io_iterator_t iterator; service = IOServiceGetMatchingServices(kIOMainPortDefault, IOServiceMatching("AppleSMC"), &amp;iterator); if(service == 0) { printf("error: could not match dictionary"); return 0; } result = IOServiceOpen(service, mach_task_self(), 0, &amp;conn); IOObjectRelease(service); return 0; } kern_return_t closeSMC(void) { return IOServiceClose(conn); } kern_return_t readSMC(char *key, SMCVal_t *val) { kern_return_t result; uint32_t keyCode = *(uint32_t *)key; SMCVal_t inputStruct; SMCVal_t outputStruct; inputStruct.datasize = sizeof(SMCVal_t); inputStruct.datatype = 'I' &lt;&lt; 24; //a left shift operation. turning the I into an int by shifting the ASCII value 24 bits to the left inputStruct.data[0] = keyCode; result = IOConnectCallStructMethod(conn, 5, &amp;inputStruct, sizeof(SMCVal_t), &amp;outputStruct, (size_t*)&amp;inputStruct.datasize); if (result == kIOReturnSuccess) { if (val -&gt; datasize &gt; 0) { if (val -&gt; datatype == ('f' &lt;&lt; 24 | 'l' &lt;&lt; 16 | 't' &lt;&lt; 8 )) { //bit shifting to from 32bit operation associated with the ASCII charecters'f', 'l', and 't', sets datatype field. double temp = *(double *)val -&gt; data; return temp; } } } return 0.0; } double convertToFahrenheit(SMCVal_t *val) { if(val -&gt; datatype == ('f' &lt;&lt; 24 | 'l' &lt;&lt; 16 | 't' &lt;&lt; 8 )) { //checking if val-&gt;datatype is equal to the result of the bit-shifting operation. double temp = *(double *)val -&gt; data; return (temp * 9.0 / 5.0) + 32.0; } return 0.0; } And my .swift file where my objc functions are called: import IOKit public class CPUTempCaller { public struct SMCVal_t { var datasize: UInt32 var datatype: UInt32 var data: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) } @_silgen_name("openSMC") func openSMC() -&gt; Int32 @_silgen_name("closeSMC") func closeSMC() -&gt; Int32 @_silgen_name("readSMC") func readSMC(key: UnsafePointer&lt;CChar&gt;?,val: UnsafeMutablePointer&lt;SMCVal_t&gt;) -&gt; kern_return_t @_silgen_name("convertToFahrenheit") func convertToFahrenheit(val: UnsafePointer&lt;SMCVal_t&gt;) -&gt; Double { let openSM = openSMC() guard openSM == 0 else { print("Failed to open SMC: \(openSM)") return 0.0; } let closeSM = closeSMC() guard closeSM == 0 else { print("could not close SMC: \(closeSM)") return 0.0; } func convertAndPrintTempValue(key:UnsafePointer&lt;CChar&gt;?,scale: Character, showTemp: Bool ) -&gt; Double? { var SMCValue = SMCVal_t(datasize: 0, datatype: 0, data:(0,0,0,0,0,0,0,0)) //initializing SMC value if let Key = key { //check if nil. If not nil, proceed to code block execution let key = "TC0P" let keyCString = (key as NSString).utf8String //passing key as null terminated utf8 string let readSMCResult = readSMC(key: keyCString, val: &amp;SMCValue) //call readSMC obj-C function, store result in "readSMCResult" if readSMCResult != KERN_SUCCESS { print("Failed to read SMC: \(readSMCResult)") } } if showTemp { //return nil if showTemp is false let convertRawToFahrenheit = convertToFahrenheit(val: &amp;SMCValue) let scaleToStr = String(scale) print(String(format: "Temperature: %0.1f °%c", convertRawToFahrenheit, scaleToStr)) return nil } else { print("could not convert temperature and format values in Fahrenheit") return nil } } return 0.0; } }
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
1 Replies
307 Views
I have a swift file where I am calling objective c functions and providing pointers to certain values where needed but im getting the error " : Cannot convert value of type 'Swift.UnsafeMutablePointer<MacStat.SMCVal_t>' to expected argument type 'Swift.UnsafeMutablePointer<__ObjC.SMCVal_t>' " Here is the relevant code block where I am getting the error: var convertRawToFahrenheit: Double = 0.0 withUnsafeMutablePointer(to: &SMCValue) { pointer in convertRawToFahrenheit = convertToFahrenheit(val: pointer) } print(String(format: "Temperature: %0.1f °%c", convertRawToFahrenheit )) return nil } else { print("could not convert temperature and format values in Fahrenheit") return nil } return 0.0; } I already have a bridging header and the path to that header set up so I know the issue Isn't anything that involves my C functions not getting recognized in swift. I will also provide my full code: import IOKit public struct SMCVal_t { var datasize: UInt32 var datatype: UInt32 var data: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) } @_silgen_name("openSMC") func openSMC() -> Int32 @_silgen_name("closeSMC") func closeSMC() -> Int32 @_silgen_name("readSMC") func readSMC(key: UnsafePointer<CChar>?,val: UnsafeMutablePointer<SMCVal_t>) -> kern_return_t func convertAndPrintTempValue(key:UnsafePointer<CChar>?,scale: Character, showTemp: Bool ) -> Double? { let openSM = openSMC() guard openSM == 0 else { print("Failed to open SMC: \(openSM)") return 0.0; } let closeSM = closeSMC() guard closeSM == 0 else { print("could not close SMC: \(closeSM)") return 0.0; } var SMCValue = SMCVal_t(datasize: 0, datatype: 0, data:(0,0,0,0,0,0,0,0)) //initializing SMC value if let key = key { //check if nil. If not nil, proceed to code block execution let key = "TC0P" let keyCString = (key as NSString).utf8String //passing key as null terminated utf8 string let readSMCResult = readSMC(key: keyCString, val: &SMCValue) //call readSMC obj-C function, store result in "readSMCResult" if readSMCResult != KERN_SUCCESS { print("Failed to read SMC: \(readSMCResult)") } } if showTemp { //return nil if showTemp is false var convertRawToFahrenheit: Double = 0.0 withUnsafeMutablePointer(to: &SMCValue) { pointer in convertRawToFahrenheit = convertToFahrenheit(val: pointer) } print(String(format: "Temperature: %0.1f °%c", convertRawToFahrenheit )) return nil } else { print("could not convert temperature and format values in Fahrenheit") return nil } return 0.0; }
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
1 Replies
475 Views
I have an Xcode project that include a .c file and .h header file. I am getting a duplicate symbol error and I cannot pinpoint what part of my code is the issue or maybe if it's a configuration issue in my Xcode settings or not. Here's my .h header file code with my declarations: #define smc_h #include <stdint.h> #include <mach/mach.h> #include <IOKit/IOKitLib.h> typedef struct { uint32_t datasize; uint32_t datatype; uint8_t data[8]; } SMCVal_t; extern io_connect_t conn; kern_return_t openSMC(void); kern_return_t closeSMC(void); kern_return_t readSMC(char *key, SMCVal_t *val); double convertToFahrenheit(SMCVal_t *val); #endif /* smc_h */ And here is my .c implementation: #include "smc.h" #include <mach/mach.h> #include <IOKit/IOKitLib.h> io_connect_t conn; kern_return_t openSMC(void) { kern_return_t result; kern_return_t service; io_iterator_t iterator; service = IOServiceGetMatchingServices(kIOMainPortDefault, IOServiceMatching("AppleSMC"), &iterator); if(service == 0) { printf("error: could not match dictionary"); return 0; } result = IOServiceOpen(service, mach_task_self(), 0, &conn); IOObjectRelease(service); return 0; } kern_return_t closeSMC(void) { return IOServiceClose(conn); } kern_return_t readSMC(char *key, SMCVal_t *val) { kern_return_t result; uint32_t keyCode = *(uint32_t *)key; SMCVal_t inputStruct; SMCVal_t outputStruct; inputStruct.datasize = sizeof(SMCVal_t); inputStruct.datatype = 'I' << 24; //a left shift operation. turning the I into an int by shifting the ASCII value 24 bits to the left inputStruct.data[0] = keyCode; result = IOConnectCallStructMethod(conn, 5, &inputStruct, sizeof(SMCVal_t), &outputStruct, (size_t*)&inputStruct.datasize); if (result == kIOReturnSuccess) { if (val -> datasize > 0) { if (val -> datatype == ('f' << 24 | 'l' << 16 | 't' << 8 )) { //bit shifting to from 32bit operation associated with the ASCII charecters'f', 'l', and 't', sets datatype field. double temp = *(double *)val -> data; return temp; } } } return 0.0; } double convertToFahrenheit(SMCVal_t *val) { if(val -> datatype == ('f' << 24 | 'l' << 16 | 't' << 8 )) { //checking if val->datatype is equal to the result of the bit-shifting operation. double temp = *(double *)val -> data; return (temp * 9.0 / 5.0) + 32.0; } return 0.0; }
Posted
by Aor1105.
Last updated
.
Post marked as solved
2 Replies
335 Views
My Xcode project has a c file, a swift file, and a .h header file with my declarations but when I build my xcdc project I get all these unknown type errors that occur specifically in my .h file. I also get the error "failed to emit precompiled header" error in my Bridging-header-h file:
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
2 Replies
445 Views
Im getting a duplicate symbols error when I build my Xcode project and Xcode doesn't specify what or where in my code is there a duplicate: "1 duplicate symbols" "Showing Recent Issues Linker command failed with exit code 1 (use -v to see invocation) " #include <stdio.h> #include <IOKit/IOKitLib.h> typedef struct { uint32_t datasize; uint32_t datatype; uint8_t data[32]; } SMCVal_t; io_connect_t conn; kern_return_t openSMC(void) { kern_return_t result; kern_return_t service; io_iterator_t iterator; service = IOServiceGetMatchingServices(kIOMainPortDefault, IOServiceMatching("AppleSMC"), &iterator); if(service == 0) { printf("error: could not match dictionary"); return 0; } result = IOServiceOpen(service, mach_task_self(), 0, &conn); IOObjectRelease(service); return 0; } kern_return_t closeSMC(void) { return IOServiceClose(conn); } kern_return_t readSMC(char *key, SMCVal_t *val) { kern_return_t result; uint32_t keyCode = *(uint32_t *)key; SMCVal_t inputStruct; SMCVal_t outputStruct; inputStruct.datasize = sizeof(SMCVal_t); inputStruct.datatype = 'I' << 24; //a left shift operation. turning the I into an int by shifting the ASCII value 24 bits to the left inputStruct.data[0] = keyCode; result = IOConnectCallStructMethod(conn, 5, &inputStruct, sizeof(SMCVal_t), &outputStruct, (size_t*)&inputStruct.datasize); if (result == kIOReturnSuccess) { if (val -> datasize > 0) { if (val -> datatype == ('f' << 24 | 'l' << 16 | 't' << 8 )) { //bit shifting to from 32bit operation associated with the ASCII charecters 'f', 'l', and 't' float temp = *(float *)val -> data; return temp; } } } return 0.0; } double getTemperature(char *key) { SMCVal_t val; kern_return_t result; result = readSMC(key, &val); if(result == kIOReturnSuccess) { if (val.datasize > 0) { printf("val.datasize: %u\n", val.datasize); if (val.datatype != 0) { double temperature = (double)val.data[0]; return temperature; } } } return 0.0; } double convertToFahrenheit(double Fahrenheit) { return (Fahrenheit * (9.0 / 5.0)) + 32.0; } int main(void) { kern_return_t result; result = openSMC(); if(result == kIOReturnSuccess) { double temp = getTemperature("TC0P"); double temperatureInFahrenheit = convertToFahrenheit(temp); printf("temp: %.2f\n", temperatureInFahrenheit); result = closeSMC(); } return 0; }
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
2 Replies
446 Views
I have a C file for accessing the apple smc and I have the corresponding header file with my declarations in it but when I build my Xcode project I get the error: "ld: Undefined symbols: _getTemperature, referenced from: _main in getsmc.o clang: error: linker comm" #include &lt;stdio.h&gt; #include &lt;IOKit/IOKitLib.h&gt; typedef struct { uint32_t datasize; uint32_t datatype; uint8_t data[32]; } SMCVal_t; io_connect_t conn; kern_return_t openSMC(void) { kern_return_t result; kern_return_t service; io_iterator_t iterator; service = IOServiceGetMatchingServices(kIOMainPortDefault, IOServiceMatching("AppleSMC"), &amp;iterator); if(service == 0) { printf("error: could not match dictionary"); return 0; } result = IOServiceOpen(service, mach_task_self(), 0, &amp;conn); IOObjectRelease(service); return 0; } kern_return_t closeSMC(void) { return IOServiceClose(conn); } double getTemperature(char *key); kern_return_t readSMC(char *key, SMCVal_t *val) { kern_return_t result; uint32_t keyCode = *(uint32_t *)key; SMCVal_t inputStruct; SMCVal_t outputStruct; inputStruct.datasize = sizeof(SMCVal_t); inputStruct.datatype = 'I' &lt;&lt; 24; //a left shift operation. turning the I into an int by shifting the ASCII value 24 bits to the left inputStruct.data[0] = keyCode; result = IOConnectCallStructMethod(conn, 5, &amp;inputStruct, sizeof(SMCVal_t), &amp;outputStruct, (size_t*)&amp;inputStruct.datasize); if (result == kIOReturnSuccess) { if (val -&gt; datasize &gt; 0) { if (val -&gt; datatype == ('f' &lt;&lt; 24 | 'l' &lt;&lt; 16 | 't' &lt;&lt; 8 )) { float temp = *(float *)val -&gt; data; return temp; } } } return 0.0; } int main(void) { kern_return_t result; result = openSMC(); if(result == kIOReturnSuccess) { double temp = getTemperature("TC0P"); printf("temp: %.2f\n", temp); result = closeSMC(); } return 0; }
Posted
by Aor1105.
Last updated
.
Post marked as solved
7 Replies
503 Views
Im trying to create a function to retrieve my Mac's RAM usage but I get alerts saying essentially that my 'scanDouble' and 'scanCharecters(from:into:)' methods have been depreciated and Xcode also throw me these alerts if I compile this code. what are the newer alternatives to these methods? import Foundation class RAMUsage { let processInfo = ProcessInfo.processInfo func getRAM() { let physicalMemory = processInfo.physicalMemory let formatter = ByteCountFormatter() formatter.countStyle = .memory let formattedMemoryUsage = formatter.string(fromByteCount: Int64(physicalMemory)) parseAndPrint(formattedMemoryUsage: formattedMemoryUsage) } func parseAndPrint(formattedMemoryUsage: String) { print("Formatted RAM usage: \(formattedMemoryUsage)") if let gigsOfRAM = parseFormattedRAMUsage(formattedUsage: formattedMemoryUsage) { print("RAM Usage in Gigabytes: \(gigsOfRAM) GB") } else { print("Could not retrieve or parse RAM usage") } } func parseFormattedRAMUsage(formattedUsage: String) -> Double? { let scanner = Scanner(string: formattedUsage) var value: Double = 0.0 var unit: NSString? if scanner.scanDouble(&value) { scanner.scanCharacters(from: .letters, into: &unit) if let unitString = unit as String?, unitString.lowercased() == "GB" { print("Parsed RAM Usage: \(value) GB") return value } else { print("could not parse and return value") } } return nil } }
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
1 Replies
364 Views
0 I am building a simple macOS menu bar app in swift that displays my machine's CPU temperature and I'm just testing out my function to make sure it can retrieve the temp and display it in the Xcode terminal but instead my error handler messages are triggered indicating an issue retrieving my machines CPU temp data "CPU temp °F could not be retrieved temps couldnot be displayed" import Cocoa import IOKit import IOKit.ps class CPUTempWithServiceMatching { static func getCPUTemp() -> Double? { let dictionaryMatching = IOServiceMatching("AppleSMC") var service = IOServiceGetMatchingService(kIOMainPortDefault, dictionaryMatching) var temp = "0.0" if service != 0 { let key = "TC0P" //thermal zone zero proxy if let result = IORegistryEntryCreateCFProperty(service, key as CFString, kCFAllocatorDefault, 0 ) { temp = (result.takeUnretainedValue() as! NSNumber).doubleValue.description IOObjectRelease(service) if let CPUTemp = Double(temp) { print("CPU Temp: \(CPUTemp) °F") return(CPUTemp) } } print("CPU temp °F could not be retrieved") } return nil } } @main struct program { static func main() { if let cpuTemp = CPUTempWithServiceMatching.getCPUTemp() { print("cpu temp\(cpuTemp) °F") } else { print("temps couldnot be displayed") } } }
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
0 Replies
308 Views
Im creating a simple chatbox using an api caller library I created and imported but it looks like Xcode is not recognizing the modules as I get multiple "no member" errors for the 'ChatClient' module. `import SwiftUI import openaiLibrary final class ViewModel: ObservableObject { private var openAI: openaiLibrary.ChatClient init(apiKey: String) { let config = openaiLibrary.ChatClient.OpenAIEndpointProvider.makeDefaultKey(api_key: apiKey, endpointProvider: openaiLibrary.ChatClient.OpenAIEndpointProvider()) self.openAI = openaiLibrary.ChatClient(apiKey: apiKey, openaiEndpoint: config.baseURL) } public func sendAIRequest(with search: String, completion: @escaping(Result<String,Error>) -> Void) { openAI?.sendCompletion(with: search) { result in switch result { case .success(let response): if let text = response.choices.first?.text { completion(.success(text)) } else { completion(.failure(NSError(domain: "error", code: 1, userInfo: [NSLocalizedDescriptionKey: "No response found"]))) } case .failure(let error): completion(.failure(error)) } } } struct ContentView: View { var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .padding() } } #Preview { ContentView() } } ` I can also provide my source code for my api caller that my openaiLibrary package dependency uses to make sure everything is defined correctly so that Xcode recognizes everything, due to character constraints I wasn't able to fit it in this post.
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
0 Replies
355 Views
in anticipation for the action button that will be coming on the iphone 15pro, am I able to build in functionality and support that takes advantage of the action button for my app in Xcode? I know there are the few 3rd party apps for watchOS that have built in support for the action button on the Apple Watch ultra but I wanted to get more information on this from other developers.
Posted
by Aor1105.
Last updated
.
Post not yet marked as solved
0 Replies
388 Views
This is my first Xcode application, I'm building a simple MacOS chatbox application that uses python scrips, PythonKit, and swift to handle serverSide operations and accessing open's api and is meant to trigger these two methods I have in a function called executeProcess() that is meant to invoke other functions in another file when a question is types in the text field and the 'enter' key on a keyboard is hit via the onCommit function, however im getting no console output. here is my relevant code from my contentView.swift file I can provide more code from other files if needed.(I will just be showing the non SwiftUI specific code here) import Cocoa import Foundation import PythonKit import AppKit protocol runPyRunnable { func runPyServer(completion: @escaping(String) -> Void) func sendRequest(userInput: String, completion: @escaping(String) -> Void) } func runPyServer() -> String { print("server run") return "server run" } struct MyPyTypePlaceHolder: runPyRunnable { func runPyServer(completion: @escaping(String) -> Void) { } func sendRequest(userInput: String, completion: @escaping (String) -> Void) { } } struct ContentView: View { var ViewController: runPyRunnable? = MyPyTypePlaceHolder() as? runPyRunnable @State private var text: String = "" @State private var filePath = "" @State private var inputText = "" var body: some View { makeContent() .onAppear{ NSApp.mainWindow?.makeFirstResponder(NSApp.mainWindow?.contentView) } } ZStack { Spacer() TextField("Ask Mac GPT...", text: $inputText, onCommit: { executeProcess(withInput: inputText) { response in print(response) } }) .font(Font.custom("Futura", size: 17.4)) .padding(.horizontal, 25) .padding(.vertical, 15) .background(Color.white) .cornerRadius(29) .overlay( RoundedRectangle(cornerRadius: 27.9).stroke(Color.gray, lineWidth: 1.0) ) .offset(y: -200) .padding(.horizontal, 35) } } func executeProcess(withInput input: String, completion: @escaping (String) -> Void ) { DispatchQueue.global().async { DispatchQueue.main.async { guard !input.isEmpty else { print("TextField is empty, enter input in the text field") return } if let myPyTypeInstance = self.ViewController { myPyTypeInstance.runPyServer { responseFromRunPyServer in myPyTypeInstance.sendRequest(userInput: input) { responseFromSendRequest in completion(responseFromSendRequest) } } } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } }
Posted
by Aor1105.
Last updated
.