Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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];
}
 
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.
 
The result was null
2012-01-17 21:56:38.815 Freelance Pro[5697:207] The amount TextField: (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!
 
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?
 
Your first line of code in your last post won't even compile because it has mismatched []. What was the actual code you used?
 
Are you referring to my first post I started this thread with or the one above your post?
 
The result was null

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?
 
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];
}
 
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.
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.
 
P.S. You should probably check to make sure testAmountTextField is not nil before you access its length property.

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.
 
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.

Damn, I meant to say "amountTextField". In case there's an issue with the connection/IBOutlet and you want to handle it specially.
 
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.
 
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?

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;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.