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];
Related
i'm programming in Obj-c with xcode4.1, i have an array with numbers in it, and i want to visualize all of them in a label...can anyone help me around this please?
thanks!
this is the code:
combinedString=[[NSMutableArray alloc] init];
NSString *finalStringLabel=#"";
for (i=0; i<=textLength; i++) {
//character coding
char myChar = [myString characterAtIndex:i];
NSString *myCharS=[NSString stringWithFormat:#"%c", myChar];
int asciiCode=[myCharS characterAtIndex:0];
NSString *asciiS=[NSString stringWithFormat:#"%i", asciiCode];
[combinedString addObject:asciiS];
}
finalStringLabel=[NSString stringWithFormat:#"", [combinedString componentsJoinedByString:#"."]];
myLabel.text=finalStringLabel;
[combinedString release];
}
You can use this
NSArray *yourArray;
NSString *createdString = [yourArray componentsJoinedByString:#" "];
myLabel.text = createdString;
As your array is combinedString,
combinedString=[[NSMutableArray alloc] init];
looks like you are providing values after this line or this is not a property (this is a local as you are releasing it later), and your code in not complete.
Anyways,
You don't need to create an empty string and then assign new object to it, need to do as :
myLabel.text=[combinedString componentsJoinedByString:#"."];
[combinedString release];
}
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;
I'm splitting a string by ';', but want to specifically grab the first and second element.
I know with PHP it's just simply $array[0], just can't find anything for this for Objective-C
NSArray *tempArray = [returnString componentsSeparatedByString:#";"];
So here I have assigned my array, how can I go about getting the first and second element?
Its just simply [array objectAtIndex:0] in Objective-C ;-)
Starting with XCode 4.5 (and Clang 3.3), you may use Objective-C Literals:
NSString *tmpString1 = tempArray[1];
NSString *tmpString = [tempArray objectAtIndex:0];
NSLog(#"String at index 0 = %#", tmpString);
NSString *tmpString1 = [tempArray objectAtIndex:1];
NSLog(#"String at index 1 = %#", tmpString1);
You may also wish to do an IF statement to check tmpArray actually contains objects in before attempting to grab its value...
e.g.
if ([tempArray count] >= 2) {
// do the above...
}
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];
}
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.