• 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
Trying to delete the first char of a string?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Trying to delete the first char of a string?


  • Subject: Trying to delete the first char of a string?
  • From: Bob Ueland <email@hidden>
  • Date: Mon, 29 Oct 2007 08:25:34 -0700 (PDT)

I'm testing catching key events and deleting the first char of a mutable string (here called mstr). The code below works fine and produces output

[Session started at 2007-10-29 16:19:15 +0100.]
2007-10-29 16:19:15.700 Task1[1905] Alabama
2007-10-29 16:19:15.700 Task1[1905] labama
2007-10-29 16:19:15.704 Task1[1905] Accepting
2007-10-29 16:19:15.704 Task1[1905] Accepting
2007-10-29 16:19:15.704 Task1[1905] Accepting
2007-10-29 16:19:15.704 Task1[1905] Accepting
2007-10-29 16:19:15.704 Task1[1905] Becoming


Now if I press the d key on the keyboard I get

2007-10-29 16:19:18.252 Task1[1905] The input is d


Here is the header file

/* MyView.h */

#import <Cocoa/Cocoa.h>

@interface MyView : NSView
{
    NSMutableString *mstr;
}
@end


Here is the implementation file

* MyView.m */
#import "MyView.h"

@implementation MyView

- (id)initWithFrame:(NSRect)frameRect
{
    if ((self = [super initWithFrame:frameRect]) != nil) {
        // Add initialization code here
    }
    return self;
}
-(void) awakeFromNib
{
    mstr=[NSMutableString stringWithString:@"Alabama"];
    NSLog(@"%@",mstr);
    // Delete first char
    [mstr deleteCharactersInRange: NSMakeRange (0, 1)];
    NSLog(@"%@",mstr);
}

- (void)drawRect:(NSRect)rect
{
}

- (BOOL)acceptsFirstResponder
{
    NSLog(@"Accepting");
    return YES;
}

- (BOOL)resignFirstResponder
{
    NSLog(@"Resigning");
    [self setNeedsDisplay: YES];
    return YES;
}

- (BOOL)becomeFirstResponder{
    NSLog(@"Becoming");
    [self setNeedsDisplay: YES];
    return YES;
}

- (void)keyDown:(NSEvent *)event
{
    [self interpretKeyEvents:[NSArray arrayWithObject:event]];
}

- (void)insertText:(NSString *)input
{
    NSLog(@"The input is %@", input);
    // if I move it here it does not work any more
}
@end


--------------------------------
So far so good. However if I now move the three lines
// Delete first char
[mstr deleteCharactersInRange: NSMakeRange (0, 1)];
NSLog(@"%@",mstr);




from awakeFromNib method to insertText method so it looks like this
- (void)insertText:(NSString *)input

{

    NSLog(@"The input is %@", input);

   // Delete first char
    [mstr deleteCharactersInRange: NSMakeRange (0, 1)];
    NSLog(@"%@",mstr);

}




the I get the following output

[Session started at 2007-10-29 16:17:45 +0100.]
2007-10-29 16:17:45.528 Task1[1880] Alabama
2007-10-29 16:17:45.531 Task1[1880] Accepting
2007-10-29 16:17:45.531 Task1[1880] Accepting
2007-10-29 16:17:45.531 Task1[1880] Accepting
2007-10-29 16:17:45.531 Task1[1880] Accepting
2007-10-29 16:17:45.532 Task1[1880] Becoming
2007-10-29 16:17:54.421 Task1[1880] The input is d

Task1 has exited with status -1.


and I'm thrown out from the program. So it's impossible to delete the first char of the string mstr.

Can someone explain why this happens?

Thanks Bob



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________

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

  • Follow-Ups:
    • Re: Trying to delete the first char of a string?
      • From: Fritz Anderson <email@hidden>
    • Re: Trying to delete the first char of a string?
      • From: Alastair Houghton <email@hidden>
    • Re: Trying to delete the first char of a string?
      • From: Jonathan del Strother <email@hidden>
  • Prev by Date: Re: How to make Redo work with a custom group
  • Next by Date: Re: Trying to delete the first char of a string?
  • Previous by thread: Re: How to make Redo work with a custom group
  • Next by thread: Re: Trying to delete the first char of a string?
  • Index(es):
    • Date
    • Thread