如何正确声明和使用NSDictionary?

时间:2021-02-12 12:05:17

Following an answer I found here: https://*.com/a/18121292/1701170, I have the following code:

我在这里找到答案:https://*.com/a/18121292/1701170,我有以下代码:

bool accessibilityEnabled = false;

// Check and make sure assistive devices is enabled.
if (AXIsProcessTrustedWithOptions != NULL) {
    // 10.9 and later
    NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt: @YES};
    accessibilityEnabled = AXIsProcessTrustedWithOptions((CFDictionaryRef)options);
} else {
    // 10.8 and older
    if (AXAPIEnabled() == true) {
        accessibilityEnabled = true;
    }
}

if (accessibilityEnabled) {
    // do something
}

The error I get is as follows:

我得到的错误如下:

[apply] error: use of undeclared identifier 'NSDictionary'; did you mean 'UseDictionary'?
[apply]         NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt: @YES};
[apply]         ^~~~~~~~~~~~
[apply]         UseDictionary

Do I have to import NSDictionary?

我必须导入NSDictionary吗?

The imports at the top of the file are as follows:

文件顶部的导入如下:

 #include <pthread.h>
 #include <sys/time.h>

 #include <ApplicationServices/ApplicationServices.h>

 #include "NativeErrors.h"
 #include "NativeGlobals.h"
 #include "NativeHelpers.h"
 #include "NativeThread.h"
 #include "NativeToJava.h"
 #include "OSXInputHelpers.h"

This is my first time looking at Objective-C.

这是我第一次看Objective-C。

1 个解决方案

#1


3  

The project you linked looks like a plain C project to me. NSDictionary is an Objective-C class and part of the Foundation framework from Apple. You have to link Foundation.framework and make sure it is imported in your source. If you're just experimenting, your best bet would probably be to just create a new project in Xcode using the Command Line Tool (of type Foundation) template.

您链接的项目对我来说就像一个普通的C项目。 NSDictionary是一个Objective-C类,是Apple的Foundation框架的一部分。您必须链接Foundation.framework并确保它已在您的源中导入。如果你只是在试验,你最好的选择可能是使用Command Line Tool(类型为Foundation)模板在Xcode中创建一个新项目。

#1


3  

The project you linked looks like a plain C project to me. NSDictionary is an Objective-C class and part of the Foundation framework from Apple. You have to link Foundation.framework and make sure it is imported in your source. If you're just experimenting, your best bet would probably be to just create a new project in Xcode using the Command Line Tool (of type Foundation) template.

您链接的项目对我来说就像一个普通的C项目。 NSDictionary是一个Objective-C类,是Apple的Foundation框架的一部分。您必须链接Foundation.framework并确保它已在您的源中导入。如果你只是在试验,你最好的选择可能是使用Command Line Tool(类型为Foundation)模板在Xcode中创建一个新项目。