-1

I'm developing my first MacOS application where I'm linking Objective-C with Swift, I still got a lot to learn.

I think that I have everything declared and linked, but for some reason the compiler prompts this error.

Use of undeclared identifier 'EventToSend'

EventToSend is an AEEventID type, and I declared that in the function prototype and in the Header file, the bridging header file has the import statement too. What I'm trying to do is to implement a Timer into the sleep and restart functions so it would wait for the timer to expire before execution.

Could someone point out what I'm doing wrong?

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-Bridging-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.
        }
    }
}
xedge
  • 3
  • 5
  • you actually know that your method accepts no parameter, right? – holex Oct 24 '17 at 14:33
  • I've put them in every declaration and in the prototype it says, 'expected method body' though it's already there and in the header it says 'Expected ';' after method prototype', and it's there too. – xedge Oct 24 '17 at 14:36
  • from your header file: `-(OSStatus)SendAppleEventToSystemProcess;`, no parameter; you may need to add a parameter to the signature like: `-(OSStatus)SendAppleEventToSystemProcess:(AEEventID)EventToSend;`, and you need to implement that method... m'kay? – holex Oct 24 '17 at 14:40
  • Yeah... I was actually looking to that when you pointed that out, but do you know if I need to actually reference the parameter with a '*'? – xedge Oct 24 '17 at 14:43
  • it depends on what `AEEventID` is actually... is it an object? is it a primitive? – holex Oct 24 '17 at 14:48
  • It's presumably a primitive, an unsigned int type of four characters to denote certain apple event. – xedge Oct 24 '17 at 14:54
  • then probably you'll need to pass the value rather then the reference. – holex Oct 24 '17 at 14:55
  • I've eliminated the static identifier because it was causing definition problems, but now it prompts this error: `'Undefined symbols for architecture x86_64: "_SendAppleEventToSystemProcess", referenced from: -[intOb executeSleep] in classFunctions.o -[intOb executeRestart] in classFunctions.o -[intOb executeShutDown] in classFunctions.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)'` – xedge Oct 24 '17 at 15:12

1 Answers1

0

add this code in classFunctions.h

@property (nonatomic, strong) AEEventID *EventToSend;

Grenoblois
  • 503
  • 1
  • 5
  • 19
  • It prompts the following error: `Property with 'retain (or strong)' attribute must be of object type` – xedge Oct 24 '17 at 15:32
  • @property (nonatomic, strong) NSString *EventToSend; – Grenoblois Oct 24 '17 at 15:36
  • Now it shows this error: `Undefined symbols for architecture x86_64: "_SendAppleEventToSystemProcess", referenced from: -[intOb executeSleep] in classFunctions.o -[intOb executeRestart] in classFunctions.o -[intOb executeShutDown] in classFunctions.o ld: symbol(s) not found for architecture x86_64 ` – xedge Oct 24 '17 at 15:53