Error calling Instantiate in Unity on WP? - windows-phone

GameObject obj = (GameObject) Instantiate (Resources.Load ("Actions/" + actions, typeof (GameObject)));
No problem with other platforms. But, when building on Windows Phone:
Object reference not set to an instance of an object.

Related

How to use spyk

I need to verify that a method has been called on an object. So I make a spy of this object:
obj = spyk(obj)
And then I verify that a method has been called:
verify(exactly = 1) { obj.f(3) }
The test fails with the following error message:
java.lang.AssertionError: Verification failed: call 1 of 1: obj(#2).f(eq(3))) was not called.
However I can clearly see the method f being called:
I can break in that method in debug mode
I print out hello world from f() in that method and see it being printed.
How do I use spies correctly in mockk?
P.S.
I tried doing val obj = spyk(obj()) but I get lateinit has not been initialized error because I need to set a parameter in obj as so:
obj.setDependency(friend)
But in the case where I first do val obj = spyk(obj()) and then call obj.setDependency(friend) like I explained above I end up with a lateinit has not been initialized error
Can someone please help me resolve this issue?
In your case, I don't understand this part obj = spyk(obj). What are you doing here? I think this part don't even compile.
You receive lateinit has not been initialized error because spyk(obj()) calls real constructor. If your object has dependencies, you have to create them too or pass mockk instead of them.
According to the documentation:
Note: the spy object is a copy of a passed object.
You have to create this object like a normal object, so all dependencies have to be filled.
I am using spyk in this way, let me show you a quick example.
fun `should call method testMethod`() {
val spy = spyk<TestClass>()
spy.testMethod(1)
verify (exactly = 1) { spy.testMethod(1) }
}

How is Companion different from INSTANCE

How are an object INSTANCE and a Companion object different and which situations should they be used in.
ObjectName.INSTANCE.iAmStaticMethod();
ClassName.Companion.iAmStaticMethod();
ClassName.Companion can be used also for accessing a non static method
iAmStaicMethod() is a static function and iAmNonStaticMethod() is a non-static function.
So, to call the above methods in Java, write the below code:
ClassName.iAmStaticMethod(); // works fine
ClassName.iAmNonStaticMethod(); // error: not a static method
ClassName.Companion.iAmStaticMethod(); // instance method remains
ClassName.Companion.iAmNonStaticMethod(); // the only way it works

Swift class properties not initialized when constructed by Objective C code

I'm attempting to create a class in Swift 3 to implement a Cordova plugin. I have this building and running, but the application crashes whenever any properties of the class are accessed. I've tried two ways of initializing the class:
#objc(DSFMediaCentre)
class DSFMediaCentre : CDVPlugin
{
var players = [UUID:DSFPlayerHandler] ();
...
}
and
#objc(DSFMediaCentre)
class DSFMediaCentre : CDVPlugin
{
var players :[UUID:DSFPlayerHandler];
override init () {
players = [:];
}
...
}
However, when my players property is used, the result is a EXC_BAD_ACCESS exception, with an address that looks like a null pointer dereference.
The object is being created by Objective C code, which is a language I have no familiarity with at all, but I think this is the line that creates it:
obj = [[NSClassFromString(className)alloc] initWithWebViewEngine:_webViewEngine];
The CDVPlugin class contains a comment stating that initWithWebViewEngine should not be overridden (and indeed I do not seem to be able to override this method, because while it is declared in the CDVPlugin.m file, it isn't mentioned in CDVPlugin.h, so the Swift compiler doesn't seem to know about it), but rather initialization code should be placed in a method called pluginInitialize instead. However, if I do that I get a compiler error ("Class DSFMediaCentre has no initializers").
Furthermore, if I put my init() method back in and set it to call pluginInitialize(), like this:
override init () {
super.init(); // necessary otherwise next line is an error
pluginInitialize();
}
override func pluginInitialize() {
players = [:];
}
the error then changes to "Property 'self.players' not initialized at super.init call".
How do I make this class initialize correctly?
You have a mismatch between the strict initialization system required by the language and the procedure used by the framework you're working with.
Swift demands that a) properties be initialized as part of object construction, and b) that construction be chained to the type's supertype. But the CDVPlugin type is doing the construction on your behalf; you don't have the ability to customize it. (This makes more sense in ObjC, because it doesn't have the same compile-time restrictions as Swift.)
The situation is similar to unpacking an object from a nib file. In that case too, because it's the nib loading system that's constructing your object, you don't have the ability to customize the initializer. Your type will always be constructed by init(coder:). In a certain sense, your initialization point moves further down, to awakeFromNib(), and among other things, that forces outlets to other objects in the archive to be declared as optional, usually implicitly unwrapped.
The same solution should avail you here. You should consider pluginInitialize() to be your initialization point. The language then requires that properties be optional, since they are not filled at its initialization point. Therefore, make the property an IUO:
#objc(DSFMediaCentre)
class DSFMediaCentre : CDVPlugin
{
var players :[UUID:DSFPlayerHandler]!
override func pluginInitialize() {
players = [:];
}
}
and all should be well.
The other solution is to use lazy keyword
lazy var players :[UUID:DSFPlayerHandler] = [:]
So, you don't need to initialize players in initializer but still make sure players always non-nulable

Error accessing function of class while converting Obj C project to Swift

I added my swift class to the target while removing my header file of the same objective C class from the target but this error shows when I try and build my project. I can't attach an image right now but the error states: "Use of instance member 'url' on type 'ServerURLFactory'; did you mean to use a value of type 'ServerURLFactory' instead?"
let accessURL: NSURL = NSURL(string: "\(ServerURLFactory.url())/CygnetInstanceXMLServlet?cygnetId=\(idNumber)")!
print(accessURL)
Has anyone ran into a similar problem and how to fix this confusing bug? Its as if the program is still trying to call the Obj C function instead of explicitly calling the one in the Swift file.
You're calling .url() on ServerURLFactory itself as a type:
ServerURLFactory.url()
I guess you should instantiate the class first. Probably something like this, but it depends on how the class is implemented:
let factory = ServerURLFactory()
Then:
factory.url()

Super constructor cannot be passed a self reference unless parameter is declared by-name

Well, I have something like this:
trait A
class Serving(a: => A)
object App extends Serving(App.Main) {
object Main extends A
}
And I get the error super constructor cannot be passed a self reference unless parameter is declared by-name. I can work around by doing
object App extends Serving(Serv.Main)
object Serv {
object Main extends A
}
but I don't want to. It adds 2 extra .classes and it strikes me as unelegant.
And using object App extends Serving(this.Main) also creates an error. The structure of A and Serving cannot really be changed, but is there any way to work around this error?
Your code compiles just fine in Scala 2.8.1, even if the parameter is not declared by-name.