Getting a value from a String
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.
Again i do apologize if my inexperience is showing but I just want to do something and learn from it.
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.
thank you so much.
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.
You should probably remove the alloc/init part and the release.
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
Thank you. In what instance would I need to utilize the alloc/init part?
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
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| formatting value in a string | sefiroths | 3 | 3,524 |
Dec 12, 2011 02:41 AM Last Post: sefiroths |
|
| Parsing from a string to something faster? | Madrayken | 3 | 2,700 |
Aug 10, 2009 03:32 PM Last Post: smasher |
|
| Is -fconstant-string-class broken under the XCode/Next runtime | Justin Brimm | 17 | 6,699 |
Jun 12, 2008 01:04 PM Last Post: Justin Brimm |
|
| string to interger comparison? | dareuhl | 8 | 3,864 |
May 22, 2008 05:44 PM Last Post: OneSadCookie |
|
| char array versus pointer to string constant question | WhatMeWorry | 7 | 6,641 |
Jan 30, 2007 12:26 PM Last Post: bronxbomber92 |
|

