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
Related
I need to update all the objects in the database, when I try it, it says:
Thread 1: "Attempting to modify a frozen object - call thaw on the Object instance first."
I have the following:
#ObservedRealmObject var meal: MealTracking
#ObservedResults(MealTracking.self) var mealTracking
mealTracking is the one that contains everything and meal is the single object in the current view.
So once I update the name on that single object (meal), I want to update the name on all other objects. So I'm doing:
for (index, meal) in self.mealTracking.enumerated() {
$mealTracking.wrappedValue[index].mealName = ""
}
and I get the error of not being able to update the objects.
I also tried it like $mealTracking[index].mealName.wrappedValue = "" and gives another error: Referencing subscript 'subscript(_:)' requires wrapped value of type 'Results<MealTracking>'
For a the single object on the current view I can update it with:
$meal.mealName.wrappedValue = "some stuff", the problem is when attempting to update All objects
How can I update all the objects?
#ObservedRealmObject is a frozen object. If you want to modify the properties of an #ObservedRealmObject directly in a write transaction, you must .thaw() it first.
In your case something like :
mealName.thaw()?.name = ""
More information :
https://www.mongodb.com/docs/realm/sdk/swift/swiftui/
https://www.mongodb.com/community/forums/t/freeze-frozen-objects-and-thawing/15706/5
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 a Uitableview that loads data from a parsed xml feed. when the feed is first parsed all the text data is stored in an entity NewsItems in core data. after the table is loaded the images asociated with each object are fetched asynchronously and stored in a separate entity NewsImages, after the feeds/images are stored locally all data is fetched locally next time the app starts. NewsItems and NewsImages have a one to one relationship with each other.
I have a refresh button which when clicked deletes all entries in NewsItems, this will also delete all objects in NewsImages associated with objects in NewsItems aswell, since the relationship delete rules are cascade. After deleteion, the feed is parsed again and data is stored locally again.
My problem is when I do this multiple number of times quickly. I get this error while saving Images locally.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-one relationship: property = "ItemImage"; desired type = NewsImages; given type = NewsImages; value = <NewsImages: 0x68c49f0> (entity: NewsImages; id: 0x6804730 <x-coredata:///NewsImages/t5444BEE7-6193-4C25-8AAB-F64113BEAB7546> ; data: {
Image = <ffd8ffe0 00104a46 49460001 01000001 00010000 ffe10058 45786966 00004d4d 002a0000 00080002 01120003 00000001 0001>;
ImageItem = nil;
}).'
This is the function responsible for inserting images
-(void)setImage:(UIImage*)moImage ForObject:(NSManagedObjectID*)moID{
NewsItems *newsItem = (NewsItems*)[self.managedObjectContext objectWithID:moID];
NewsImages *newsImage = (NewsImages*)[NSEntityDescription insertNewObjectForEntityForName:#"NewsImages" inManagedObjectContext:self.managedObjectContext];
newsImage.Image = UIImageJPEGRepresentation(moImage,1.0);
newsItem.ItemImage = newsImage;
[self commitSave];
}
I think the most likely problem is that you are passing the objectID for a NewsImages object instead of a for NewsItems object in setImage:ForObject: and in this line:
NewsItems *newsItem = (NewsItems*)[self.managedObjectContext objectWithID:moID];
...you are actually getting a NewsImages object returned cast as a NewsItems.
Cast can be problematical in Objective-C because an object will not complain at return if you send it a message it does not understand. The compiler won't catch the error because of the cast.
I would remove the cast, set the return item to id like so:
id newsItem = (NewsItems*)[self.managedObjectContext objectWithID:moID];
... then log its class:
NSLog(#"class=%#",[newItem class]);
... or check it in the debugger. Since you seem to be using custom NSManagedObject subclasses this should confirm the source of the error.
I had the same problem.
I fixed it by providing a class in the entity editor for the derived class.
The following setup was producing the error you've described:
Entity name : Contact <-------- User
ObjC Class : Contact Default to NSManagedObject.
The following setup fixed it:
Entity name : Contact <-------- User
ObjC Class : Contact Contact
I did some little experimentation on this.
I created an boolean attribute, named isFault. You know, that's a method of NSManagedObject and therefore actually not allowed for an attribute name because of KVC.
Simply, I used the default Core Data template for this test, but created the data model programmatically so I can show you what I do.
So here we go:
NSAttributeDescription *badAttr = [[NSAttributeDescription alloc] init];
[badAttr setName:#"isFault"];
[badAttr setAttributeType:NSBooleanAttributeType];
[badAttr setOptional:YES];
// don't want to occupy you with the whole, non-important rest ...
Next, I've modified the -insertNewObject method of the controller, added these lines:
// Assume: An managed object is created into the context, but not saved yet...
NSLog(#"isFault = %d", [newManagedObject isFault]); // 0 = NO
BOOL isFault = [[newManagedObject valueForKey:#"isFault"] boolValue];
NSLog(#"isFault = %d", isFault); // 0 = NO
[newManagedObject setValue:[NSNumber numberWithBool:YES] forKey:#"isFault"];
isFault = [[newManagedObject valueForKey:#"isFault"] boolValue];
NSLog(#"isFault = %d", isFault); // 0 = NO
Like you can see, I'm not able to set the isFault attribute to YES. It remains NO. Now, changing the attribute name to isFaultXYZ, will allow that.
So actually, what I wanted to ask is... since this stuff seems to depend on KVC, does the rule only apply to methods that return something and have no parameter? And does it matter what data type is returned? For example, -changedValues has no parameter and returns an NSDictionary. But since there is no attribute type like that, would this cause a collision anyways?
So, here is the evidence. When naming the attribute changedValues, THIS happens after attempting to access it via KVC:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary boolValue]: unrecognized selector sent to instance 0x1205680'
2010-06-10 14:52:40.232 CoreData[33996:207] Stack: (
8035,
4437,
8825,
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?