Getting a value from a String

Member
Posts: 24
Joined: 2008.05
Post: #1
Im trying to make figure out how to test a string in an if statement but i cant seem to get the value that is in the string. The value is in the string object, that i know, but its getting out that seems a bit difficult. Here's a bit of the code. I apologize if it is atrocious and incoherent for i am just trying all I can to at the very least start programing.

Code:
-(IBAction)enterIt:(id)sender
{

    NSString *string = [[NSString alloc] init];
    string = [textField stringValue];
    NSLog(@"string from %@ is weird", string);
    
    if ([string length] ==0){
        NSLog(@"string from %@ is of zero lenght",string);
        return;
    }
    
        if (string == @"yes") //This right here is what is bothering me
                                                 // it should be [string ???] right?
       {
        NSLog(@"THIS WORKS!!!!!");
        NSString *answer1 = @"Thats Right!!";
        [answerField setStringValue:(NSString *)answer1];
        return;
    }
    [string release];
}

Again i do apologize if my inexperience is showing but I just want to do something and learn from it.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Code:
if ([string isEqualToString:@"yes"])

http://developer.apple.com/mac/library/d...tring.html
Quote this message in a reply
Member
Posts: 24
Joined: 2008.05
Post: #3
thank you so much.
Quote this message in a reply
Member
Posts: 27
Joined: 2008.12
Post: #4
BTW you're doing some other worrying stuff. Strings are immutable, so when you "assign" a string you're not changing its value; you're pointing the pointer at a completely different string object.

Code:
    //declare a string variable, point it at empty string A
    NSString *string = [[NSString alloc] init];

    //point string variable at atring B; string A gets leaked
    string = [textField stringValue];
    NSLog(@"string from %@ is weird", string);
    

//snip other code

    //release a string B, which you didn't alloc/init or copy or retain!
    //that can cause a crash
    [string release];
}

You should probably remove the alloc/init part and the release.

-- Available Now: Dead Panic, a casual zombie shooter!
-- Development Blog: How to make a game under $1k
-- Twitter: xsmasher
Quote this message in a reply
Member
Posts: 24
Joined: 2008.05
Post: #5
Thank you. In what instance would I need to utilize the alloc/init part?
Quote this message in a reply
Member
Posts: 27
Joined: 2008.12
Post: #6
Megamac04 Wrote:Thank you. In what instance would I need to utilize the alloc/init part?

I'd never init an empty string; it's easier to write string=@"" if you really need an empty string. There are other init methods you might need though, like initWithUTF8String: when reading strings from a database or initWithFormat: when you're creating a string from numbers or other data.

-- Available Now: Dead Panic, a casual zombie shooter!
-- Development Blog: How to make a game under $1k
-- Twitter: xsmasher
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  formatting value in a string sefiroths 3 4,203 Dec 12, 2011 02:41 AM
Last Post: sefiroths
  Parsing from a string to something faster? Madrayken 3 3,244 Aug 10, 2009 03:32 PM
Last Post: smasher
  Is -fconstant-string-class broken under the XCode/Next runtime Justin Brimm 17 7,966 Jun 12, 2008 01:04 PM
Last Post: Justin Brimm
  string to interger comparison? dareuhl 8 4,536 May 22, 2008 05:44 PM
Last Post: OneSadCookie
  char array versus pointer to string constant question WhatMeWorry 7 7,556 Jan 30, 2007 12:26 PM
Last Post: bronxbomber92