将 Kotlin 中的 `Unit` 转换为 Java 中的 `String`。

huangapple 未分类评论45阅读模式
英文:

covert kotlin's Unit to Java's String

问题

public String BeaconValue() {
    ProximityObserver proximityObserver =
            new ProximityObserverBuilder(mContext, ((MyApplication) mContext).cloudCredentials)
                    .onError(new Function1<Throwable, Unit>() {
                        @Override
                        public Unit invoke(Throwable throwable) {
                            Log.e("desks", "proximity observer error: " + throwable);
                            return null;
                        }
                    })
                    .withBalancedPowerMode()
                    .build();
    ProximityZone zone = new ProximityZoneBuilder()
            .forTag("desks")
            .inNearRange()
            .onEnter(new Function1<ProximityZoneContext, Unit>() {
                @Override
                public Unit invoke(ProximityZoneContext proximityZoneContext) {
                    NotificationsManager.this.beaconValue = proximityZoneContext.getAttachments().get("desk-owner");
                    return null;
                }
            })
            .build();
    proximityObserver.startObserving(zone);

    return this.beaconValue;
}

// Getting the beacon attachment value string
public String getBeaconValue() {
    beaconValue = BeaconValue();
    return beaconValue;
}
英文:
  public String BeaconValue() {
    ProximityObserver proximityObserver =
            new ProximityObserverBuilder(mContext, ((MyApplication) mContext).cloudCredentials)
                    .onError(new Function1&lt;Throwable, Unit&gt;() {
                        @Override
                        public Unit invoke(Throwable throwable) {
                            Log.e(&quot;desks&quot;, &quot;proximity observer error: &quot; + throwable);
                            return null;
                        }
                    })
                    .withBalancedPowerMode()
                    .build();
    ProximityZone zone = new ProximityZoneBuilder()
            .forTag(&quot;desks&quot;)
            .inNearRange()
            .onEnter(new Function1&lt;ProximityZoneContext, Unit&gt;() {
                @Override
                public Unit invoke(ProximityZoneContext proximityZoneContext) {
                    NotificationsManager.this.beaconValue = proximityZoneContext.getAttachments().get(&quot;desk-owner&quot;);
                    return null;;
                }
            })
            .build();
    proximityObserver.startObserving(zone);

    return this.beaconValue;
}

// Getting the beacon attachment value string
public String getBeaconValue() {
    beaconValue = BeaconValue(beaconValue);
    return beaconValue;
}

so this is the code here, currently I have the Unit, but after some research, I found out that Unit is implicit so it does not need to return anything and it does not really return anything.
However, I want to return the beaconValue in this function, so I made this new function to get the implicit Unit from the beaconvalue function, it does not work. Wondering why.
(I have tried to change the Unit to String, but that is not the way it gives error. since on enter does not support String (Java))
Thank you.

updated.

答案1

得分: 0

这个方法返回 Unit,因为你在 Function1 对象的返回类型上声明了它。如果 onEnter 函数允许任何类型,请将 Function1 声明中的类型更改为 <ProximityZoneBuilder, String>。如果它特定需要 Unit,那么你必须使用 Unit

英文:

The method returns Unit because you have declared it as the return type on the Function1 object. Change the types in the Function1 declaration to&lt;ProximityZoneBuilder, String&gt; if the onEnter function allows for any type. If it specifically requires Unit then you must use Unit.

huangapple
  • 本文由 发表于 2020年4月9日 22:48:00
  • 转载请务必保留本文链接:https://java.coder-hub.com/61123893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定