• 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: Wed, 12 May 2004 08:51:30 -0400

On May 11, 2004, at 1:01 PM, Pierre Vaudrey wrote:

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

> I still need help for the conversion of the code of the
> removeCommentAction method : the previous code using splice did not
> work anymore.

You can use [], push(), shift(), splice(), and other Perl array
functions and operators with Cocoa arrays. Whenever a function or
method that returns an NSArray (or descendant) object is called in list
context, the object is automatically tie()d to a Perl array interface.

Above, you're calling alloc()->init() in scalar context, so what you
get back is an object reference - if you wanted to use it as a Perl
array instead of using Cocoa methods with it, you could also create the
array and store a reference to it with code like this:

my @records =
NSMutableArray->alloc()->initWithContentsOfFile($self-
>{'recordsFile'});
unless (defined @records) {
@records = NSMutableArray->alloc()->init();
}
$self->{'comments'} = \@records;

You can play similar tricks with hashes and Cocoa NSDictionary objects.

> There is probably a shorter solution in Perl compared to the following
> ObjC code :

The example code you give using splice() is obviously shorter - but
that's because it doesn't do as much. It assumes a single row is
selected, whereas using -selectedRowEnumerator handles multiple,
possibly non-contiguous, selected rows. It also verifies the operation
with an alert panel and a beep.

There's still some room to make the code shorter though. So let's play
a round of golf. ;-)

> - (IBAction)deleteRecord:(id)sender
> {
> int status;
> NSEnumerator *enumerator;
> NSNumber *index;
> NSMutableArray *tempArray;
> id tempObject;
>
> if ( [tableView numberOfSelectedRows] == 0 )
> return;
>
> NSBeep();
> status = NSRunAlertPanel(@"Warning!", @"Are you sure that you want
> to delete the selected record(s)?", @"OK", @"Cancel", nil);
>
> if ( status == NSAlertDefaultReturn ) {
> enumerator = [tableView selectedRowEnumerator];
> tempArray = [NSMutableArray array];
>
> while ( (index = [enumerator nextObject]) ) {
> tempObject = [records objectAtIndex:[index intValue]];
> [tempArray addObject:tempObject];
> }
>
> [records removeObjectsInArray:tempArray];
> [tableView reloadData];
> [self saveData];
> }
> }

sub removeCommentAction {
my ($self, $sender) = @_;

return if ($self->{'Table'}->numberOfSelectedRows() == 0);

NSBeep();

if ( NSAlertDefaultReturn == NSRunAlertPanel("Warning!", "Are you
sure...?", "OK", "Cancel", undef); ) {
foreach my $ix (reverse
$self->{'Table'}->selectedRowEnumerator()->allObjects()) {
$self->{'records'}->removeObjectAtIndex($ix->intValue());
}

$self->{'Table'}->reloadData();
$self->saveData();
}
}

The above assumes that $self->{'records'} is an object reference. If
it's a reference to a tie()d array as described above, you could also
use Perl's splice() or delete() functions:

splice @{$self->{'comments'}}, $ix->intValue(), 1;
delete $self->{'comments'}->[$ix->intValue()];

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: 
 >Re: CamelBones question (From: Pierre Vaudrey <email@hidden>)

  • Prev by Date: Re: NSBezierPath, flipped Glyphs, and NSOutlineView
  • Next by Date: How to tell if first responder
  • Previous by thread: Re: CamelBones question
  • Next by thread: Re: CamelBones question
  • Index(es):
    • Date
    • Thread