Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tools & Utilities

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2011, 02:40 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 44
Karl6669 is on a distinguished road
Arrow if statement to see if a text field is empty

Hi, I have made a settings bundle for my app containing several NSStrings. I then have an if statement where I want it to do one thing if there is ANYTHING entered into that field, and something else if there is NOTHING in that field. Can't seem to get it to work properly though. Any tips?

Code:
    if ([[NSUserDefaults standardUserDefaults] stringForKey:(@"supervisor_email")]) {
        [supervisorsEmail setFont:[UIFont italicSystemFontOfSize:(8)]];
        [supervisorsEmail setText:[defaults stringForKey:@"supervisor_email"]];
    } else {
        [supervisorsEmail setTextColor:[UIColor redColor]];
        [supervisorsEmail setText:@"Not Set Yet"];
        UIAlertView *supervisorEmail = [[[UIAlertView alloc] 
                                         initWithTitle:@"SUPERVISOR'S EMAIL"
                                         message:@"You have not assigned a supervisor's email address yet.  This is required to email the report.  Please go to you iPhone settings."
                                         delegate:self 
                                         cancelButtonTitle:@"Dismiss"
                                         otherButtonTitles:nil] autorelease];
        [supervisorEmail setTag:4];
        [supervisorEmail show];
    };
Quote:
I have also tried boolForKey, but, either way I do it, it always returns the same result. Any advice?
Karl6669 is offline   Reply With Quote
Old 07-10-2011, 07:46 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 23
imtz is on a distinguished road
Default

Try

Code:
NSString * supervisorEmail = [[NSUserDefaults standardUserDefaults] stringForKey:@"supervisor_email"];

if (supervisorEmail == nil || [supervisorEmail isEqualToString:@""]) {
    // String is Empty or doesn't exist.
}
else {
    // String is not empty.
}
imtz is offline   Reply With Quote
Old 07-10-2011, 09:38 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 50
Maxner is on a distinguished road
Default

the ==nil wont work, but this will:


NSString * supervisorEmail = [[NSUserDefaults standardUserDefaults] stringForKey:@"supervisor_email"];

if ([supervisorEmail isEqualToString:@""]) {
//NSUSerDefaults is empty
}
else {
//NSUSerDefaults is not empty!
}
Maxner is offline   Reply With Quote
Old 07-10-2011, 07:18 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Quote:
Originally Posted by Maxner View Post
the ==nil wont work
Quote:
stringForKey:
Returns the string associated with the specified key.

- (NSString *)stringForKey: (NSString *)defaultName
Parameters
defaultName
A key in the current user's defaults database.
Return Value
The string associated with the specified key, or nil if the default does not exist or does not contain a string.
.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 07-10-2011, 10:15 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 44
Karl6669 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
.
Quote:
Thanks guys, I solved it slightly differently:
Code:
NSString *superEmail = [defaults stringForKey:@"supervisor_email"];
    if ([superEmail length] > 0) {
         //blah
    } else {
         do something else
    }
Karl6669 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 409
11 members and 398 guests
AppleDev, chemistry, Emy, Gi-lo, ipodphone, mistergreen2011, pipposanta, QuantumDoja, Retouchable, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,924
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:29 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0