J2ObjC 的使用

2018.11.05_更新: 此文章已经很久,笔者已忘记具体相关这篇笔记的具体实现,如有此问题需求者,请移步 Google 的 J2Objc 相关文档进行阅读。

Ps: 根据笔者自己的使用测试对比,发现 Xcode 在实现 J2ObjC 时,比 Eclipse 实现 J2ObjC 成功率更高,且更方便快捷。

So,此处先讲解 Xcode 中 J2ObjC 的实现。


Xcode 中配置并使用 J2ObjC,实现 Java 代码转为 Objective-c 代码

1. 首先,到 github 上下载文件。(下载链接点这里)

Paste_Image.png

2. 把 .java 文件拖进工程中(尽量保证 .java 文件在文件夹内目录清晰,导入要使用的 .java 文件可以直接把整个文件夹丢进工程里)

Paste_Image.png

3. (此时肯定是编译不通过的,因为还未进行任何配置,Xcode 压根儿就不认识 Java),配置如下,先添加第一个要配置的东西。(官方的教程链接在此)
顺序:点击工程名--->Build Rules--->+(点击加号) ,然后,添加内容如下:

Paste_Image.png

4. 完成操作后,点击工程名→Build Settings→搜索框内搜索→search path
(此处是设定search path,是为了知道转码后的文件在哪里)

Paste_Image.png

5. 删除搜索框内的内容:search path ,输入搜索内容:library search

Paste_Image.png

6. 删除搜索框内的内容:library search ,输入搜索内容:other linker

Paste_Image.png

7. 然后,就可以直接调用了,像这样:

Paste_Image.png

8. 运行效果如下:

Paste_Image.png

当然,此处有个前提:

Paste_Image.png

另外需要注意的是,Java 文件的名字,和放法调用时的名字问题
调用时是这样:

Paste_Image.png

Java 文件中是这样:(类名和文件名保持一样)

Paste_Image.png

Test+Hello 就是类名,mark 变为类方法名。而如果是有参有返回值的,就是变为对象方法。详细情况,可以点进 .m 文件中调用。(下图是,转换后的 .h 文件示例)

Paste_Image.png



作者:只灯片笺
链接:https://www.jianshu.com/p/f58ee097e773
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用运行时API可以定位内存泄露,具体可以通过以下步骤实现: 1. 在 Xcode 中选择 Product -> Scheme -> Edit Scheme,然后在左侧选择 Run,接着在 Arguments Passed On Launch 中添加环境变量:MallocStackLoggingNoCompact,并将其值设为 1。 2. 在代码中添加以下代码: ``` #import <objc/runtime.h> void checkMemoryLeak() { unsigned int count = 0; Class *classes = objc_copyClassList(&count); for (unsigned int i = 0; i < count; i++) { Class cls = classes[i]; if (class_respondsToSelector(cls, @selector(doesNotRecognizeSelector:))) { // This class has no method implementations of its own. continue; } unsigned int methodCount = 0; Method *methods = class_copyMethodList(cls, &methodCount); for (unsigned int j = 0; j < methodCount; j++) { Method method = methods[j]; SEL selector = method_getName(method); if (selector == @selector(alloc) || selector == @selector(new)) { // Skip alloc and new methods to avoid false positives. continue; } NSString *methodName = NSStringFromSelector(selector); if ([methodName hasPrefix:@"init"]) { // Skip init methods to avoid false positives. continue; } IMP imp = method_getImplementation(method); if (imp == NULL) { // Skip methods with no implementation. continue; } if (strstr(methodName.UTF8String, "copy") != NULL || strstr(methodName.UTF8String, "mutableCopy") != NULL) { // Skip copy and mutableCopy methods to avoid false positives. continue; } if (strstr(methodName.UTF8String, "retain") != NULL) { // Skip retain methods to avoid false positives. continue; } if (strstr(methodName.UTF8String, "release") != NULL || strstr(methodName.UTF8String, "autorelease") != NULL) { // Skip release and autorelease methods to avoid false positives. continue; } // Call the method with a nil argument to create an object without retaining it. ((id (*)(id, SEL, ...))method_invoke)(cls, selector, nil); } free(methods); } free(classes); } ``` 3. 在代码中调用 checkMemoryLeak 函数,这个函数会递归遍历所有类及其方法,并在不保留对象的情况下调用方法,从而创建对象并触发内存分配。如果有内存泄漏,可以通过 Instruments 工具捕获堆栈信息并进行分析。 需要注意的是,这个方法只是一种定位内存泄漏的手段,具体的问题还需要根据具体情况进行分析和解决。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值