我用kotlin写一个扩展方法
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler
{
if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @"10.0" ) )
{
NSLog( @"iOS version >= 10. Let NotificationCenter handle this one." );
return;
}
NSLog( @"HANDLE PUSH, didReceiveRemoteNotification: %@", userInfo );
if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
{
NSLog( @"INACTIVE" );
completionHandler( UIBackgroundFetchResultNewData );
}
else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
{
NSLog( @"BACKGROUND" );
completionHandler( UIBackgroundFetchResultNewData );
}
else
{
NSLog( @"FOREGROUND" );
completionHandler( UIBackgroundFetchResultNewData );
}
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSLog( @"Handle push from foreground" );
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler
{
NSLog( @"Handle push from background or closed" );
}
当我在package com.zhongan.zachat.extention
import android.content.Context
import android.widget.Toast
/**
* Created by Carl on 2016/12/1.
*
*
*/
fun Context.toastLong(msg:String) = Toast.makeText(this,msg,Toast.LENGTH_LONG).show()
fun Context.toastshort(msg:String) = Toast.makeText(this,msg,Toast.LENGTH_SHORT).show()
致电kotlin activity
时
没问题。
但在toastLong("test")
IDE中说无法找到这种方法。
如何在java代码中调用kotlin扩展方法
答案 0 :(得分:6)
扩展实际上并不会修改它们扩展的类。
应该注意,不能从对象类调用扩展,因为原始类仍然是相同的。 (因此Context不会神奇地拥有额外的函数,因此无法使用Java中的Context.functionName调用它)
您应该可以使用以下方式调用它:
com.zhongan.zachat.extention.<fileName>.toastLong(ctx,"string")
e.g。如果文件名为kotlinFile.kt:
com.zhongan.zachat.extention.KotlinFileKt.toastLong(ctx,"string")