I'm trying to compare a value to one stored in a NSString, but I get the following error:
Implicit conversion of int to 'NSArray *' is disallowed with ARC
Here is my code:
NSString *tmplabel = [tmpC description];
temp.text = [NSString stringWithFormat: #"%.f", [tmplabel doubleValue]];
if (tmpC >= 10) {
// perform action
}
else {
// perform action
}
How can I do this comparison? I just want to see if the value stored is bigger than or equal to 10.
Thanks for your time :)
Related
- (IBAction)btndlr:(id)sender
{
NSString *str =txtdata.text;
lblfinal.text=[NSString stringWithFormat:#"%d/60",(int)str];
}
After running this code without any errors i get garbage value as output in the label.any guidance will be appreciated.thank you.
You must use the NSString method intValue to make it work, try this:
- (IBAction)btndlr:(id)sender
{
NSString *str = txtdata.text;
lblfinal.text = [NSString stringWithFormat:#"%d/60",[str intValue]];
}
EDIT:
If you want to perform the calculation before writing it out, you should change the last line to:
lblfinal.text = [NSString stringWithFormat:#"%d",[str intValue]/60];
And by the way, if you use division you might want a decimal number as output, then you can write:
lblfinal.text = [NSString stringWithFormat:#"%.2f",[str floatValue]/60];
Hope it helped!
You can't do computations inside format strings. You have to convert the string to an integer with e.g. the intValue property, and perform the computation outside:
- (IBAction)btndlr:(id)sender
{
NSString *str = txtdata.text;
lblfinal.text = [NSString stringWithFormat:#"%d", str.intValue / 60];
}
Please try this one
- (IBAction)btndlr:(id)sender
{
NSString *str = txtdata.text;
lblfinal.text = [NSString stringWithFormat:#"%d",[str intValue]/60];
}
I've been trying to pull the value "tempF" from /var/mobile/Library/Preferences/com.skylerk99.snappref.plist to return for the float value.
-(float) temperatureDegFahrenheit {
NSDictionary *pref = [[NSDictionary alloc]initWithContentsOfFile:#"/var/mobile/Library/Preferences/com.sky.snap.plist"];
NSString *tempF;
if( [[pref objectForKey:#"showF"] boolValue] ) {
return tempF;
}
else {
return %orig;
}
}
but I keep getting the error
cannot initialize return object of type 'float' with an lvalue of type 'NSString *'
return tempF;
What is the best way to accomplish this, because I obviously don't know?
Update:
Based on the discussion below, let's assume the temperature is entered through a UITextField called temperatureTextField. You can get the float value of the input temperature like this:
[[temperatureTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] floatValue];
The function expects you to return float, but tempF is a NSString *.
To parse float value from a NSString, try this (by the way, I assume that you did not show all your code, otherwise your tempF was never assigned):
[tempF floatValue];
Hi I have made an IOS app that converts binary, hexadecimal and decimal values. It all works fine except for my decimal to binary conversion. Here is what I have. It returns 0s and 1s but far too many. Can anyone tell me why this is or help me with a better method?
NSString *newDec = [display text]; //takes user input from display
NSString *string = #"";
NSUInteger x = newDec;
int i = 0;
while (x > 0) {
string = [[NSString stringWithFormat:#"%u", x&1] stringByAppendingString:string];
x = x>> 1;
++i;
}
display.text = string; //Displays result in ios text box
Try this:
NSUInteger x = [newDec integerValue];
And next time don't ignore the Compiler's "Incompatible pointer to Integer conversion" hint...
Explanation: Afaik, assigning an object to an int, actually assigns the address of the object to that integer, not the content of the string (which is what you want).
I'm getting a value back from an Objective C library (it's Firebase, but that doesn't really matter) of type id. The documentation states that this value will be an NSNumber for both boolean and numeric results. I want to take a different action based on whether or not this result corresponds to a boolean.
I know this is possible because printing out the class of the result via NSStringFromClass([value class]); gives "__NSCFBoolean" for booleans, but I'm not really sure how to correctly structure the comparison.
The objCType method gives information about the type of the data contained in the
number object:
NSNumber *n = #(1.3);
NSLog(#"%s", [n objCType]); // "d" for double
NSNumber *b = #YES;
NSLog(#"%s", [b objCType]); // "c" for char
The possible values are documented in
"Type Encodings"
in the "Objective-C Runtime Programming Guide".
Since BOOL is defined as unsigned char, it is reported as such by this method.
This means that you cannot distinguish it from a NSNumber object containing any char.
But it is sufficient to check between "boolean" and "numeric":
if (strcmp([obj objCType], #encode(BOOL)) == 0) {
// ...
} else if (strcmp([obj objCType], #encode(double)) == 0) {
// ...
} else {
// ...
}
I have got a problem with converting an NSNumber value to an NSString
MyPowerOnOrNot is an NSNumber witch can only return a 1 or 0
and myString is an NSString..
myString = [NSString stringWithFormat:#"%d", [myPowerOnOrNot stringValue]];
NSLog(#"%#",myString);
if(myString == #"1") {
[tablearrayPOWERSTATUS addObject:[NSString stringWithFormat:#"%#",#"ON"]];
}
else if(myString == #"0") {
[tablearrayPOWERSTATUS addObject:[NSString stringWithFormat:#"%#",#"OFF"]];
}
What is wrong with this?
The NSLog shows 0 or 1 in the console as a string but I can't check it if it is 1 or 0 in an if statement?
If doesn't jump into the statements when it actually should.. I really don't understand why this doesn't works..
Any help would be very nice!
A couple of problems
myString = [NSString stringWithFormat:#"%d", [myPowerOnOrNot stringValue]];
-stringValue sent to an NSNumber gives you a reference to a string. The format specifier %d is for the C int type. What would happen in this case is that myString would contain the address of the NSString returned by [myPowerOnOrNot stringValue]. Or, on 64 bit, it would return half of that address. You could actually use [myPowerOnOrNot stringValue] directly and avoid the relatively expensive -stringWithFormat:
if(myString == #"1")
myString and #"1" are not necessarily the same object. Your condition only checks that the references are identical. In general with Objective-C you should use -isEqual: for equality of objects, but as we know these are strings, you can use -isEqualToString:
if ([[myPowerOnOrNot stringValue] isEqualToString: #"1"])
Or even better, do a numeric comparison of your NSNumber converted to an int.
if ([myPowerOnOrNot intValue] == 1)
Finally if myPowerOnOrNot is not supposed to have any value other than 0 or 1, consider having a catchall else that asserts or throws an exception just in case myPowerOnOrNot accidentally gets set wrong by a bug.
"myString " is a reference to a string, not the value of the string itself.
The == operator will compare the reference to your string literal and so never return true.
Instead use
if( [myString isEqualToString:#"1"] )
This will compare the value of myString to "1"
In Objective C; you can't compare strings for equality using the == operator.
What you want to do here is as follows:
[tablearrayPOWERSTATUS addObject:([myPowerOnOrNot integerValue]?#"ON":#"OFF"])];
Compact, fast, delicious.