Testing for sting in UITextField

Discussion in 'iOS Programming' started by larswik, Jan 17, 2012.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    I ran in to problems tonight testing to see if there was a string in a textField. Can you test to see if the textField is empty or do you need to copy the value in to an NSString first to test it? Even with the textField Empty it evaluates to TRUE and executes the if statement.

    Code:
    -(void)closeEntry{
        NSLog(@"The amount TextField: %@", amountTextField.text); // test 
    
       [COLOR="Red"] if (![amountTextField.text isEqualToString:@""]) [/COLOR]{
            entryAmount = [amountTextField.text floatValue];
            NSString *floatConversion = [NSString stringWithFormat:@"$ %@.02f", entryAmount];
            entryString = workTypeTextField.text;
            
            NSDictionary *tempDict = [[NSDictionary alloc] initWithObjectsAndKeys:floatConversion,@"entryAmount",entryString,@"entryDiscription", nil];
            
            [tableViewArray addObject:tempDict];
            NSLog(@"TabelViewArray %@", tableViewArray);
            
            totalIncome = 0.0;
            for (int i = 0; i < [tableViewArray count]; i++) {
                NSMutableDictionary *tempIncomeDict = [[NSMutableDictionary alloc] initWithDictionary:[tableViewArray objectAtIndex:i]];
                NSLog(@"TempIncomeDict %@", tempIncomeDict);
                NSString *tempString = [NSString stringWithFormat:@"%@", [tempIncomeDict objectForKey:@"entryAmount"]];
                
                totalIncome = totalIncome + [tempString floatValue];
            }
            total.text = [NSString stringWithFormat:@"%.02f",totalIncome];
            [self.tableView reloadData];
            
        }
       [enteryView removeFromSuperview];
    }
    
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    What is the output from the NSLog()? It's not null, is it? Because null isn't an empty string.

    What is the output if you NSLog the text property's length?

    Have you considered the possibility that the text property contains one or more spaces? A string of spaces would appear to be empty when NSLog'ed, yet not be equal to @"".

    Why don't you just try it the way you said: get the text property into an NSString* variable and test it there. In short: try it, see what happens.

    Having the text in a variable would make it easier to inspect in the debugger, too. If you don't know how to use the debugger, now would be a good time to learn.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    The result was null
    I added placeholder to the textField and thought that might be causing the problem. bellow is the code that creates the TextField and adds it to the pop up window I am creating.
    Code:
    CGRect frame1 = CGRectMake(60.0, 50.0, 100.0, 30.0);
        amountTextField = [[UITextField alloc] initWithFrame:frame1];
        amountTextField.borderStyle = UITextBorderStyleRoundedRect;
        amountTextField.font = [UIFont boldSystemFontOfSize:15.0];
        amountTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        amountTextField.placeholder = @"Enter Amount";
        amountTextField.textAlignment = UITextAlignmentCenter;
        amountTextField.delegate = self;
        [enteryView addSubview:amountTextField];
    
    I thought I would check first to see if I was missing something. I will write the extra code to pass the value to an NSString and test the string.

    Thanks!
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    It works when I assign the textField value to an NSString and test the String. But while doing it I hit a snag at first that gave me a SIGBRT error. I solved it but I don't know why my first attempt did not work? I first wrote this line of code which crashed it.

    Code:
    NSString *testAmountTextField = [[NSString alloc] initWithString:[amountTextField.text];
    
    Then I did it in 2 steps and it worked.

    Code:
     
        NSString *testAmountTextField = [[NSString alloc] init];
        testAmountTextField = amountTextField.text;
    
    To me they seem like the same code almost. I know that you can not reuse the same NSString so it creates a new one with the same name, NSString is not mutable. But why would the top code crash and the bottom code work?
     
  5. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #5
    Your first line of code in your last post won't even compile because it has mismatched []. What was the actual code you used?
     
  6. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    Are you referring to my first post I started this thread with or the one above your post?
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    It may work. but it also leaks memory. Do you know why?
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    Maybe you should check if amountTextField is null or not, before trying to get its text property.


    Code:
    NSString *testAmountTextField = [[NSString alloc] initWithString:[COLOR="Red"][SIZE="4"][[/SIZE][/COLOR]amountTextField.text];
    
    This line of code won't compile. What does the big red [ do?
     
  9. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #9
    dejo - For this project I am working on I am using ARC. from what I understand I am no longer responsible for releasing my objects. If I am wrong please let me know.

    chown33 - I see that typo. I have the method working now but I want to test it the other way as well, the version that had the typo, without the error. But in the end I think that I should be able to test if there is anything in the UITextField. But maybe (without reading the docs yet) the return type of a UITextField that is empty is null, which maybe different the @""?

    My final code
    Code:
    -(void)closeEntry{
        
        NSString *testAmountTextField = [[NSString alloc] init];
        testAmountTextField = amountTextField.text;
        
        if (testAmountTextField.length != 0) {
            entryAmount = [amountTextField.text floatValue];
    
            NSString *floatConversion = [NSString stringWithFormat:@"%.02f", entryAmount];
            
            NSLog(@"floatConversion %@", floatConversion);
            entryString = workTypeTextField.text;
            
            NSDictionary *tempDict = [[NSDictionary alloc] initWithObjectsAndKeys:floatConversion,@"entryAmount",entryString,@"entryDiscription", nil];
            
            [tableViewArray addObject:tempDict];
            NSLog(@"TabelViewArray %@", tableViewArray);
            
            totalIncome = 0.0;
            for (int i = 0; i < [tableViewArray count]; i++) {
                NSMutableDictionary *tempIncomeDict = [[NSMutableDictionary alloc] initWithDictionary:[tableViewArray objectAtIndex:i]];
                NSLog(@"TempIncomeDict %@", tempIncomeDict);
                NSString *tempString = [NSString stringWithFormat:@"%@", [tempIncomeDict objectForKey:@"entryAmount"]];
                
                totalIncome = totalIncome + [tempString floatValue];
            }
            total.text = [NSString stringWithFormat:@"%.02f",totalIncome];
            [self.tableView reloadData];
            
        }
       [enteryView removeFromSuperview];
    }
    
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Alright. But answer me this then: why do you think you need to alloc/init your testAmountTextField string when you are simply going to reassign its pointer in the very next statement?

    P.S. You should probably check to make sure testAmountTextField is not nil before you access its length property.
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    Actually checking for length this way is something I do all the time. It does two checks at once. It checks for nil and checks that the length is zero. If the value is nil the result will still be 0. In many cases I don't care if the text is nil or if the string is non-nil but it's length is nil.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Damn, I meant to say "amountTextField". In case there's an issue with the connection/IBOutlet and you want to handle it specially.
     
  13. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #13
    I see. You meant the text field variable itself not the text from the text field. His variable names are confusing. But OP says it's working now so I guess the outlet must be connected.
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    I did.

    They are.

    I'd probably use something like amountString rather than testAmountTextField, amountFloat instead of entryAmount, etc.
     
  15. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #15
    dejo - That is a good point and it alloc / init has become a habit for me. I did try to write that in one line first but I had a typo and was causing problems.

    Code:
    NSString *testAmountTextField = [[NSString alloc] initWithString:[COLOR="Red"][[/COLOR]amountTextField.text];
    One of my big hang ups still is when to use the []. I know that I use the [] when I work with objects so it seemed right to use [amountTextField.text];. The amountTextField is a UITextField object.

    So the next line of code feels wrong even though it is right.
    Code:
        NSString *testAmountTextField = [[NSString alloc] init];
        testAmountTextField = amountTextField.text;
    My first thought on the first line of code is that I have to create an object to be used. Then next line I take the object I created and use it, in this case assign it the the amountTextField.text.

    I have to remind myself all the time that I am working with pointers and not the objects themselves. So bellow the newString pointer, which is of type NSString, is begin assigned to the 'text' in the amountTextField if I said that right?

    newString = amountTextField.text;
     
  16. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #16
    Sort of a reverse Hungarian notation ;-)

    I'd use text and value or even v.
     

Share This Page