I am having trouble trying to run this test due to an error on the first line of this code that states:
Incompatible pointer to integer conversion initializing 'int' with an
expression of type 'NSString *'
- (IBAction)Button:(id)sender
{
int x = TramNumber.text;
if (x < 9)
{
Tramresult.text = [NSString stringWithFormat:#"lol"];
}
else
{
NSLog (#"x is less than 9!");
}
}
#end
Please help. I am on iOS and running xCode 5.1.1 if that helps.
You are representing NSString value wrong.
Use this code sample to solve your problem:
int x = [TramNumber.text intValue];
To represent int value from your textfield.
int x = [TramNumber.text intValue];
You just need to convert NSString to int.
You cannot assign text as an integer.
You should cast NSString to int like this:
int x = [TramNumber.text intValue];
Just try this:
NSString *code = dic[#"code"];
int statusCode = [code intValue];
NSLog(#"%d",statusCode);
if(statusCode==1)//........cont...
Related
I am having serious problem while conversion.
The first solution I tried is the below code ::
NSByteCountFormatter *formatter = [[NSByteCountFormatter alloc] init];
formatter.allowsNonnumericFormatting = NO;
return [NSByteCountFormatter stringFromByteCount:valueFromAPI countStyle:NSByteCountFormatterCountStyleMemory];
The second solution I tried is the below code ::
double convertedValue = (double)valueFromAPI;
DebugLog(#"%f",convertedValue);
int multiplyFactor = 0;
NSArray *tokens = #[#"bytes",#"KB",#"MB",#"GB",#"TB",#"PB",#"EB",#"ZB",#"YB"];
while (convertedValue >= 1024) {
convertedValue /= 1024;
multiplyFactor++;
}
return [NSString stringWithFormat:#"%4.0f %#",convertedValue, tokens[multiplyFactor]];
But both returns the same weird output. After checking for a lot of cases I found that it only happens when the file size is small.
The image displays the problem :
The image clearly shows that the returned file sizes are in negative and in exobyte(EB) which are actually just images picked from UIImagePickerController.
In the second solution I found out that while converting from long long to double` it is converted to extended format which creates the problem.
I don't how to solve it. Please help me out. Thanks in advance.
Try this method.
Usage :
#import "FileSize.h"
...
unsigned long long fileSize = ...;
NSString *compressedFilesSize = [FileSize convertSize:fileSize];
NSLog(#"File size : %#", compressedFilesSize);
From conversing with you via comments you have stated that valueFromAPI is an NSString *, in which case the issue is that you are converting the address of the string, not the value it contains:
double convertedValue = (double)valueFromAPI;
You want:
double convertedValue = [valueFromAPI doubleValue];
- (NSString*)transformedValue:(double) valueFromAPI
{
NSByteCountFormatter *formatter = [[NSByteCountFormatter alloc] init];
formatter.allowsNonnumericFormatting = NO;
return [NSByteCountFormatter valueFromAPI countStyle:NSByteCountFormatterCountStyleMemory];
}
I was converting the valueFromAPI to 'long long' and then to 'double' which created problem. So I converted the NSString to double and directly passed it. Now, my file sizes are showing correctly.
Thanks to all for your suggestions.
The code showing here assigns the value of an variables to the text field I want to do the opposite of that.
_Initial.text = [NSString stringWithFormat:#"%f", init];
The opposite of the code above. Please help
The opposite of that would be getting converting string to float. You can do it with this
init = [_Initial.text floatValue];
You should search for an answer before you ask a question.
NSString *intString = #"123";
NSString *floatString = #"456.789";
int intVal = [intString intValue];
float floatVal = [floatString floatValue];
printf("%i %f\n",intVal,floatVal);
With NSNumber being a class, I am trying to convert it to an NSInteger to do some computations. In NSLog, it shows that I am converting and doing the multiplication correct. However, when I got to return doubler as a regular NSInteger, I get "Implicit conversion of 'NSInteger' (aka 'long') to 'NSNumber* ' is disallowed with ARC". Where am I going wrong and what do I do to make this correct?
- (NSNumber *) numberThatIsTwiceAsBigAsNumber:(NSNumber *)number {
NSInteger doubler = [number integerValue] * 2;
NSLog(#"%ld", (long)doubler);
return doubler;
}
EDIT: For those curious, this is how I solved it:
NSInteger unboxing = [number integerValue] * 2;
NSNumber *boxing = [NSNumber numberWithInteger:unboxing];
return boxing;
You need to return the number as NSNumber, not NSInteger. So, convert the NSInteger to NSNumber before returning.
return #(doubler);
Change your return type to
- (NSInteger)
If you intend to continue to use it as such, or explicitly cast it.
Following is a code snippet i am using to add data to nsmutable array, now I am not sure on what to type cast it on while extracting, i need integer value.
Problem is that I am getting warnings of 'id' and 'NSInteger' conversion. What could be better way of extracting:
self.itemsBottom = [NSMutableArray array];
for (int i = 20; i < 30; i++)
{
[itemsBottom addObject:#(i)];
}
wanna do something like:
NSInteger itemAddressed = [self.itemsBottom objectAtIndex:itemIndex]
In this statement
[itemsBottom addObject:#(i)];
you are boxing the integer value to NSNumber.
While here
NSInteger itemAddressed = [self.itemsBottom objectAtIndex:itemIndex]
you are tried to store NSNumber to NSInteger, hence getting the error.
You can use :
NSInteger itemAddressed = [[self.itemsBottom objectAtIndex:itemIndex] integerValue];
Or in short :
NSInteger itemAddressed = [self.itemsBottom[itemIndex] integerValue];
All seems reasonable...I would think the last line would need to be...
NSInteger itemAddressed = [self.itemsBottom[itemIndex] integerValue];
Maybe?
I'm having trouble converting a string into an integer. I googled it but all I can find is how to convert an int into a string. Does anyone know how to do it the other way around? Thanks.
See the NSString Class Reference.
NSString *string = #"5";
int value = [string intValue];
How about
[#"7" intValue];
Additionally if you want an NSNumber you could do
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter numberFromString:#"7"];
I use:
NSInteger stringToInt(NSString *string) {
return [string integerValue];
}
And vice versa:
NSString* intToString(NSInteger integer) {
return [NSString stringWithFormat:#"%d", integer];
}
This is the simple solution for converting string to int
NSString *strNum = #"10";
int num = [strNum intValue];
but when you are getting value from the textfield then,
int num = [txtField.text intValue];
where txtField is an outlet of UITextField
Swift 3.0:
Int("5")
or
let stringToConvert = "5"
Int(stringToConvert)
I had to do something like this but wanted to use a getter/setter for mine. In particular I wanted to return a long from a textfield. The other answers all worked well also, I just ended up adapting mine a little as my school project evolved.
long ms = [self.textfield.text longLongValue];
return ms;
NSString *string = /* Assume this exists. */;
int value = [string intValue];
Very easy..
int (name of integer) = [(name of string, no ()) intValue];
Yet another way: if you are working with a C string, e.g. const char *, C native atoi() is more convenient.
You can also use like :
NSInteger getVal = [self.string integerValue];
To convert an String number to an Int, you should do this:
let stringNumber = "5"
let number = Int(stringNumber)