ImportObjC编译指示

importc pragma for C 类似, importobjc 编译指示可用于导入 Objective C 方法。 生成的代码使用Objective C 方法调用语法: [obj method param1: arg]. 除了 headeremit 编译指示之外,这允许与Objective C中编写的库的 草率 对接:

  1. # 和GNUStep对接的反例 ...
  2.  
  3. {.passL: "-lobjc".}
  4. {.emit: """
  5. #include <objc/Object.h>
  6. @interface Greeter:Object
  7. {
  8. }
  9.  
  10. - (void)greet:(long)x y:(long)dummy;
  11. @end
  12.  
  13. #include <stdio.h>
  14. @implementation Greeter
  15.  
  16. - (void)greet:(long)x y:(long)dummy
  17. {
  18. printf("Hello, World!\n");
  19. }
  20. @end
  21.  
  22. #include <stdlib.h>
  23. """.}
  24.  
  25. type
  26. Id {.importc: "id", header: "<objc/Object.h>", final.} = distinct int
  27.  
  28. proc newGreeter: Id {.importobjc: "Greeter new", nodecl.}
  29. proc greet(self: Id, x, y: int) {.importobjc: "greet", nodecl.}
  30. proc free(self: Id) {.importobjc: "free", nodecl.}
  31.  
  32. var g = newGreeter()
  33. g.greet(12, 34)
  34. g.free()

需要告诉编译器生成Objective C(命令 objc )以使其工作。当编译器发出Objective C代码时,定义条件符号 objc