我尝试使用Parse.com在后台保存对象,但我无法覆盖
override fun done(e: ParseException?) {
//code
}
我收到错误:Modifier 'override' is not applicable to 'local function
在java中我会使用:
myObject.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
myObjectSavedSuccessfully();
} else {
myObjectSaveDidNotSucceed();
}
}
});
这是我的全班
class StarterApplication : Application() {
override fun onCreate() {
super.onCreate()
Parse.initialize(Parse.Configuration.Builder(this)
.applicationId(appID)
.clientKey(null)
.server(serverUrl)
.build()
)
var exampleObject: ParseObject = ParseObject("ExampleObject")
exampleObject.put("myString", "fwfwe")
exampleObject.saveInBackground( {
override fun done(e: ParseException?) { //here is an error //`Modifier 'override' is not applicable to 'local function`
}
})
}
}
答案 0 :(得分:3)
就这样做:
exampleObject.saveInBackground(object : SaveCallback {
override fun done(e: ParseException?) {
// Add your code here
}
})
在Java中,您声明了一个扩展SaveCallback
的匿名类。在Kotlin中,您使用Object Expressions执行此操作。
答案 1 :(得分:1)
您也可以尝试这样的最简单的解决方案
exampleObject.saveInBackground({
//you code here
})
https://kotlinlang.org/docs/reference/java-interop.html#sam-conversions