3

I wrote a simple ios5 application (includes garbage collector) that has a single view and a UITextField I need to analyze input text in this UITextField here's my code. header file:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>
{
    IBOutlet UITextField *myTextField;
}

@property (nonatomic, retain) IBOutlet UITextField *myTextField;

-(IBAction)editingChanged:(UITextField *)sender;

editingChanged: tracked with send event editing changed so this method calls everytime user changes something in my UITextField

part of implementation file:

#pragma mark - textField

-(NSString *)stringWithoutAbc:(NSString *)sourceString
{

    NSString *resultString=[sourceString stringByReplacingOccurrencesOfString:@"abc:" withString:@""];
    if (![resultString isEqualToString:sourceString])
    {
        NSLog(@"    sourceString: %@", sourceString);
        NSLog(@"    resultString: %@", resultString);
    };
    return resultString;
}

-(IBAction)editingChanged:(UITextField *)sender
{
    NSLog(@"editing Changed. text: %@", sender.text);

    //removing "abc:" from string in text field
    NSString *str=[self stringWithoutAbc:sender.text];

    //if something was removed - changing text in text field
    if (![str isEqualToString:sender.text])
    {
        sender.text=str;
    };
}

Everytime user changes text in UITextField we remove "abc:" strings from this text using standard NSString method.

The problem is: the application is unstable. It sometimes crashes when "abc:" gets removed. Help me please. How to solve this problem?

Oleg
  • 1,383
  • 4
  • 19
  • 35
  • Usually crashed when 2-3 times "abc:" gets removed at the begginning (or at the end) of the typed text. And the we type "abc:" in the middle of typed text in `UITextField` – Oleg Feb 28 '12 at 08:56
  • 1
    Have you checked for NSZombie's with Instruments? – Kheldar Feb 28 '12 at 08:56
  • Garbage collector? In iOS? Do you mean ARC? – sosborn Feb 28 '12 at 09:00
  • I'm launching profiling (instruments app) -> memory -> zombies. Of course there are some memory allocations. How can I see if there are zombies? – Oleg Feb 28 '12 at 09:03
  • 1
    There is a tick box to be ticked, then you run your software until it crashes. Instruments will tell you if it detects a zombie and guide you to the responsible line. – Kheldar Feb 28 '12 at 09:24
  • I thought so. There's no zombies detected in this app – Oleg Feb 28 '12 at 10:04

2 Answers2

4

You can use below textField's delegate method :-

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
Maulik
  • 19,348
  • 14
  • 82
  • 137
Rama Rao
  • 1,043
  • 5
  • 22
  • If i use this delegate method I hope _my_ problem will be solved **but** it is strange that I can not use `UITextField`'s send actions for this very simple task – Oleg Feb 28 '12 at 09:06
  • 1
    If the text gets edited in "editing changed" send action of `UITextField` the app will be unstable. Is it a bug or something like that? – Oleg Feb 28 '12 at 09:08
1

If you are using Arc, you don't have to retain the object. And btw, ARC is not garbage collector. http://longweekendmobile.com/2011/09/07/objc-automatic-reference-counting-in-xcode-explained/ MAybe because you use retain on the textField.

Adrian Ancuta
  • 1,036
  • 9
  • 16