J2ObjC 的使用

——只灯片笺

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

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

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

img_fc330cca1b0630aa69698eae1c4e61d5.png
Paste_Image.png

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

img_e66a0720c47aed8cb0b59547c95ecb3d.png
Paste_Image.png

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

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

img_5d08dfd1f3ca57bdc532e37273b1d1fe.png
Paste_Image.png

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

img_d9c9d51e5255b2fbb649d5de49580e60.png
Paste_Image.png

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

img_8d918091b23457a9db525990f5e05eba.png
Paste_Image.png

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

img_28f2daa0f9dcbb1e650096a2ea85d890.png
Paste_Image.png

8.运行效果如下:

img_4b30252297f17c1c1a255c366f5906c0.png
Paste_Image.png

当然,此处有个前提:

img_db6fac4064864f95d473cf7b1df51837.png
Paste_Image.png

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

img_df65fbd4e5a70c1233d8ac7372604c66.png
Paste_Image.png

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

img_459c969cfa17075e93dffd292a1f6bed.png
Paste_Image.png

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

img_bb7df1459d038b2f1e7df2ebcf48634c.png
Paste_Image.png
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值