我正在开发我的第一个MacOS应用程序,我将Objective-C与Swift联系起来,我还有很多需要学习的东西。
我认为我已经声明并链接了所有内容,但由于某种原因,编译器会提示此错误。
Use of undeclared identifier 'EventToSend'
EventToSend是一个AEEventID类型,我声明在函数原型和Header文件中,桥接头文件也有import语句。我要做的是将Timer实现为休眠和重启函数,以便等待计时器在执行之前到期。
有人可以指出我做错了吗?
classFunctions.m
#include <stdio.h>
#include <CoreServices/CoreServices.h>
#include <Carbon/Carbon.h>
#include <Foundation/Foundation.h>
#import "Header.h"
@implementation intOb : NSObject
static OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend);
int executeSleep(void);
int executeRestart(void);
int executeShutDown(void);
- (OSStatus) SendAppleEventToSystemProcess{
AEAddressDesc targetDesc;
static const ProcessSerialNumber kPSNOfSystemProcess = { 0, kSystemProcess };
AppleEvent eventReply = {typeNull, NULL};
AppleEvent appleEventToSend = {typeNull, NULL};
OSStatus error = noErr;
error = AECreateDesc(typeProcessSerialNumber, &kPSNOfSystemProcess,
sizeof(kPSNOfSystemProcess), &targetDesc);
if (error != noErr)
{
return(error);
}
//the error appears here
error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc, kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend);
AEDisposeDesc(&targetDesc);
if (error != noErr)
{
return(error);
}
error = AESend(&appleEventToSend, &eventReply, kAENoReply,
kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
AEDisposeDesc(&appleEventToSend);
if (error != noErr)
{
return(error);
}
AEDisposeDesc(&eventReply);
return(error);
}
- (int) executeSleep{
OSStatus error = noErr;
error = SendAppleEventToSystemProcess(kAESleep);
if (error == noErr)
{
printf("Computer is going to sleep!\n");
return error;
}
else
{
printf("Computer wouldn't sleep");
return error;
}
}
- (int) executeRestart{
OSStatus error = noErr;
error = SendAppleEventToSystemProcess(kAERestart);
if (error == noErr)
{
printf("Computer is going to restart!\n");
return error;
}
else
{
printf("Computer wouldn't restart\n");
return error;
}
}
- (int) executeShutDown{
OSStatus error = noErr;
error = SendAppleEventToSystemProcess(kAEShutDown);
if (error == noErr)
{
printf("Computer is going to shutdown!\n");
return error;
}
else
{
printf("Computer wouldn't shutdown\n");
return error;
}
}
@end
Header.h
#ifndef Header_h
#define Header_h
#import <Foundation/Foundation.h>
@interface intOb : NSObject
- (OSStatus) SendAppleEventToSystemProcess;
- (int) executeSleep;
- (int) executeRestart;
- (int) executeShutDown;
@end
#endif /* Header_h */
FTEST-桥接-Header.h
#import "Header.h"
ViewController.swift
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let obj: intOb = intOb()
var sequentialAction: Timer!
sequentialAction = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(obj.executeSleep), userInfo: nil, repeats: true)
sequentialAction.invalidate()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
答案 0 :(得分:0)
在classFunctions.h
中添加此代码@property(非原子,强)AEEventID * EventToSend;