3

So I am trying to concatenate a bunch of input strings together as one string so I can save that to a text file. So far I am trying to write something like this

NSString *tempString = [[NSString alloc]initWithFormat:@"%@%@%@", text1, text2, text3];

The only problem with this is that I need a total of 30 strings stored this way. I need a way to do this without typing out each string name. Is there a way to use a for loop or something to accomplish this? Type the strings like this perhaps?

text(i)

So that the variable name would change each time it went through the for loop. I've tried doing something like this and I can't get it to work. If you can help me with this method or another way that you know to do it I would be very thankful.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
lfitzgibbons
  • 783
  • 1
  • 5
  • 18
  • 1
    Essentially you need to have each textbox or string in an array to take advantage of a loop or convenience method similar to Alex's answer below. – Joe May 07 '13 at 23:52

3 Answers3

3

Okay, so all of the answers here take the wrong approach (sorry guys).

The fundamental problem is that you are using your "text boxes" as a data source, when they should simply be views. When someone changes the text, you should immediately store them in your model (which could be a simple array) and then reference that model later. (This is part of MVC. Look it up if you aren't familiar, as you should be if you are programming for iOS!)

Here is what I would do. (I'm assuming that your "text boxes" are UITextField's.)

  1. Set the delegate for each text field to your view controller.
  2. Set the tag for each text field to a number which represents the order that you want the strings joined in. (ie 1-30)

  3. If you don't have a separate class for your data model, then setup a declared property in your view controller which stores a reference to a NSMutableArray which can contain all of the strings in order. Let's call it dataSource. In viewDidLoad: set this to an actual mutable array filled with empty values (or previously stored values if you are saving them). The reason that we store empty values is so that we can replace them with the user entered strings for any index, even if they are entered out of order:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.dataSource = [[NSMutableArray alloc] initWithCapacity:20];
        for(int i = 0; i < 30; i++)
            [self.dataSource addObject:@""];
    }
    
  4. Then, use the following text field delegate method which stores the strings into the array as they are entered:

    // This is called every time that a text field finishes editing.
    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
        if (textField.tag > 0)
            [self.dataSource replaceObjectAtIndex:textField.tag-1 withObject:textField.text];
    }
    
  5. Congratulations! All of your strings are now stored in one array. Now we just have to combine them all:

    NSMutableString *theString = [self.dataSource componentsJoinedByString:@""];
    

Note that I have NOT tested all of this so there may be typos. This should get you pointed in the right direction though!

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • 2
    There are typos but this does seem to be the only answer that deals with the possible case of the user filling ou UITextFields in an arbitrary order. Will edit to fix typos when I am in front of my computer. +1 – geraldWilliam May 15 '13 at 02:28
1

If you set up your text boxes in Interface Builder with an IBOutletCollection(UITextField) you would have an array of text boxes that you could access the text value using KVC and join them.

//interface
...
@property (nonatomic, strong) IBOutletCollection(UITextField) NSArray *textBoxes;


//implementation
...
    NSString *tempString = [[textBoxes valueForKey:@"text"] 
                            componentsJoinedByString:@""];

Using iOS 4's IBOutletCollection

If you programmatically create your text boxes then add them to an array as you create them.

Joe
  • 56,979
  • 9
  • 128
  • 135
  • That is definitely what I am looking for. Is there a way to sort the text boxes in the array? Because as far as I can tell it is pretty random. – lfitzgibbons May 08 '13 at 00:30
  • If it appears the `IBOutletCollection` is in random order then you could declare it as an `NSMutableArray` and on `viewDidLoad` [sort](https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/SortDescriptors/Articles/Creating.html) the array based on your criteria (i.e. `tag`). – Joe May 08 '13 at 00:33
0

NSMutableString's appendString: method is your friend.

NSArray *strings = [NSArray arrayWithObjects: @"Hi", @" there", @" dude", nil];

NSMutableString *result = [[NSMutableString alloc] init];

for (NSString *string in strings) {
  [result appendString:string];
}

NSLog(@"result: %@", result); // result: Hi there dude
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 2
    Or `[strings componentsJoinedByString:@""]` – Joe May 07 '13 at 23:50
  • So with this I would still have to type out each text box name in the array which is what I was trying to get away from considering there are 30. Is this just not possible to do without typing them all out? – lfitzgibbons May 07 '13 at 23:56
  • Use an `NSMutableArray` and add the strings from your interface with the array's `addObject:` method. Once you have all the strings in the array, iterate through it with a `for` loop using `NSMutableString`'s `append` as per Alex's code above. – davidf2281 May 08 '13 at 00:10