Subscribe ( )

iPhone Programming Tutorials

 

Forum

You must be logged in to post Login Register

Search 

Strange behavior from NSString : initWithFormat

User Post

2:36 pm
October 27, 2008


Lakario

Noob

posts 3

1

I am trying to create a formatted string to display in a UITableView so I did the following:

NSString *itemText = [[NSString alloc] initWithFormat:@”%s   $%d”, i.itemName, i.itemPrice];
[cell setText:itemText];
[itemText release];

This sets and runs fine, but when I display the output it looks like this:

The expected output would be more like: “Item Name   $5.00″

Any idea what's going on here?

2:47 pm
October 27, 2008


Lakario

Noob

posts 3

2

I switched that %s for a %@ at somebody's recommendation and it produced the desired result, but what exactly is %@? I thought %s was what I needed.

11:38 am
November 18, 2008


VertigoSol

Moderator

posts 26

3

%s == char* type -> c-string
%@ == object type -> NSString is a cocoa object representing a string

%@ in general prints objects and since most of the cocoa types are objects this is the preferred way to print stuff

here is where you would use %s

char[6] myString;
sprintf(myString, “hello”);

NSLog(@”string = %s”, myString);


About the iCodeBlog forum

Currently Online:

5 Guests

Maximum Online: 25

Forums:

Groups: 2

Forums: 6

Topics: 194

Posts: 585

Members:

There are 357 members

There are 1 guests


Brandon has made 87 posts

Top Posters:

bobcubsfan - 52

crazyiez - 30

VertigoSol - 26

AdeC - 17

Uhu - 17

Administrator: Brandon | Moderators: VertigoSol


© - Version 3.1.4 (Build 357)