1

I'm beginning to program iOS Apps and I want to know if its possible save the content of a label in a variable. I got this code:

if (event.subtype == UIEventSubtypeMotionShake) {
int randomNumber = arc4random() %2;
    switch (randomNumber) {
        case 0:
            label.text = [NSString stringWithFormat:@"Out of luck"];
            break;
        case 1:
            label.text = [NSString stringWithFormat:@"Yes, you can"];
            break;
        default:
            break;
     }
}

Then I want to use the label obtained in the shake event to tweet the result like this:

[twitter setInitialText:[NSString stringWithFormat:@"I asked the app and says:", label]];

But as I said before I'm a newby and I'm stuck here. ¿Any ideas?

Thank you in advance.

roymckrank
  • 689
  • 3
  • 12
  • 27
  • If the label is still retained, `@"I asked the app and says: %@", label.text]];` should work. notice the `.text` and the `%@` – mkral Sep 21 '12 at 23:11
  • Also, just so you know for most applications `@"Out of luck"` is the same as `[NSString stringWithFormat:@"Out of luck"];` since you are not formatting the string with other variables. `%@` is for objects (NSString in your case), `%d` is int, `%f` is float. – mkral Sep 21 '12 at 23:15
  • @mkral actually it's not the same. `@"Out of luck"` is a perfectly valid piece of code, while `[NSString stringWithFormat:@"Out of luck"];` is not, as `+stringWithFormat:` expects a format string, not a string literal. – Filip Radelic Sep 21 '12 at 23:49
  • @FilipRadelic As long as I know [NSString stringWithFormat:@"Out of luck"]; it is OK... @"Out of luck" is a correct format just like @"Out of %@", luck.text and no exception is raised. – Giuseppe Garassino Sep 22 '12 at 00:00
  • @FilipRadelic , that's basically what I meant, it's unneeded. Without going into too much detail because this guy is clearly new to obj-c and iOS dev. – mkral Sep 22 '12 at 04:50

2 Answers2

1

Have you tried this?

   [twitter setInitialText:[NSString stringWithFormat:@"I asked the app and says: %@", label.text]];
Peter Zhou
  • 3,881
  • 2
  • 21
  • 19
  • That works perfectly, thank you Peter and also thanks to @mkral, and then, it's better to use @"Out of luck" than NSString stringWithFormat:@"Out of luck"? – roymckrank Sep 24 '12 at 16:16
  • both of them would work. I use NSString stringWithFormat when am creating a string. For example, NSString* myString = [NSString stringWithFormat:@"My name is %@. I am %d",@"Peter",25]; If you print myString, you will get "My name is Peter. I am 25". – Peter Zhou Sep 24 '12 at 16:53
0

Yes, it is possible to save the content of a label in a variable.

Actually, when you write

[NSString stringWithFormat:@"Out of luck"];

you are creating a NSString that can be used later in your application.

Just change it this way:

NSString *aString = [NSString stringWithFormat:@"Out of luck"];

Doing so you put "Out of luck" in aString.

Next step is getting back your string:

NSString *aString = [NSString stringWithFormat:@"Out of luck"];
label.text = aString;

Here you create your string aString and then you put its content "Out of luck" in your label.

You can obtain your goal in many different ways, for example you can write:

NSString *aString = @"Out of luck";

You have to practise variables and declarations... I suggest you to take a look at:

NSString Class Reference

The Objective-C Programming Language

You could also take a look at many posts that can be helpful like this:

Basic objective C variable declaration

EDIT: I just noticed that your question was more about labels than strings, so... If you want to get your label content just write:

[NSString stringWithFormat:@"I asked the app and says: %@", label.text]

as Peter Zhou suggested.

label.text is writable, like in your code:

label.text = [NSString stringWithFormat:@"Out of luck"];

and readable:

NSString *theContentOfALabel = label.text;
Community
  • 1
  • 1
Giuseppe Garassino
  • 2,272
  • 1
  • 27
  • 47