iOS set UILabel value - objective-c

I would like to append the word "Completed" to the end of the self.myLabel.text, following the value returned from numberValue.
So, say numberValue returns 234, I would like self.myLabel.text to become "234 completed".
I am sure this is a simple task, but am missing the obvious?!
The code used to display the number only is:-
self.myLabel.text = [NSString stringWithFormat:#"%ld", numberValue];

self.myLabel.text = [NSString stringWithFormat:#"%d completed", numberValue];

If you are using NSNumber (or any similar object class such as NSString, NSDecimalNumber, NSDate), use the %# format string.
So for your example: [NSString stringWithFormat:#"%# completed", numberValue];
If you are using NSInteger, you can use the %d/%ld (long integer) formats.

if numberValue is integer use the following:
self.myLabel.text = [NSString stringWithFormat:#"%d completed", numberValue];
if numberValue is of type NSNumber or some unknown type just use as follow:
self.myLabel.text = [NSString stringWithFormat:#"%# completed", numberValue];

As their is a first result in google and has not been updated for swift:
Swift:
var the_label
the_label.text = "the text to assign"

Related

Format string that already has format specifier %#

I have a string which already contains a formatter %#.
NSString *str = #"This is an %#";
I need to parse that string and to replace %# with 'example'. If I use
[NSString stringWithFormat:#"%#", str];
I get the following output:
This is an %#
I want output like:
This is an example
NSString *str = #"This is an %#";
str = [str stringByReplacingOccurrencesOfString:#"%#" withString:#"example"];
I would recommand to use the formatted string as "format"
NSString *str = #"This is an %#";
str = [NSString stringWithFormat:str, #"example"];
is working with every type. A better solution than replacing, because you can use unspecified replacings
is very usefull if you use localized.strings with x values you want to add ;)

How to use combination of text and variables in NSString?

How would I use a combination of text and variables in a NSString?
I know that in an NSLog, it looks like this:
int number = 5;
NSLog(#"My favorite number is %i", number);
How would I go about doing something like that in an NSString or even a char variable?
That is fairly simple:
NSString *string = [NSString stringWithFormat:#"My favorite number is %i", number];
its basically the same as nslog.
NSString * str = [NSString stringWithFormat:#"My favorite number is %i", number];
if you just want to read about format specifiers, see: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
use all the power of old beloved printf formatting.
for objects "%#" will call the description method, so be smart to write this method for every custom class:
-(NSString*)description;
{
NSString* result = [NSString stringWithFormat(#"%#" .......
}
for example:
-(NSString *)description;
{
return [NSString stringWithFormat:#"is: %# %# %# ; at: %f %f",
name, address, img_name,
coord.latitude, coord.longitude];
}

Objective-c integer to string giving random numbers?

I'm busy working on an iPad application and my web service returns pretty simple JSON data. All seems well and I have other methods doing this same conversion without issue however, I have 1 method that returns a random string when doing a integer -> string conversion.
My userdata object below is a NSDictionary created by the SBJSON parser. The value when debugging of [userdata objectForKey:#"UserID"] is 1.
However when I do this
NSString *userId = [NSString stringWithFormat:#"%d", [userdata objectForKey:#"UserID"]];
The value in userId is or appears to be a random number such as 23425234. I also tried the %d in my format but got the same result.
Because it is an object, not an int, you see the address of the object, instead, you can do that:
NSString *userId = [NSString stringWithFormat:#"%#", [userdata objectForKey:#"UserID"]];
^
Try with:
NSString *userId = [NSString stringWithFormat:#"%d", [[userdata objectForKey:#"UserID"] intValue]];
Or:
NSString *userId = [NSString stringWithFormat:#"%#", [userdata objectForKey:#"UserID"]];
object for key is probably NSNumber and not int...

Help with HelloWorld Objective-C

I'm Very new to Objective-C iPhone programming. I'm kinda gonna go with the trial-and-error method on this one:P. So my question has to do with getting a name. Here's my code:
-(IBAction)sayHello:(id)sender{
NSString *name = [textField text];
label.text = [NSString stringWithFormat:#"Hello ", name];
What this does is get the text from a UITextField (I already have this setup) and outputs to a UILabel. I would think this would work, but apparently not, because it just outputs "Hello ". Missing the name. Help would be appreciated :)
Thanks,
Eric
EDIT: Thanks to all that responded. It worked! :)
It should read:
label.text = [NSString stringWithFormat:#"Hello %#", name];
stringWithFormat uses "%" sequences to insert values into the string it's building. You use different "%" sequences depending on what type of value you're inserting: "%#" is for Objective-C objects, "%d" is for integers, and so forth. You can insert multiple values by using multiple "%" sequences, for example:
label.text = [NSString stringWithFormat:#"Hello %# %# and your %d friends", firstName, lastName, 7];
Try this:
[NSString stringWithFormat:#"Hello %#", name];
-(IBAction)sayHello:(id)sender{
NSString *name = [textField text];
label.text = [NSString stringWithFormat:#"Hello %#", name];
}
You have to add %#, so that it will place your name string in the formatted string.
You need a format specifier:
label.text = [NSString stringWithFormat:#"Hello %#", name];
The %# format specifier means "an NSObject reference". For formatting other datatypes - int, double, etc. you should use %d, %f and so on.

Objective C - OSX NSInterger to NSString

Hi i am reading a book by aaron hillegass on cococa programming and I doing one of the mini tasks he asks us to do.
the task is to create an application that has one window open and has 1 input text field, a button and a label.
when a user inputs some text and presses the button, the label displays the text and the length of the text inputted.
Here is what I have got so far
//retrieve text from textfield
NSString *string = [textFieldInput stringValue];
//retrieve length of text and store in NSInteger called length
NSInteger length = [string length];
//store length in string format
NSString *string_length = [NSString stringWithFormat:#"%d", length];
//join strings
NSString *full_string = [string stringByAppendingString:(#"has ",string_length,#" characters")];
//set label text
[textField setStringValue:full_string];
however the actual string is shown and the characters string is shown, just not the string_length. any suggestions and am i going about this in the right way? Thanks.
NSString *fullString = [string stringByAppendingFormat:#"has %# characters", string_length];
//retrieve text from textfield
NSString *string = [textFieldInput stringValue];
NSString *fullString = [string stringByAppendingFormat:#" has %d characters", [string length]];
//set label text
[textField setStringValue:fullString];
Your usage of stringByAppendingString: is wrong.
If I understand you correctly, you are trying to pass a list of strings that should be appended to string but that method only takes a single string argument.
You can try the following:
NSString* fullString = [string stringByAppendingString:[NSString stringWithFormat:#"%# %# %#", #"has ", string_length, #" characters"]];