用法/注射剂:
import java.util.Arrays;
class Example {
void method(Object... args) {
System.out.println(Arrays.toString(args));
}
public static void main(String[] args) {
Example e = new Example();
e.method(1, "foo", 2, "bar", 3);
e.method(true, "foo", 'c', 'd', new Boolean(false), false);
}
}
我的模块
@Inject
@field:Named("MyStringSaverFunction")
lateinit var stringSaverFunction: (String) -> Unit
错误详细信息:
@Module(includes = [AbcModule.UiBinding::class])
object AbcModule {
@Module
interface UiBinding {
@PerFragment
@ContributesAndroidInjector
fun provideFragment(): AbcFragment
}
@Provides
@JvmStatic
@Singleton
@Named("AbcSharedPrefs")
fun provideAbcSharedPrefs(context: Context) : SharedPreferences {
return context.getSharedPreferences("AbcSharedPrefs", Context.MODE_PRIVATE)
}
@Provides
@JvmStatic
@Singleton
@Named("MyStringSaverFunction")
fun provideDfIntentSaver(@Named("AbcSharedPrefs") sharedPreferences: SharedPreferences): (String) -> Unit {
return { theString: String ->
sharedPreferences.edit().putString("TheKey", theString).apply()
}
}
}
答案 0 :(得分:2)
在注入位置使用@JvmSuppressWildcards
来匹配签名-
@Inject
@field:Named("MyStringSaverFunction")
@JvmSuppressWildcards
lateinit var stringSaverFunction: (String) -> Unit