It was very strange when I saw this on debugging my application.
int iTag = btnTemp.tag; //btnTemp.tag = 1
NSString *strFriendID = [NSString stringWithFormat:#"%i",iTag];
gave me strFriendID as empty string.
int iTag = btnTemp.tag; //btnTemp.tag = 1
NSString *strFriendID = [NSString stringWithFormat:#"%d",iTag];
gave me strFriendID as 1.
How can this happen?
i don't know why you get this answer, but when i read your question i tried in my project but i get the value
UIButton *btnTemp = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btnTemp.tag = 1;
int iTag = btnTemp.tag; //btnTemp.tag = 1
NSString *strFriendID = [NSString stringWithFormat:#"%i",iTag];
NSLog(#"Str %#", strFriendID);
NSString *strFriendID1 = [NSString stringWithFormat:#"%d",iTag];
NSLog(#"Str %#", strFriendID1);
Out Put
2012-03-26 10:32:02.899 Leaves[506:f803] Str 1
2012-03-26 10:32:02.901 Leaves[506:f803] Str 1
both gives me 1
As per Apple:
%d, %D and %i all represent Signed 32-bit integers.
So yeah it's weird that %i didn't work but it's not so weird that %d worked.
Perhaps btnTemp.tag was null at that point
Related
How to remove string from the original one
i.e. my string is,
series0001.0001
or
series0010.0101
or
series0110.0050
from this string I have to convert it to
expected result
Series 1 1
Series 10 101
Series 110 50
This is the code stuff which I am doing
NSArray * words = [sym.data componentsSeparatedByString:#"\r\n\r\n"];
NSLog(#"words are %#",words);
NSString *strSeriesAndLabelDetail = [words objectAtIndex:0];
NSLog(#"strSeriesAndLabelDetail is %#",strSeriesAndLabelDetail);
NSArray *wordsToSeparateSeriesAndLabel = [strSeriesAndLabelDetail componentsSeparatedByString:#"."];
NSLog(#"wordsSeriesLabel are %#",wordsToSeparateSeriesAndLabel);
strLabelNumber = [wordsToSeparateSeriesAndLabel objectAtIndex:1];
NSLog(#"strLabelNumber are %#",strLabelNumber);
strSeriesNumber = [wordsToSeparateSeriesAndLabel objectAtIndex:0];
NSLog(#"strSeriesNumber is %#",strSeriesNumber);
Current OutPut is:
words are (
"series0001.0003",
"Use the Sort-a-Cord app to read this code or visit www.sortacord.com to get your Sort-a-Cords."
)
strSeriesAndLabelDetail is series0001.0003
wordsSeriesLabel are (
series0001,
0003
)
strLabelNumber are 0003
strSeriesNumber is series0001
Can anybody help me out. Thanks in advance for any suggestion.
A way to do it:
NSString *stringToModify = #"series0110.0050";
stringToModify = [stringToModify stringByReplacingOccurrencesOfString:#"series" withString:#""];
NSArray *array = [stringToModify componentsSeparatedByString:#"."];
NSString *finalString = [NSString stringWithFormat:#"Series %d %d", [[array objectAtIndex:0] integerValue], [[array objectAtIndex:1] integerValue]];
NSLog(#"%#",finalString);
Note that it maybe modified according to what you really want. I assumed that you got always "series" to look for.
I am working with Sqlite Database.
In my database
I have some filed Like, Invoice,Name,Date
My Invoiceno datatype is INTEGER,
In Select query Got output like this.
Query:(
{
InvoiceNo = 1;
Name : ABC
Date = "2012-04-16 00:00:00";
}
)
But when try to Get that Invoice no to string it's given Diff. Value.
Code for getting invoice from array to String:
NSString *str = [NSString stringWithFormat:#"%i",[[Query objectAtIndex:indexPath.row] valueForKey:#"InvoiceNo"]];
NSLog(#"str:%#",str);
Here, My invoice no is 1, & It's give me a value "2387056"
Please, Help me..How can i solve this problem?
NSString *str = [NSString stringWithFormat:#"%i",[[Query objectAtIndex:indexPath.row] valueForKey:#"InvoiceNo"]];
NSLog(#"str:%#",str);
you have to replace above lines with below lines
NSString *str = [NSString stringWithFormat:#"%i",[[[Query objectAtIndex:indexPath.row] valueForKey:#"InvoiceNo"] intValue]];
NSLog(#"str:%#",str);
Convert NSString to int
NSString *values = #"1";
int yourValue = [values intValue];
NSLog(#"Int Value : %d",yourValue);
I have an NSString *string=#"606" and I want to add a ":" colon after two digits.
The output should look like this: 6:06
It this is possible?
Help would be appropriated.
Thank you very much.
You can add the column between the hours and the minutes like this:
NSString *string = #"606";
NSString *result = [string stringByReplacingCharactersInRange:NSMakeRange(string.length-2, 0) withString:#":"];
NSLog(#"%#", result);
This will give the following results
#"606" => #"6:06"
#"1200" => #"12:00"
#"1406" => #"14:30"
Note: This will only work if the string has 3 or 4 characters, but this is the case according to your question.
Because you do not have two separate objects for hours and minutes, use:
NSString *newTimeString, *hour, *minute;
NSUInteger length = [timeString length];
if (length == 3)
{
hour = [timeString substringToIndex:1];
minute = [timeString substringFromIndex:2];
}
else
{
hour = [timeString substringToIndex:2];
minute = [timeString substringFromIndex:3];
}
newTimeString = [NSString stringWithFormat:#"%#:%#", hour, minute];
I used a long version to illustrate the concept. Basically, use the length of the original string (timeString) to extract the time components and combine them with a colon.
Will it be 2 digits no matter what? Otherwise you could build your custom string from parameters.
NSString *string = [NSString stringWithFormat:#"%#:%#", hours, minutes];
UPDATE
If you just need to add a colon after 1 char, you can do it this way. Although I would suggest finding a safer method as this could be inaccurate if you have a double digit hour.
NSString *hour = [NSString substringToIndex:1]; // double check the index
NSString *min = [NSString substringFromIndex:1]; // double check the index
NSString *time = [NSString stringWithFormat:#"%#:%#", hour, min];
Hopefully this will help.
What would be the expected result from the following Objective C code?
int intValue = 1;
NSString *string = [NSString stringWithFormat:#"%+02d", intValue];
I thought the value of string would be "+01", it turns out to be "+1". Somehow "0" in format string "+01" is ignored. Change code to:
int intValue = 1;
NSString *string = [NSString stringWithFormat:#"%02d", intValue];
the value of string is now "01". It does generate the leading "0". However, if intValue is negative, as in:
int intValue = -1;
NSString *string = [NSString stringWithFormat:#"%02d", intValue];
the value of string becomes "-1", not "-01".
Did I miss anything? Or is this a known issue? What would be the recommended workaround?
Thanks in advance.
#Mark Byers is correct in his comment. Specifying '0' pads the significant digits with '0' with respect to the sign '+/-'. Instead of '0' use dot '.' which pads the significant digits with '0' irrespective of the sign.
[... stringWithFormat:#"%+.2d", 1]; // Result is #"+01"
[... stringWithFormat:#"%.2d", -1]; // Result is #"-01"
NSString *string = [NSString stringWithFormat:#"+0%d", intValue];
NSString *string = [NSString stringWithFormat:#"-0%d", intValue];
I hope you can help me out with this 'small' problem. I want to convert a string to a double/float.
NSString *stringValue = #"1235";
priceLabel.text = [NSString stringWithFormat:#"%d",[stringValue doubleValue]/(double)100.00];
I was hoping this to set the priceLabel to 12,35 but I get some weird long string meaning nothing to me.
I have tried:
priceLabel.text = [NSString stringWithFormat:#"%d",[stringValue intValue]/(double)100.00];
priceLabel.text = [NSString stringWithFormat:#"%d",[stringValue doubleValue]/100];
but all without success.
This is how to convert an NSString to a double
double myDouble = [myString doubleValue];
You have to use %f to show float/double value.
then %.2f means 2digits after dot
NSString *stringValue = #"1235";
NSString *str = [NSString stringWithFormat:#"%.2f",[stringValue doubleValue]/(double)100.00];
NSLog(#"str : %# \n\n",s);
priceLabel.text = str;
OUTPUT:
str : 12.35
I think you have the wrong format string. Where you have:
[NSString stringWithFormat:#"%d", ...];
You should really have:
[NSString stringWithFormat:#"%f", ...];
%d is used for integer values. But you're trying to display a floating point number (%f).