Objective-C & string concatenation

I’ve started learning objective-C for my iPhone SDK programming project and was wondering how to concatenate strings and integers, … I saw a number of posts on the web about how verbose Objective-C is compared to C#. No mention was made that C# arrived 20 years after  Objective-C and it was a poorer version of Java. Anyhow, want to concatenate a bunch of primitive types as a string? Here is what you do (recall your sprintf syntax)

[NSString stringWithFormat: @”Hello Aqua %@ %d”, @”cue “, 1];

For string concatenation, Objective-C is almost as concise as Perl -)

4 Responses to “Objective-C & string concatenation”

  1. J Alex Antony Vijay Says:

    NSString *ns, *val;
    ns = “welcome”;
    val = “world”;
    while i using “[NSString stringWithFormat:”%@%@”, ns,val]” this command i am getting bellow error:

    An unhandled win32 exception occurred in “TestProject.exe”

    plz anybody help me, to solve this problem
    i am new for objective-c/cocoa

    • Moses Hall Says:

      ns = @”welcome”;
      val = @”world”;

      To use the %@ formatter the object has to be derived from NSObject. Trying to assign a C string to an NSString will tend strongly to crash. So you use the @ prefix to make an NSString literal. Or you can just use char* strings and use %s in the format. It’s probably more efficient.

  2. Clare Says:

    can I ask how can I concatenate string values of buttons??

  3. programmatore php roma Says:

    programmatore php roma…

    […]Objective-C & string concatenation « n o t e 1 9 . c o m[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: