2017-12-18 5 views

답변

4

https://github.com/lavoiesl/osx-cpu-temp/blob/master/smc.c을 사용하면 효과가 있습니다.

double calculate() 
{ 
    SMCOpen(); 
    double temperature = SMCGetTemperature(SMC_KEY_CPU_TEMP); 
    SMCClose(); 
    temperature = convertToFahrenheit(temperature); 
    return temperature; 
} 

이 ObjC 래퍼를 쓰기 :
#import "SMCObjC.h" 
#import "smc.h" 
@implementation SMCObjC 

+(double)calculateTemp { 
    return calculate(); 
} 

@end 

이 스위프트 브리지 헤더에 헤더를 추가

단순화 main() 다른 일에 : 당신은 몇 가지를해야한다. 응용 프로그램이 샌드 박스되면

// 
// Use this file to import your target's public headers that you would like to expose to Swift. 
// 
#import "SMCObjC.h" 

는 AppleSMC에 대한 보안 면제를 추가 스위프트에서

<dict> 
    <key>com.apple.security.app-sandbox</key> 
    <true/> 
    <key>com.apple.security.files.user-selected.read-only</key> 
    <true/> 
    <key>com.apple.security.temporary-exception.sbpl</key> 
    <array> 
     <string>(allow iokit-open)</string> 
     <string>(allow iokit-set-properties (iokit-property &quot;AppleSMC&quot;))</string> 
     <string>(allow mach-lookup (global-name &quot;com.apple.AssetCacheLocatorService&quot;))</string> 
    </array> 
</dict> 
</plist> 

전화를.

https://www.dropbox.com/sh/lpe9bm08s2ynoob/AAA-N_br7aqRxJRks53FYK0fa?dl=0

+0

그것은 작동합니다

import Cocoa class ViewController: NSViewController { override func viewDidLoad() { super.viewDidLoad() let temp = SMCObjC.calculateTemp() print("I got \(temp) in swift") } } 

여기 내 샘플 프로젝트입니다! 고맙습니다 –

관련 문제