From this answer, I know that I can get the name of the class type using
NSStringFromClass(YourClassName)
But I actually also need the name of the current object instance. So if I have code like this:
let loginForm = LoginFormViewController();
I want to actually get the loginForm in string, within the implementation of LoginFormViewController.
How can I do that?
Related
I'm trying to get a value of a field from inside a nested class in Kotlin, however, I'm having some difficulties with the logic. Somewhere I saw that it is possible through an interface, but I don't know where to start.
A simple sample:
class ExternalClass {
class InternalClass {
val internalValue = 2
}
val externalValue = InternalClass().internalValue
}
fun main(args: Array<String>) {
print(ExternalClass().externalValue)
}
The easiest way (and most obvious) to achieve this would be to instantiate the InternalClass class inside the External, however, in some cases, it would be necessary to pass several parameters in the Internal's constructor, which would not be the case.
So how would it be possible to do this through an interface?
Any idea or insight will be welcome!
Thank you!
Taking a step back, it is nonsensical for the outer class to want to access a stateful property of some arbitrary instance of the nested class. There's no reason for an arbitrary instance's state to be useful. The only reason the outer class would need to access a property of the other class is if it is doing something with a specific instance of the inner class, in which case it would already have an instance of it on which to access its property.
In your example code, the nested class is weird because it defines a property that can only ever hold a value of 2. So every instance of the class is reserving memory for a property to hold a duplicate of that same value, even though it should really be a constant that is shared by all instances.
If the value you want to access is a constant (the same value for all instances), then it would make sense to want to access it regardless of instance because it doesn't have anything to do with a specific instance. To make it constant, it should be defined in a companion object like this. Then it can be accessed through the name of the nested class without creating an instance of it.
class ExternalClass {
class InternalClass {
companion object {
val internalValue = 2
}
}
val externalValue = InternalClass.internalValue
}
An interface would have nothing to do with this kind of thing.
javaClass.simpleName returns StanadloneCoroutinejava instead of class name name when it is called from within coroutine scope.
I want to get the upper level class name. Any ideas how can I do so?
I think if you do this#NameOfClass::class.simpleName you'll get the correct name.
Suppose I have a fully qualified reference string:
val location = "org.path.to.some.field"
val foo = getFieldFromString(location)
I want to get the same value as if I did:
import org.path.to.some.field
How do I do this? Does it make sense to use reflection?
I think this might be the way to do it:
Class.forName("org.path.to.some").getField("field").get(null)
One problem is that you do need to know the type of the field in order for this to return something other than AnyRef. The get(null) is because we don't need an instance to get the static field.
How do I get a class object of a certain class in GObject / Gtk? For example, if my class is GtkSpinButton, I want to get the instance of GtkSpinButtonClass that represents the class. It is the parameter "class" in
gtk_spin_button_class_init (GtkSpinButtonClass *class)
and it is the object where virtual functions are stored. When I have an instance of GtkSpinButton, I can call
GtkSpinButtonClass *class = GTK_SPIN_BUTTON_GET_CLASS (instance)
however I don't have an instance around. GTK_TYPE_SPIN_BUTTON gives me the type id, a number, and not the class object. Any idea how to get the actual instance?
You want to use g_type_class_ref
GtkSpinButtonClass *klass = g_type_class_ref(GTK_TYPE_SPIN_BUTTON);
and when you're done with it
g_type_class_unref(klass);
I am wondering what type do I put in place of XXX
def registerClass(cl:XXX) = kryo.register(classOf[cl])
EDIT: For why I want to do this.
I have to register many classes using the above code. I wanted to remove the duplication of calling kyro.register several times, hoping to write code like below:
Seq(com.mypackage.class1,com.mypackage.class2,com.mypackage.class3).foreach(registerClass)
Another question, can I pass String instead? and convert it somehow to a class in registerClass?
Seq("com.mypackage.class1","com.mypackage.class2").foreach(registerClass)
EDIT 2:
When I write com.mypackage.class1, it means any class defined in my source. So if I create a class
package com.mypackage.model
class Dummy(val ids:Seq[Int],val name:String)
I would provide com.mypackage.model.Dummy as input
So,
kryo.register(classOf[com.mypackage.model.Dummy])
Kryo is a Java Serialization library. The signature of the register class is
register(Class type)
You could do it like this:
def registerClass(cl:Class[_]) = kryo.register(cl)
And then call it like this:
registerClass(classOf[Int])
The type parameter to classOf needs to be known at compile time. Without knowing more about what you're trying to do, is there any reason you can't use:
def registerClass(cl:XXX) = kryo.register(cl.getClass)