0

I'm quite new to Xcode and have a quite amateur question still, it's quite relevant. I come from VB.NET and if I want to print mylabel + mylabel(x10) i'd use the following code:

for(i=0,i<=10,i++) {
    mylabel = i;
    mylabel &= mylabel;
}

I'd like to do this for xcode as well...

what I currently have will overwrite the string instead of adding it:

for (int i=0; i<=10; i++) {
    NSMutableString *lol =
        [[NSMutableString alloc]initWithFormat:@" Getal: %i \n",i];
    [myLabel setStringValue:lol];
}
6
  • 2
    Obviously you're quite new, since no one has taught you yet that Xcode is the development system and Objective-C is the language you (usually) write in. Commented Jul 16, 2013 at 0:24
  • Use %d, not %i. And generally you don't use a \n in label text. And there's no need to create a mutable string if you're not going to alter it. And if myLabel is a UILabel, you set it's text with setText:. Commented Jul 16, 2013 at 0:28
  • @HotLicks %d and %i are the same when used with string formats. Commented Jul 16, 2013 at 1:18
  • @rmaddy - I forget the scenario, but there are a few cases where &i doesn't work. Commented Jul 16, 2013 at 1:39
  • @HotLicks Thank you, but i'm trying your idea's and I as I must say it's a IBOutlet in Cocoa so I don't think: [myLabel setText:@...] will work. Thanks for the tips i'll keep them in mind! PS: what's the alternative for the \n in the string? Commented Jul 16, 2013 at 12:06

3 Answers 3

2

If your intent is to create a string with 10 copies of "Getal: #" in it, on separate rows, you'd use something like this:

NSMutableString* result = [NSMutableString stringWithCapacity:150];
for (int i = 0; i < 10; i++) {
    [result appendFormat:@"Getal: %d\n", i];
}
Sign up to request clarification or add additional context in comments.

Comments

1

Looks like you're trying to build a list of the indices? Try this:

NSMutableString *accumulator = [NSMutableString string];
for (int i = 0; i <= 10; i++) {
    [accumulator appendString:[NSString stringWithFormat:@"%d", i]];
}
myLabel.text = accumulator;

If that's not exactly what you want, perhaps it'll get you started. Or, if you could give an example of the output you're looking for, someone might be nice enough to edit this.

1 Comment

Minor mistake - declare accumulator as an NSNMutableString instead of NSString.
-1

Thank you, now I understand what the append is for and how to use it in the right way!

What I came up with was the following:

  [myLabel setStringValue:@""];
  for (int i=0; i<=10; i++) {
    NSMutableString *lol = [[NSMutableString alloc]initWithFormat:@"%@ Getal: %i \n",[myLabel stringValue],i];
    [myLabel setStringValue:lol];

3 Comments

What sort of object is "myLabel"?? And is there any reason why you didn't want to append to the mutable string vs using repeated string recreates (where the mutable string is unnecessary)?
Well it's an IBOutlet. The thing is, I didn't know how to "append". I'm really new to the language and trying to find my programming basics around objective C, since I come from Vb.net it's quite a shock, héhé
No, it's not an IBOutlet. "IBOutlet" is an annotation placed on properties to let the various parts of the development system know that these properties are to be connected to elements of the UI. Directly to the right of "IBOutlet" you will see the type of the variable. Eg: IBOutlet UILabel* myLabel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.