D言語からObjective-CとCocoaAPIを使いたい
Objective-C 文字列 NSString、C言語文字列、D言語文字列の出力
D言語で Mac の NSString を使用して、OS 情報を表示します。
digitalmars.D - Can't get Objective-C class method to compile. これは、Foundation.framework で充分です。
2015年の内容でしたので、selector を @selector に修正等を行いました。
(base) hidhok % dmd -L-framework -LCocoa string_out.d
(base) hidhok % ./string_out
2022-12-26 .... osxversion[..:..] MacOS (NSString) = Version 12.6.1 (Build ...)
2022-12-26 .... osxversion[..:..] NSString strCopy = Version 12.6.1 (Build ...)
MacOS(Clang string) = Version 12.6.1 (Build ...)
MacOS(Dlang string) = Version 12.6.1 (Build ...)
(base) hidhok %
サンプルコード
Powered by Prism
string_out/dub.json
{
"authors": ["hidhok"],
"description": "A D/Objective-C Cocoa application.",
"name": "string_out",
"dflags": ["-L-framework", "-LCocoa"],
"sourceFiles": ["../cocoa/strings"]
}
string_out/source/app.d
/**
* string_out/source/app.d (osxversion.d) Version 0.0.2
* dmd -L-framework -LCocoa stringOut.d
* dmd -L-framework -LFoundation stringOut.d
*/
module string_out.main;
import core.attribute : optional, selector;
import std.conv : to;
import std.stdio : writeln, printf;
import cocoa.strings;
void main() {
NSString str = NSProcessInfo.processInfo.operatingSystemVersionString();
NSLog("MacOS (NSString) = %@".toNs, str);
NSLog("NSString strCopy = %@".toNs, str.strCopy);
printf("MacOS(Clang string) = %s\n", str.toCs);
writeln("MacOS(Dlang string) = ", str.toDs);
}
extern (Objective-C)
extern class NSProcessInfo {
static NSProcessInfo processInfo() @selector("processInfo");
NSString operatingSystemVersionString() @selector("operatingSystemVersionString");
}
cocoa/strings.d
- Log
- 2024-06-09 修正しました。
- 2023-01-23 修正しました。
- 2022-12-26 修正しました。
- 2022-01-01 upload