Created
June 3, 2015 03:12
-
-
Save desamtralized/bccc4c9ba22e7c6fef49 to your computer and use it in GitHub Desktop.
Accessing ParseObject properties in a easy way using Kotlin Data Class and Delegate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Define your sublclass of ParseObject | |
ParseClassName("Person") | |
public data class Person : ParseObject() { | |
var name: String by ParseDelegate<String>() | |
var age: Int by ParseDelegate<Int>() | |
var avatar: ParseFile by ParseDelegate<ParseFile>() | |
} | |
//Define ParseDelegate | |
public class ParseDelegate<T> { | |
fun get(parseObj: ParseObject, propertyMetadata: PropertyMetadata): T { | |
return parseObj.get(propertyMetadata.name) as T | |
} | |
fun set(parseObj: ParseObject, propertyMetadata: PropertyMetadata, a: Any) { | |
parseObj.put(propertyMetadata.name, a) | |
} | |
} | |
//Use it | |
//fecth some Person | |
person.age //will return an Int | |
person.avatar //will return a ParseFile | |
person.age = 30 //will set the new value in the ParseObject using put() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment