This question already has answers here:
NSUInteger should not be used in format strings?
(4 answers)
Closed 8 years ago.
I'm trying to cast a NSUInteger to a string so I can print a message. From searching, it seems like I need to use stringWithFormat, but I am getting an error that an implicit cast not allowed with ARC.
Here's the line in question:
NSString *text = [[NSString stringWithFormat: (#"%li", NSUInteger)];
I've tried changing the format specifier to %lu with no help.
Thanks.
You probably have a variable of type NSUInteger, something like
NSUInteger myNumber;
Then you can convert it to a string like this:
NSString *text = [NSString stringWithFormat:#"%li", myNumber];
A solution that I prefer now is this:
NSString *text = [NSString stringWithFormat:#"%#", #(myNumber)];
This helps avoid compile warnings about incorrect number formatting codes (after a long time I still get confused in them).
Related
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to get substring of nsstring?
how do I get a substring in iOS?
I am new to iphone.In my project i have a string such as chapter1 but actually i want the substring 1 only how it is possible.if any body know this please help me
Check out the substringFromIndex: or substringToIndex: methods of NSString. You can find the documentation for NSString here.
Ex:
NSString *string = #"Chapter1";
NSString *newString = [string subStringFromIndex: ([string length] - 1)]; // => "1"
This question already has answers here:
Hex in a stringWithFormat BAD ACCESS
(2 answers)
Closed 8 years ago.
I'm stuck.
I give you the exact code which I use, not a sample:
So, I make two strings like this:
DestChoice = [NSString stringWithFormat:#"%lX", [DestField integerValue]];
SourceChoice = [NSString stringWithFormat:#"%lX", [SourceField integerValue]];
So the user write the Source/Dest in Decimal value but they are stocked as hex in a string, and then I have a button which reformat the source & dest like this:
NSString * CommandReadyToSend = [NSString stringWithFormat:#"X:0/%#,%#\r", DestChoice, SourceChoice];
My code is working, BUT, strangly some values makes a EXC_BAD_ACCESS at the CommandReadyToSend part...
Example: If I write 1 in my text field => I receive 1 (hex value) as result in DestChoice
If I write 10 in my text field => I receive A in DestChoice
BUT If I write 31 in the text field, I'm supposed to get 1F, but I get a EXC_BAD_ACCESS...
Same thing with 29 (dec value)
It seems that certains hex value can't be reformatted to string, I don't understand...
Seems to be working for me, the cause must be elsewhere.
NSString *destChoice = [NSString stringWithFormat:#"%lX", 1];
NSString *sourceChoice = [NSString stringWithFormat:#"%lX", 31];
NSString *commandReadyToSend = [NSString stringWithFormat:#"X:0/%#,%#\r", destChoice, sourceChoice];
NSLog(#"%#", commandReadyToSend);
2012-05-10 13:56:29.092 test[9383:707] X:0/1,1F
-
btw; If DestField is a UITextField then you should be using [DestField.text integerValue]; - this is probability your problem.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert string to float in Objective-C
I'd like to convert a string to a float.
/* dict is an NSDictionary to load Preferences */
NSString *str = [dict objectForKey:#"key"];
This is where I got. Now I'd like to convert the string value (in this case #"32.0f") in a float, where it could be processed by my application. How can I do this?
CGFloat strFloat = (CGFloat)[str floatValue];
Just pass the message floatValue to the NSString object. NSString::floatValue
I have an NSString and fail to apply the following statement:
NSString *myString = #"some text";
[myString stringByAppendingFormat:#"some text = %d", 3];
no log or error, the string just doesn't get changed. I already tried with NSString (as documented) and NSMutableString.
any clues most welcome.
I would suggest correcting to (documentation):
NSString *myString = #"some text";
myString = [myString stringByAppendingFormat:#" = %d", 3];
From the docs:
Returns a string made by appending to the receiver a string constructed from a given format string and the following arguments.
It's working, you're just ignoring the return value, which is the string with the appended format. (See the docs.) You can't modify an NSString — to modify an NSMutableString, use -appendFormat: instead.
Of course, in your toy example, you could shorten it to this:
NSString *myString = [NSString stringWithFormat:#"some text = %d", 3];
However, it's likely that you need to append a format string to an existing string created elsewhere. In that case, and particularly if you're appending multiple parts, it's good to think about and balance the pros and cons of using a mutable string or several immutable, autoreleased strings.
Creating strings with #"" always results in immutable strings. If you want to create a new NSMutableString do it as following.
NSMutableString *myString = [NSMutableString stringWithString:#"some text"];
[myString appendFormat:#"some text = %d", 3];
I had a similar warning message while appending a localized string. This is how I resolved it
NSString *msgBody = [msgBody stringByAppendingFormat:#"%#",NSLocalizedString(#"LOCALSTRINGMSG",#"Message Body")];
I have a fairly simple question concerning NSString however it doesn't seem to do what I want.
this is what i have
NSString *title = [NSString stringWithformat: character.name, #"is the character"];
This is a line in my parser takes the charactername and inserts in into a plist , however it doesn't insert the #"is the character" is there something I'm doing wrong?
Your code is wrong. It should be :
NSString *title
= [NSString stringWithformat:#"%# is the character", character.name];
assuming that character.name is another NSString.
Read the Formatting String Objects paragraph of the String Programming Guide for Cocoa to learn everything about formatting strings.
stringWithFormat takes a format string as the first argument so, assuming character.name is the name of your character, you need:
NSString *title = [NSString stringWithformat: #"%s is the character",
character.name];
What you have is the character name as the format string so, if it's #"Bob" then Bob is what you'll get. If it was "#Bob %s", that would work but would probably stuff up somewhere else that you display just the character name :-)
Note that you should use "%s" for a C string, I think "%#" is the correct format specifier if character.name is an NSString itself.