When trying to instantiate my RealmDouble model I get the following error:
*** Terminating app due to uncaught exception 'RLMException', reason: 'Invalid value '0' to initialize object of type 'RealmDouble': missing
key 'double_value'
class RealmDouble: Object {
dynamic var double_value: Double = 0.00
}
RealmDouble(value: 0.0)
I have tried deleting the app from the simulator, as well as deleting the Realm file. Does anyone know how to fix this?
You'd see this exception if you're passing a Double in a place where Realm expects to see either a dictionary from property names to values or an array of values, such as Realm.create(_:value:update). For instance, you'll see an error like this from the following code:
realm.create(RealmDouble.self, value: 0.0)
You should instead do:
realm.create(RealmDouble.self, value: [0.0])
From the future if it helps anyone:
Same error was thrown when I accidentally passed the wrong Type on the first parameter of the method realm.create(type: T.Type, value:_, update:_)
So make sure type is the correct and set to accept the values you are passing.
Related
I have some code and when I run it produces an error, saying:
NoSuchMethod: the method 'XYZ' was called on null
What does that mean and how do I fix it?
Why do I get this error?
Example
As a real world comparison, what just happened is this conversation:
Hey, how much gas is left in the tank of the car?
What are you talking about, we don't have a car.
That is exactly what is happening in your program. You wanted to call a function like _car.getGasLevel(); but there is no car, the variable _car is null.
Obviously, in your program it might not be a car. It could be a list or a string or anything else really.
Technical explanation
You are trying to use a variable that is null. Either you have explicitly set it to null, or you just never set it at all, the default value is null.
Like any variable, it can be passed into other functions. The place where you get the error might not be the source. You will have to follow the leads from the actual null value to where it originally came from, to find what the problem is and what the solution might be.
null can have different meanings: variables not set to another value will be null, but sometimes null values are used by programmers intentionally to signal that there is no value. Databases have nullable fields, JSON has missing values. Missing information may indeed be the information itself. The variable bool userWantsPizzaForDinner; for example might be used for true when the user said yes, false when the user declined and it might still be null when the user has not yet picked something. That's not a mistake, it's intentionally used and needs to be handled accordingly.
How do I fix it?
Find it
Use the stack trace that came with the error message to find out exactly which line the error was on. Then set a breakpoint on that line. When the program hits the breakpoint, inspect all the values of the variables. One of them is null, find out which one.
Fix it
Once you know which variable it is, find out how it ended up being null. Where did it come from? Was the value never set in the first place? Was the value another variable? How did that variable got it's value. It's like a line of breadcrumbs you can follow until you arrive at a point where you find that some variable was never set, or maybe you arrive at a point where you find that a variable was intentionally set to null. If it was unintentional, just fix it. Set it to the value you want it to have. If it was intentional, then you need to handle it further down in the program. Maybe you need another if to do something special for this case. If in doubt, you can ask the person that intentionally set it to null what they wanted to achieve.
simply the variable/function you are trying to access from the class does not exist
someClass.xyz();
above will give the error
NoSuchMethod: the method 'xyz' was called on null
because the class someClass does not exist
The following will work fine
// SomeClass created
// SomeClass has a function xyz
class SomeClass {
SomeClass();
void xyz() {
print('xyz');
}
}
void main() {
// create an instance of the class
final someClass = SomeClass();
// access the xyz function
someClass.xyz();
}
I came across a heterogeneous dictionary definition like this on a tutorial online:
var mixedMap4 = [AnyHashable(0): "Zero" as Any,
AnyHashable(1): 1.0 as Any,
AnyHashable("pi"): 3.14 as Any]
I was wondering why the author chose to write
AnyHashable(0) instead of 0 as AnyHashable. When I tried this on Swift playground, it also worked. However when I turned "Zero" as Any into Any(0) it gives the following
error: error: The Dictionary.xcplaygroundpage:41:34: error: protocol
type 'Any' cannot be instantiated var mixedMap4 = [AnyHashable(0):
Any("Zero") ,
Thank you for the answer
The clue is in the error message.
AnyHashable is a struct that type-erases the underlying hashable type, and so can be directly instantiated as an object
Any is a protocol and therefore can't be directly instantiated, although all other types can be complied with it, thus a String such as "Zero" can be cast as Any but Any(String) is meaningless.
To me it all just feels like a bucket load of trouble waiting to happen!
Migrating to Swift 3 from 2.3 and am running into this issue. The error is traceable to a view controller.
I do not see any extensions/protocols which would require a 'Key' constrained to 'String'.
I've tried to comment out code that might be causing the error, and have had strange results - ie removing an empty viewDidLoad() made the error appear in another class.
I'll update the thread if I make progress.
Key is a type inside a structure maybe a struct/class like Dictionary.
Use AnyHashable as Key to replace String if in Dictionary.
I have the following class defined on a Playground with Swift 3:
class MyError: Error {
}
Then, I create an instance of such class and check if it is a NSError
let firstError = MyError()
firstError is NSError // Output: false
The output is as expected, and I also get a warning which indicates Cast from 'MyError' to unrelated type 'NSError' always fails. This makes total sense for me, but if I change the code a little bit and declare the variable as an Error, I get a strange result:
var secondError: Error
secondError = MyError()
secondError is NSError // Output: true
And in this case I get a warning in the last line that says 'is' test is always true. I don't get why an Error would always be an NSError, when the model is defined the other way round (NSError: Error). Any idea what is going on here?
This is intentional behavior to allow Swift Error types to interop with Objective-C.
The compiler will only do the coercion when bridging Swift errors to Objective-C, or in your case where all you have is the Error existential that could contain anything... remember it could just as well have come from a throws function written in Objective-C. This also gives you an out to get the coercion if you need to pass an NSError directly to some Objective-C method as a parameter (for whatever reason).
If you change the 'Underlying type name' property of an enumeration in something that does not exist (typo). The application throws an 'CF0230: Invalid enumeration type 'System.Int32System.Int16'.' Error. Which is fine.
However, there is no way to change the type afterwards. I had to edit the CFP manually to correct the error.