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;
}