<NSCoding> problems with bindings.
<NSCoding> problems with bindings.
- Subject: <NSCoding> problems with bindings.
- From: BlueHorseshoe <email@hidden>
- Date: Tue, 22 Mar 2005 00:04:04 -0500
Dear Fellow Cocoa Developers,
I have ran into some issues with persistence and bindings that I have not seen discussed here yet.
1.) Bindings do not seem to be decodable from normal, non-NIB, archives. As demonstrated in from the output of the code below, I bind the "title" of one button to another through an NSObjectController programmatically and then encode all three objects as instance variables of NXObjectControllerArchive. After decoding, I must rebind the first button to reestablish the relationship.
2.) Using -setContent on NSObjectController, instead of binding "contentObject," does not solve the problem for NSObjectController, as it must be reconnected to it's content after decoding.
3.) If NSObjectController is bound to it's "contentObject," after decoding the binding mechanism appears to fail although -setContent continues to work. It is as if, the NSObjectController instance has become corrupted during the coding process.
Yours,
Gordon G.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Output from the code below:
2005-03-20 02:09:27.252 CocoaTesting[9050] [[objectControllerArchive objectController] content] = <NSButton: 0x5268a0>
2005-03-20 02:09:27.252 CocoaTesting[9050] [[objectControllerArchive button1] title] = button1
2005-03-20 02:09:27.252 CocoaTesting[9050] [[objectControllerArchive button2] title] = button1
2005-03-20 02:09:27.254 CocoaTesting[9050] [[unarchivedObjectControllerArchive objectController] content] = (null)
2005-03-20 02:09:27.254 CocoaTesting[9050] [[unarchivedObjectControllerArchive button1] title] = button1
2005-03-20 02:09:27.254 CocoaTesting[9050] [[unarchivedObjectControllerArchive button2] title] = button1
2005-03-20 02:09:27.255 CocoaTesting[9050] [[unarchivedObjectControllerArchive button2] title] = button1
2005-03-20 02:09:27.256 CocoaTesting[9050] [[unarchivedObjectControllerArchive button2] title] =
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
@interface NXObjectControllerArchive : NSObject <NSCoding>
{
NSObjectController *objectController;
NSButton *button1, *button2;
}
- (NSObjectController *)objectController;
- (NSButton *)button1;
- (NSButton *)button2;
@end
@implementation NXObjectControllerArchive
- (
void)dealloc
{
[objectController release];
[button1 release];
[button2 release];
[
super dealloc];
}
- (
void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:objectController];
[coder encodeObject:button1];
[coder encodeObject:button2];
}
- (
id)initWithCoder:(NSCoder *)coder
{
self = [
super init])
{
[(objectController = [coder decodeObject]) retain];
[(button1 = [coder decodeObject]) retain];
[(button2 = [coder decodeObject]) retain];
}
return self;
}
- (
id)init
{
self = [
super init])
{
objectController = [NSObjectController new];
button1 = [[NSButton alloc] initWithFrame:NSMakeRect(
0,
0,
100,
100)];
button2 = [[NSButton alloc] initWithFrame:NSMakeRect(
0,
0,
100,
100)];
//[objectController setContent:button1];
[objectController bind:
@"contentObject" toObject:
self withKeyPath:
@"button1" options:
nil];
[button2 bind:
@"title" toObject:objectController withKeyPath:
@"selection.title" options:
nil];
[button1 setTitle:
@"button1"];
}
return self;
}
- (NSObjectController *)objectController {
return objectController;}
- (NSButton *)button1 {
return button1;}
- (NSButton *)button2 {
return button2;}
@end
int main () {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NXObjectControllerArchive *objectControllerArchive = [NXObjectControllerArchive new];
NSLog(
@"[[objectControllerArchive objectController] content] = %@", [[[objectControllerArchive objectController] content] description]);
NSLog(
@"[[objectControllerArchive button1] title] = %@", [[objectControllerArchive button1] title]);
NSLog(
@"[[objectControllerArchive button2] title] = %@", [[objectControllerArchive button2] title]);
NSData *archivedData = [NSArchiver archivedDataWithRootObject:objectControllerArchive];
NXObjectControllerArchive *unarchivedObjectControllerArchive = [NSUnarchiver unarchiveObjectWithData:archivedData];
NSLog(
@"[[unarchivedObjectControllerArchive objectController] content] = %@", [[[unarchivedObjectControllerArchive objectController] content] description]);
NSLog(
@"[[unarchivedObjectControllerArchive button1] title] = %@", [[unarchivedObjectControllerArchive button1] title]);
NSLog(
@"[[unarchivedObjectControllerArchive button2] title] = %@", [[unarchivedObjectControllerArchive button2] title]);
// NOTE: using -setContent works as expected here, but binding "contentObject" fails.
//[[unarchivedObjectControllerArchive objectController] setContent:[unarchivedObjectControllerArchive button1]];
[[unarchivedObjectControllerArchive objectController] bind:
@"contentObject" toObject:unarchivedObjectControllerArchive withKeyPath:
@"button1" options:
nil];
[[unarchivedObjectControllerArchive button1] setTitle:
@"button11111111111"];
NSLog(
@"[[unarchivedObjectControllerArchive button2] title] = %@", [[unarchivedObjectControllerArchive button2] title]);
// NOTE: "button2" must be rebound for it's to be set to @"button1111111111"
[[unarchivedObjectControllerArchive button2] bind:
@"title" toObject:[unarchivedObjectControllerArchive objectController] withKeyPath:
@"selection.title" options:
nil];
NSLog(
@"[[unarchivedObjectControllerArchive button2] title] = %@", [[unarchivedObjectControllerArchive button2] title]);
[pool release];
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden