[英]Obtaining a list of class names with objc_getClassList in JavaScript for Automation
By using ObjC.bindFunction
in JXA, we can obtain an integer result from objc_getClassList
. 通过在JXA中使用ObjC.bindFunction
,我们可以从objc_getClassList
获得一个整数结果。
Does anyone understand the types and bridging issues well enough to find a route to getting a list of class name strings returned by objc_getClassList
into the JavaScript for Automation JSContext ? 是否有人对类型和桥接问题了解得足够好,以找到一条途径,以将由objc_getClassList
返回的类名字符串列表导入JavaScript for Automation JSContext?
(The code below returns only an [Object Ref]
string) (下面的代码仅返回[Object Ref]
字符串)
(() => {
'use strict';
ObjC.import('stdlib');
ObjC.bindFunction('CFMakeCollectable', [ 'id', [ 'void *' ] ]);
ObjC.bindFunction('objc_getClassList', ['int', ['void *', 'int']]);
var classes = Ref();
const intClasses = $.objc_getClassList(null, 0);
$.objc_getClassList(classes, intClasses);
$.CFMakeCollectable(classes);
return [intClasses, classes];
//-> [11411, [object Ref]]
})();
The objc_getClassList
function is expecting us to provide it with a buffer of memory to copy the class list into. objc_getClassList
函数希望我们为其提供一个内存缓冲区,以将类列表复制到其中。 Normally, JXA would treat the return type of malloc as a C array of unsigned chars, but using bindFunction
we can cast malloc's return type to a C array of pointers, and make objc_getClassList
's first argument match that type. 通常,JXA会将malloc的返回类型视为无符号字符的C数组,但是使用bindFunction
我们可以将malloc的返回类型转换为C指针数组,并使objc_getClassList
的第一个参数与该类型匹配。 Then, it's just a matter of indexing into the buffer (of type Ref) and passing that value into class_getName
. 然后,只需索引到缓冲区(类型为Ref)并将该值传递给class_getName
。
ObjC.bindFunction('objc_getClassList', ['int', ['void**', 'int']])
ObjC.bindFunction('malloc', ['void**', ['int']])
ObjC.bindFunction('class_getName', ['char *', ['void*']])
const numClasses = $.objc_getClassList(undefined, 0)
const classes = $.malloc(8*numClasses)
$.objc_getClassList(classes, numClasses)
for (i=0; i<numClasses; i++) {
console.log("classes[" + i + "]: " + $.class_getName(classes[i]))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.