In Xcode I'm trying to get the text of an NSTextField (Label) to see if it says Yes or it is says No
I've tried this:
if ([LabelYesNo StringValue] == #"Yes"){
[LabelYesNo setStringValue:#"No"];
else{
[LabelYesNo setStringValue:#"Yes"];
}
}
and
if (LabelYesNo isEqualToString #"Yes"){
[LabelYesNo setStringValue:#"No"];
else{
[LabelYesNo setStringValue:#"Yes"];
}
}
and a few other variations of that. Just can't seem to get it right.... Can anyone help?
Thanks
[[theTextField stringValue] isEqualToString:#"Yes"];
should work
in your first code, you're comparing strings via ==. Using the C == operator will simply compare the addresses of the objects.
in your second code, your whole code is wrong, and you'are trying to compare element of type NSTextField to NSString.
see String comparison in Objective-C
Related
- (IBAction)addSpace:(id)sender {
//here noSpce is a textbox in which i will enter some value
NSString *one=self.noSpace.text;
if (one.length==4)
//if the length of the string is equal to four then it should put a whitespace
//in between the string.
{
NSString *blank=#" ";
NSString *two=[blank. stringByAppendingString:one,index(2)];
NSString *new=two;
//here withSpace is another textbox in which i want to show the expected result
self.withSpace.text=new;
}
//this really does not work for me
NSString *two=[blank. stringByAppendingString:one,index(2)];
There's an extraneous . after blank in the method call. Remove that and the index(2) and your code should work better.
Your code would be improved by using a mutable string. You could use -insertString:atIndex: and avoid the need for all those intermediate strings.
Update: You should also change the name of your new variable. new is the name of a method in NSObject, and also a reserved word in C++. Using it as a variable name should work, but it's bound to lead to confusion in the future.
- (IBAction)addSpace:(id)sender
{
//here noSpace is a textBox
NSMutableString *string1=[NSMutableString stringWithString:self.noSpace.text];
//this was what i was looking for. . .
[string1 insertString:#" " atIndex:2];
//showing result of noSpace textBox in withSpace textBox.
self.withSpace.text=string1;
}
I have searched all over the place for this, including apple's docs on NSString (maybe I didn't see?) but I am trying to find a method in xCode for checking wether a NSString does not equal to something. Much like
if (myNSSting = #"text" {...
except specifically I want to check if it does not equal to 'text'.
if(![myNSString isEqualToString:#"text"])
For case insensitive compare we can handle by:
if( [#"Some String" caseInsensitiveCompare:#"some string"] == NSOrderedSame ) {
// strings are equal except for possibly case
}
I'm very new to objective-c and I'm getting a basic error and unsure on how to fix it.
for(ZBarSymbol *sym in syms) {
resultText.text = sym.data; //sym.data = 0012044012482
[self phpPost:(int)sym.data];
break;
}
}
- (void)phpPost: (int)barcode {
NSString *theValue = [NSString stringWithFormat:#"%#", barcode]; //problem line
labelScan.text = theValue;
//labelScan.text = #"Barcode scanned";
}
when i use #"%#" the text of the label is correct (0012044012482), but when i use #"%d" it isn't (random number every time i restart the app). I would like to use #"%#" (or something that works) but for some reason xCode is giving me an error. and I'm unsure on how to fix the error.
The error is:
Format specifies type 'id' but the argument has type 'int'
In the end I plan on having that code (sym.data) written to a MySQL database using the POST method.
You can't just convert it to an int by casting if it's an object (which it must be if the %# format specifier isn't causing a crash).
Assuming from the fact that you're assinging it directly to a label's text that it's an NSString, you should either change the parameter type of phpPost:
- (void)phpPost: (NSString *)barcode {
labelScan.text = barcode;
}
or extract the intValue before passing sym.data:
[self phpPost:[sym.data intValue]];
and then use the proper %d format specifier in phpPost:.
Your barcode isn't an int, it is an NSString. Instead of doing (int)sym.data, pass in [sym.data intValue]. That should correctly convert it to an integer.
The reason you get a random number is because you can't just cast a string object to a primitive data type :)
I don't know what type sym.data is, but it is likely a pointer to an object, and not the value itself. You cast that pointer to int, so when you are using %d you are effectively printing the memory location of the object. That is why it changes each time you run the program (Objective-C let's you do this without any warnings - something to watch out for).
To fix this, either extract the integer value you need from the sym.data object using it's properties; or pass the object as a pointer. For instance, you could try calling your method like this:
[self phpPost:sym.data];
And then change your method to be:
- (void)phpPost: (id)barcode {
NSString *theValue = [NSString stringWithFormat:#"%#", barcode];
labelScan.text = theValue;
}
Ok, I did some thinking while I was at work today, and I figured out that an INT isn't going to work for me. if I make that object to an int, I would loss some data that is vital to what I'm doing. eg. object=001234 int=1234. I need the zeros. So, in the end, I'm keeping it an object (string) and just passing it into the function.
Here is my code after I got it working correctly.
for(ZBarSymbol *sym in syms) {
resultText.text = sym.data;
[self phpPost:sym.data];
break;
}
}
- (void)phpPost: (NSString *)barcode {
labelScan.text = barcode;
//labelScan.text = #"Barcode scanned"; //My custon label
}
Thanks, everyone for your responses. Your answer will not go unused. I'm sure I'll be needing this information here soon.
O, if you see that I did this wrong, or not the correct way, please make a comment and tell me .
i'm parsing something from the Apple JSON (the rating of the app) and i tried something like:
if ([StoreParse objectForKey:#"averageUserRating"] == #"4.5") {
NSLog(#"xx");
} else {
NSLog(#"xxx");
}
The App has a rating of 4.5 and if i do
NSlog (#"%#", [StoreParse objectForKey:#"averageUserRating"]);
The output is : 4.5
but when i run the script the NSlog in the first code's output is "xxx" does anyone can help me?
Comparing strings, which are essentially pointers to instances of the NSSring class, is erroneous, as two identical-content strings can have a different memory address. Use
if ([[StoreParse objectForKey:#"averageUserRating"] isEqualToString:#"4.5"])
instead.
Use isEqualToString:
if ([[StoreParse objectForKey:#"averageUserRating"] isEqualToString:#"4.5"]) {
NSLog(#"xx");
}
You can't do this:
if ([StoreParse objectForKey:#"averageUserRating"] == #"4.5")
You need to do:
if ([[StoreParse objectForKey:#"averageUserRating"] isEqualToString:#"4.5"])
That's assuming it's a string. If it's an NSNumber then do:
if ([[StoreParse objectForKey:#"averageUserRating"] floatValue] == 4.5f)
(Although be careful about comparing equality of floats)
See this question for more information on string equality.
If the value for averageUserRating is an NSNumber, you should convert it to a formatted NSString first then compare it to the #"4.5" string literal:
if ([[NSString stringWithFormat:#"%1.1f", [[StoreParse objectForKey:#"averageUserRating"] floatValue]] isEqualToString:#"4.5"])
I've been struggling with a simple comparison but I can't get it to work.
I´m reading a XML file and I need to compare data from it in order to show the right picture.
http://www.cleaner.se/larm.xml (Example file for parsing)
I have tried things like:
if([aLarm.larmClass isEqualToString:#"A"])
NSLog(#"same");
else
NSLog(#"Not same");
If I use: NSLog(aLarm.larmClass); console puts it out nicely as it should. What am I doing wrong?
You can use the NSString compare: methods. For example:
if ([myString caseInsensitiveCompare:#"A"] == NSOrderedSame ) {
NSLog(#"The same");
} else {
NSLog(#"Not the same.");
}
The result is an NSComparisonResult which is just an enum with types NSOrderedSame, NSOrderedAscending and NSOrderedDescending.
Check the documentation on the various compare: methods here.
Of course, if the receiver is actually an NSString, then isEqualToString: should also work. So if you're trying to compare a class name (aLarm.larmClass ??), then you can call:
if ([NSStringFromClass([aLarm class]) isEqualToString:#"A"] ) {
NSLog(#"The same");
}
If the larmClass property is a string, make sure that it is actually one character in length (i.e. it doesn't have any leading or trailing whitespace that was accidentally included when parsing the XML). If the larmClass property truly is an NSString containing the letter ‘A’ then [aLarm.larmClass isEqualToString:#"A"] will return YES.
Do a:
NSLog(#"%u, %#", [aLarm.larmClass length], aLarm.larmClass);
and just make sure that it shows “1, A”.