Help with HelloWorld Objective-C - 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.

Related

XCode - String before array in .detailtextlabel

im assuming this is possible but cant seem to work it out... I have:
cell.detailTextLabel.text = person.age;
in the table view it displays just the age, i want it to display as:
age: 22
rather than just
22
cheers for the help
Objective-C:
cell.detailTextLabel.text = [NSString stringWithFormat:#"age: %i",person.age];
the format specifier %i is for ints, I don't know the type of person.age. If it is a NSNumber, NSString or any other subclass of NSObject you have to use %#.
Just so you know it for the future, in Swift you would make:
Swift:
cell.detailTextLabel.text = "age: \(person.age)"
Much easier right ;-).
There are several ways to do that. For example:
cell.detailTextLabel.text = [#"age: " stringByAppendingString:person.age];
or
cell.detailTextLabel.text = [NSString stringWithFormat:#"age: %#", person.age];
Try this is age property is integer type:
cell.detailTextLabel.text = [NSString stringWithFormat:#"Age: %d", person.age];
if it a NSString type use %# instead of %d.
You should do it like this:
cell.detailTextLabel.text = [NSString stringWithFormat:#"age: %#", person.age];

iOS set UILabel value

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"

Xcode - build string from multiple sources

Hi just starting to learn this language, more of a javascript/PHP guy...
I can't seem to figure out the proper syntax and after searching the internets for a straight answer or explanation I decided to bother you SOF community:
This works as I want it to work:
self.displaysTheStack.text = [self.displaysTheStack.text stringByAppendingString:#" "];
self.displaysTheStack.text = [self.displaysTheStack.text stringByAppendingString:operation];
self.displaysTheStack.text = [self.displaysTheStack.text stringByAppendingString:#" "];
I wanted to know if I could do the same thing in less lines something like:
NSString *displayTheArrayText = [NSString stringWithFormat:#" ",operation,#" "];
self.displaysTheStack.text = [self.displaysTheStack.text stringByAppendingString:displayTheArrayText];
When I do it this way I get the Two #" " (spaces) but "operation" doesn't show up: why and how do I write the latter command properly?
stringWithFormat: uses C-style formats similar to those used by printf
you probably want something like that:
NSString *displayTheArrayText = [NSString stringWithFormat:#" %# ",operation];
have a look at Formatting String Objects
self.displaysTheStack.text = [NSString stringWithFormat:#"%# %# ", self.displaysTheStack.text, operation];
Alternately:
NSMutableString *string = [NSMutableString stringWithString:self.displaysTheStack.text];
[string appendFormat:#" %# ", operation];
self.displaysTheStack.text = string;

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];
}

NSObject to NSString Objective-C

Can someone help me to convert an NSObject to NSString?
I'm trying to do something like this -
NSString *address = [NSString stringWithFormat:ivpObj.addressStr];
But I got an warning - Format is not a string literal and no format arguments
Please some one help
How about this:
NSString *address = [NSString stringWithFormat:#"%#", ivpObj.addressStr];
Simpler still:
NSString *address = [ivpObj.addressStr description];
try this, it is worked for me
NSObject* obj= values[i];
NSString *StringObject= [NSString stringWithFormat:#"%#", obj];
it is giving you a warning because you are useing stringWithFormat and you are not passing in a format you just passing in either an NSString or a CString
so chose from on of the other options
[NSString stringWithString: ivpObj.addressStr]
[NSString stringWithCString: ivpObj.addressStr encoding: String_Encoding of you addressStr here]
[NSString stringWithUTF8String:ivpObj.addressStr]
this should remove your warnings, otherwise if you want to format the string using something similar to Eimantas's answer