I've created a singleton class that loads a plist. I keep getting this error when I try to set a value:
[<MyPlist 0x1917bc0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key test.'
I have one key in the plist file. The key is named "test" and has no value associated with it. I set the value like this:
[[PlistManager sharedManager].plist setValue:#"the title value" forKey:#"test"];
I look at the set plist dictionary and see this from within PlistManager:
po self.plistDictionary
{
test = "";
}
I get the error just as I'm leaving PlistManager in the debugger. PlistManager is of type NSObject. So no xibs. Any ideas on what I need to do?
Could it be that you are using a non-mutable dictionary instead of NSMutableDictionary?
Related
I'm playing around with cocoa bindings and table views, and I'm trying to create a simple 2 column table view where the keys are displayed in the left column and values in the right. When I try to add anything to the base dictionary, the build succeeds but I get this:
2020-08-10 20:58:36.063356-0400 DictTest[55388:1344906] -[__NSCFNumber length]: unrecognized selector sent to instance 0x8ff53cf7c2dc61e7
2020-08-10 20:58:36.063531-0400 DictTest[55388:1344906] Failed to set (contentViewController) user defined inspected property on (NSWindow): -[__NSCFNumber length]: unrecognized selector sent to instance 0x8ff53cf7c2dc61e7
As for my bindings (I've done them all through storyboard), I have each column bound to the dictionary controller, controllerKey = arrangedObjects, Model Key Path = key/value depending. For the dictionary controller, I have it bound to the dictionary in my View Controller class, which is as such:
#objc dynamic var dict = [Int: String]()
I've been trying to add items to it in the conventional way (dict[0] = "test0").
Thanks
I'm setting the Placeholder value of a EntryView() in my application to a value I'm retrieving from my database. But now I want to change my approach and instead set the value of the EntryView so that the user does not have to fill in the content.
This is my code to retrieve the value and then set the Placeholder:
let firstName = dict["firstName"] as? String
...
self.firstNameEntryView.setPlaceholder(firstName!)
This functionality works fine and it populates the correct value as the placeholder. But how do I make the String value of 'firstName' be set as the actual value in my firstNameEntryView?
I tried this approach:
self.firstNameEntryView.setValue(String?.self, forKey: firstName!)
But I get this error: " setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Thomas"
Thomas is the correct value from my db. Am I using the setValue method incorrectly?
I ended up figuring this out through trial and error.
The code to set the text is as follows:
self.firstNameEntryView.textField.text?.append(firstName!)
I stumbled upon a weird thing when trying to fetch an object from my Realm (iOS, Swift, Realm version 0.98.2)
print("speaker:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker).first!)
Correctly dumps my object in the console:
speaker:
FavoriteSpeaker {
name = Ashley Nelson-Hornstein;
}
But when I try to get the name property's value:
print("speaker name:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker).first!.name)
I get an empty string 🤔
speaker name:
The four lines are together in my model's init method
Update 1: I found an answer that suggests that you merely don't see the values when printed in the Console: Realm object is missing all properties except primaryKey but I also tried displaying the name property via an alert view and that is also empty.
Update 2: Just to make sure that everything happens sequentially and on the same thread I did this:
let favorite1 = FavoriteSpeaker()
favorite1.name = "Debbie Downer"
try! RealmProvider.appRealm.write {
RealmProvider.appRealm.deleteAll()
RealmProvider.appRealm.add(favorite1)
}
print("speaker:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker.self).first!)
print("speaker name:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker.self).first!.name)
But the result is the same - printing name prints an empty string
The name property is probably not declared as dynamic, which leads to it reading the nil value stored on the object itself rather than reading the data from the Realm.
I have this following code :
NSAssert(dst, #"sdiasiosadiodasio");
// Add to a list so that we don't lose the references
[layersToDsts setObject:dst forKey:layer];
NSLog(#"The key is %lu", layer);
for (Bit *layer in [layersToDsts allKeys])
{
BitHolder *dst = [layersToDsts objectForKey:layer];
NSLog(#"My added key is %lu", layer);
NSAssert(dst, #"ddddd");
}
layersToDsts is a mutable NSDictionary. I'm trying to use the address of an object as the key, mapping to another object.
The code worked on iOS 5. For some reason, the code gives
The key is 484196128
My added key is 484253328
Assertion failure in -[Animator animateImpl:] 'NSInternalInconsistencyException', reason: 'ddddd'
Obviously, the object I added to the dictionary can't be retrieved. The object isn't nil. Why the mapping key is another address?? The code worked perfectly on iOS 5....
CALayer does not implement the NSCopying protocol and so can not be used as a key, use the address of the CALayer and turn it into an NSNumber you ay alternatively be able to use the name property on CALayer.
If you want to use an object address as the key, you should create an object that contains that address. For example
[layersToDsts setObject:dst forKey:[NSValue valueWithPointer:layer]];
However I STRONGLY discourage you from doing it. You should instead come out with a proper key and not depend on any specific object address.
I have a class called mycallback:
[mycallback setValue:[code objectForKey:#"abc"] forKey:#"abc"];
It gets called for a lot of my controllers. THe thing is i pull data from a mysql database and I send it to mycallback. So I may not know what the key is. And in addition to that, I don't want to declare instance variables inside mycallback class because I want it separated.
Unfortunately, if the code as is runs above, I get this message:
exception 'NSUnknownKeyException', reason: '[<CallbackClass 0x7b6c5d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key fence.'
So I want to dynamically set the keys but not inside mycallback. So I create a category for NSDictionary and add valueForUndefinedKey method:
- (id)valueForUndefinedKey:(NSString *)key
{
NSLog(#"This key not being called %#", key);
return nil;
}
but 1) valueForUndefinedKey is not called and 2) I'm not sure what to put in there to make that error go away.
thanks for response.
You've defined valueForUndefinedKey: which is what deals with returning values. But in your code snippet it looks like you are trying to set a value for an undefined key. In which case, the method to use is:
setValue:forUndefinedKey:
An example, based on what it looks like you are trying to do:
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
NSLog(#"Attempting to set value: %# for key: %#", value, key);
}