我正在尝试 hilt,我想注入 moshi 进行序列化和反序列化。
以下是来自 github Repo 的代码示例,该示例未使用 di:
open class InfoTypeConverter {
private val moshi = Moshi.Builder().build() //not using dependency injection
@TypeConverter
fun fromString(value: String): PokemonInfo.Type? {
val adapter: JsonAdapter<PokemonInfo.Type> = moshi.adapter(PokemonInfo.Type::class.java)
return adapter.fromJson(value)
}
@TypeConverter
fun fromInfoType(type: PokemonInfo.Type): String {
val adapter: JsonAdapter<PokemonInfo.Type> = moshi.adapter(PokemonInfo.Type::class.java)
return adapter.toJson(type)
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试随机的东西来现场注入,就像 @AndroidEntryPoint/@EntryPoint 的注释一样,显然它不起作用。
创建一个具有一种可变数据类型的通用数据类
data class <T> GenericResponse(
val success: Boolean,
val message: String,
val data: T
)
Run Code Online (Sandbox Code Playgroud)
使用方法:GenericResponse<SomeOtherDataClass>
如何在科特林中做到这一点?
我写了一个非常基本的程序,将元音与输入字符串分开。该程序不仅分离元音,而且还返回奇怪的符号/字母!
我真的找不到原因。救命!
输出>>
aoeeo x?E?óì
每次运行它都会生成不同的字母(?)!
代码>>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
string input = "stackoverflow";
vector<char> vowels = {'a','e','i','o','u'};
vector<char> result;
for(int i = 0 ; i < input.size() ; i++){
for(int j = 0; j < vowels.size(); j++){
if(input[i] == vowels[j]){
result.push_back(input[i]);
}
}
if (input[i] == 'u' || input[i] == 'e') {
result.push_back(input[i]);
}
}
for(int i = 0 ; i < input.size() ; i++){
cout << result[i];
}
return 0 …Run Code Online (Sandbox Code Playgroud)