• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: CamelBones question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CamelBones question


  • Subject: Re: CamelBones question
  • From: Sherm Pendley <email@hidden>
  • Date: Mon, 10 May 2004 18:30:44 -0400

On May 10, 2004, at 4:11 PM, Pierre Vaudrey wrote:

As an exercise I'm translating the AddressBook application
http://www.macdevcenter.com/pub/a/mac/2001/08/24/cocoa.html from ObjC
to Perl using CamelBones.
I need help to translate in Perl initWithContentsOfFile and writeToFile
in this part of code ( the rest of code is OK ) :
- (void)awakeFromNib
{
// We'll implement the backup file scheme that was discussed in the
end of the column.
recordsFile = [NSString
stringWithString:@"~/Library/Preferences/AddressBookData.plist"];
recordsFile = [[recordsFile stringByExpandingTildeInPath] retain];
records = [[NSMutableArray alloc]
initWithContentsOfFile:recordsFile];
if ( nil == records ) {
records = [[NSMutableArray alloc] init];
}
}

- (void)saveData
{
[records writeToFile:recordsFile atomically:YES];
}

IB isn't aware of Perl, so controller classes can't be instantiated inside a NIB. Therefore, awakeFromNib isn't a good place to do this, as it will never be called.

The way it's commonly done in Perl is to have your main app instantiate the controller class directly, and the controller class loads the NIB and makes itself the NIB's owner in its constructor. With that in mind, here's a translation of a constructor with the above code in it:

sub new {
my $self = bless({}, shift());

# Disable automatic conversion of NSString objects to Perl scalars
$CamelBones::ReturnStringsAsObjects = 1;

my $str = NSString->stringWithString('~/Library/Preferences/ AddressBookData.plist');

# Re-enable automatic conversion of NSString objects to Perl scalars
$CamelBones::ReturnStringsAsObjects = 0;

$self->{'recordsFile'} = $str->stringByExpandingTildeInPath();

my $records = NSMutableArray->alloc()->initWithContentsOfFle($self->{'recordsFile'});
unless (defined $records) {
$records = NSMutableArray->alloc()->init();
}

$self->{'records'} = $records;

NSWindowController->alloc()->initWithWindowNibName_owner('MainWindow', $self);

return $self;
}

sub saveData {
my ($self) = @_;
$self->{'records'}->writeToFile_atomically($self->{'recordsFile'}, 1);
}

There's probably a more Perl-ish way to expand the tilde, but I couldn't recall one off the top of my head. Anyway, it can be useful to know how to bypass the normal scalar conversion to get a "real" NSString object when you need one.

sherm--
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: CamelBones question
      • From: Pierre Vaudrey <email@hidden>
References: 
 >CamelBones question (From: Pierre Vaudrey <email@hidden>)

  • Prev by Date: Localized menus
  • Next by Date: Re: glitch in my size code?
  • Previous by thread: CamelBones question
  • Next by thread: Re: CamelBones question
  • Index(es):
    • Date
    • Thread