CFPreferences and init: a possible reason?
CFPreferences and init: a possible reason?
- Subject: CFPreferences and init: a possible reason?
- From: Adam Penny <email@hidden>
- Date: Tue, 28 Oct 2008 02:08:22 +0100
Forgive me if I'm sounding like a broken record, but after a day
experimenting with different approaches based on ideas that came up
yesterday, I'm pretty confident that everything is sound as far as my
controller class is concerned. I've been mulling this problem over and
wanted to put my hypothesis forward for consideration by anyone with
more experience, which is probably everyone else on this list.
I am able to write The contents of my one NSTextField to the plist
with CFPreferencesSetAppValue, it's writing the two arrays that is the
sticking point. When I try to do CFPreferencesSetAppValue for the
array(s) I get an EXC_BAD_ACCESS raised by the debugger.
I got to wondering whether my problem might have something to do with
the content of the array rather than the array itself. My
NSArrayController is bound to a class Servers that initialises two
NSStrings: name and mac which are inserted into the new index in the
NSMutableArray and these are bound to the two columns of my NSTableView.
I was just wondering whether my controller class necessarily knows
anything about the two NSStrings in each row of the array, since it's
the array controller that deals with formatting the rows for display
in the NSTableView? Could not having the contents of the array
declared in the preference controller itself be what's upsetting
CFPreferences when I try to write the array?
Just as a reminder here's my WopolPrefs.m as it stands now along with
the server class. I'm not bothering to put the printers class as it's
much the same principle as the server class anyway.
I've put them at the end of the email as you've probably seen
variations on it far too many times by now. The code isn't very tidy
at the moment as I've been experimenting a lot, but it all works save
the aforementioned issues.
Thanks again,
Adam
*******Beginning of code**********
#import "WopolPref.h"
@implementation WopolPref
- (id)initWithBundle:(NSBundle *)bundle
{
if (![super initWithBundle:bundle]) return nil;
appID = CFSTR("uk.co.pennynet.Wopol");
CFPropertyListRef
serversFromPlist=CFPreferencesCopyAppValue( CFSTR("servers"), appID);
if (serversFromPlist &&
CFGetTypeID(serversFromPlist)==CFArrayGetTypeID())
{
servers=[[NSMutableArray alloc]initWithArray: (NSArray
*)serversFromPlist];
CFRelease(serversFromPlist);
}
else servers= [[NSMutableArray alloc] init];
CFPropertyListRef printersFromPlist=
CFPreferencesCopyAppValue( CFSTR("printers"), appID);
if (printersFromPlist &&
CFGetTypeID(printersFromPlist)==CFArrayGetTypeID())
{
printers=[[NSMutableArray alloc]initWithArray: (NSArray
*)printersFromPlist];
CFRelease(printersFromPlist);
}
else printers= [[NSMutableArray alloc] init];
broadcastIP= @"255.255.255.255";
return self;
}
- (void) mainViewDidLoad
{
CFPropertyListRef value;
/*Get value for the broadcastIP textfield*/
value=CFPreferencesCopyAppValue( CFSTR("broadcastIP"), appID);
if (value && CFGetTypeID(value) == CFStringGetTypeID()) [broadcastIP
setStringValue:(NSString *)value];
else [broadcastIP setStringValue:(NSString *)@"255.255.255.255"];
/*release the value*/
if (value) CFRelease(value);
}
- (void) setPrinters:(NSMutableArray *)newPrinters
{
//Not relevant
}
- (void) setServers:(NSMutableArray *)newServers;
{
//Method for adding a new server, with mac address to the servers dict
if (newServers==servers) return;
[newServers retain];
[servers release];
servers=newServers;
/*if([servers count]!=0) CFPreferencesSetAppValue(CFSTR("servers"),
(NSArray *)servers, appID);
else CFPreferencesSetAppValue(CFSTR("servers"), NULL, appID);
[self updatePrefs];*/
}
- (void) updatePrefs;
{
CFPreferencesAppSynchronize(appID);
}
- (void)controlTextDidEndEditing: (NSNotification *)aNotification;
{
NSLog(@"TextField changed");
/* CFPreferencesSetAppValue(CFSTR("broadcastIP"), (NSString *)
[broadcastIP stringValue], appID);
[self updatePrefs];*/ }
- (void)dealloc
{
[printers release];
[servers release];
[super dealloc];
}
- (IBAction) toPlist:(id)sender
{
NSLog(@"Trying to write to Plist");
NSArray *serversToWrite=[[NSArray alloc]initWithArray:servers];
if([serversToWrite count]!=0)
CFPreferencesSetAppValue(CFSTR("servers"), (NSArray *)serversToWrite,
appID);
else CFPreferencesSetAppValue(CFSTR("servers"), NULL, appID);
[self updatePrefs];
[serversToWrite release];
NSLog(@"PList saved");
}
@end
********end of code *************
The class that I mentioned that is bound to the servers array
controller is:
*******beginning of code************
//The .h:
#import <Foundation/Foundation.h>
@interface Server : NSObject {
NSString *name;
NSString *mac;
}
@property (readwrite, copy) NSString *name;
@property (readwrite, copy) NSString *mac;
@end
//The .m
#import "server.h"
@implementation Server
- (id)init
{
[super init];
name = @"New Server Name";
mac = @"000000000000";
return self;
}
- (void)dealloc
{
[name release];
[mac release];
[super dealloc];
}
@synthesize name;
@synthesize mac;
@end
*******end of code***************
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden