google / google/j2objc

New @ObjectiveCType annotation

Open
#1,026 0 comments 4 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
6k
Forks
999
Avg merge
19h 20m
Merged PRs (30d)
14

Description

[yegor.k wrote on j2objc-discuss](https://groups.google.com/forum/#!topic/j2objc-discuss/DaDjhW4ePtA):

Consider the following code example:
```
public class MyClass
{
Object view;
public native void setAlpha(float alpha)
/*-[
[(UIView*)view_ setAlpha:alpha];
]-*/;
}
```

Obviously this works. But my suggestion is to use an annotation to specify the type to use in ObjC code, for example:

```
public class MyClass
{
@ObjectiveCType("UIView*") Object view;
public native void setNativeAlpha(float alpha, int animate)
/*-[
[view_ setAlpha:alpha];
]-*/;
}
```

This way there is no need to cast in the native code, so the code looks cleaner and there is now type safety on the ObjC side! A win-win.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.