2

Please bear with me as I'm very new to the world of iOS and Objective-C. I've read Apple's Obj-C primer, as well as a few free ones provided on the web.

On a button press, I'm trying to simply take the text of a label and concatenate it with a string. My mindset is still very much in Android/Java and how simple it could be, but I'm having trouble here. Nonetheless here is my code:

- (IBAction)myButton:(UIButton *)sender {
    self.myLabel.text = [self.myLabel.text stringByAppendingString:@"obj-c is hard =/"];

}

It seems pretty standard, but I can imagine myself doing this often so I want to make sure this is correct or what other ways are there to do this?

EGHDK
  • 17,818
  • 45
  • 129
  • 204

4 Answers4

2

Yes this is correct way. And if you want to use another method then use this one

self.myLabel.text = [NSString stringWithFormat:@"%@ obj-c is hard =/",self.myLabel.text];
John Topley
  • 113,588
  • 46
  • 195
  • 237
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • `stringWithFormat` will allow you to pass multiple arguments? Unlike `stringByAppendingString` correct? – EGHDK Jun 06 '13 at 05:11
0

It is the standard way to join string.As ios updated syntaxes to make it easy like NSArray and NSDictiornary delaration but for concatenation it has not declared any shortcut way.

Have a look at this

OR

you can use a trick to simplify concatenation of string.Pass a parameter to macro and use following joining literal syntax.

#define STRING(text) @""text""

@implementation SPTViewController

- (void)viewDidLoad
{
    NSString *joinedFromLiterals =STRING(@"Congratulations!, ") @"I " @"really " @"enjoy " @"carpenting!";
    NSLog(@"joined string %@",joinedFromLiterals);

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

output is ---------

joined string Congratulations!, I really enjoy carpenting!

Community
  • 1
  • 1
Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90
0

Yes, this is correct, but there is a gotcha. If you haven't previously set the value of self.myLabel.text, it will be nil by default. Then the result of calling any method (like [self.myLabel.text stringByAppendingString:@"obj-c is hard =/"]) will also be nil, so myLabel will still have empty text. The way Objective-C handlesnil values is different than handling null in Java.
So to be safe, initialize label's text first:

self.myLabel.text = @"";
Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
0

You are doing it right. Sure Objective-C is a bit more verbose than C# or Java or even Visual Basic .net (as I used to work on all those languages) but don't be bugged by those long method names. Although some #defines can be very helpful like (rewritten as C inline function):

static inline __attribute__((always_inline))
__attribute__((format(NSStirng, 1, 2)) NSString *SKSTR(NSString *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    NSString *string = [[NSString alloc] initWithFormat:fmt arguments:args];
    va_end(args);
#if !__has_feature(objc_arc)
    [string autorelease];
#endif
    return string;
}

Hope the __attribute__s and #ifs does not confuse you - you can safely ignore them.

To use:

self.label.text = SKSTR(@"%@, ugh!", self.label.text); // just like NSLog or snprintf :)
Maxthon Chan
  • 1,181
  • 8
  • 15