日常问题整理
日常问题整理
记录日常开发中遇到的问题和解决方案,不定期更新。
kotlin使用room时候引发下面错误
error: Entities and POJOs must have a usable public constructor.
You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
我的代码如下:
@Entity
data class CatalogData(
@PrimaryKey val id: Int,
@Ignore val name: String
)
将需要忽略的字段放在对象体中即可 解决方案
@Entity
data class CatalogData(
@PrimaryKey val id: Int
) {
@Ignore val name: String
}
记录一个正则表达式(?<=(A)).*?(?=(B))
匹配指定两个字符之间的内容,如下示例可以获取字符串中所有A,B之间的字符串
val resp = """
A我们BA都BA是好孩子B
""".trimIndent()
val matcher = Pattern.compile("(?<=(A)).*?(?=(B))").matcher(resp)
while (matcher.find()) {
val image = matcher.group(0)
println(image?.substring(0, image.lastIndexOf("\"")) ?: "")
}
println("匹配完成")
打印内容为 :
我们
都
是好孩子
匹配完成
Kotlin 使用Moshi解析Json时候@Json注解无效
各种百度,真坑啥都没有,Google了下,在Moshi的Issues中找到了答案
通常我们在Java中给一个对象使用注解如下
class A {
@Json("baidu_shabi")
public String time;
}
但是在kotlin这里需要使用@field:Json
的方式