我该如何在Objective-C中拼接字符串?

3

我正在尝试将一系列输入字符串连接成一个字符串,以便将其保存到文本文件中。目前,我正在尝试编写以下内容:

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

这样的唯一问题是我需要以这种方式存储总共30个字符串。我需要一种无需手动输入每个字符串名称的方法。是否有一种使用for循环或其它方法来完成此操作的方式?像这样输入字符串可能可以:
text(i)

因此,变量名称每次通过for循环都会更改。我尝试做了这样的事情,但无法使其工作。如果您能帮助我使用此方法或其他您知道的方法,我将非常感激。


1
基本上,您需要将每个文本框或字符串放入数组中,以利用循环或类似于Alex下面回答的便捷方法。 - Joe
3个回答

3
好的,所有这里的答案都采取了错误的方法(对不起,各位)。
根本问题在于你正在使用“文本框”作为数据源,而它们应该只是视图。当有人更改文本时,您应该立即将其存储在您的模型中(可以是简单的数组),然后稍后引用该模型。(这是MVC的一部分。如果您不熟悉,请查阅相关资料,如果您正在为iOS编程,则应该熟悉!)
这是我会做的事情。(我假设你的“文本框”是UITextField。)
  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:@""];
    
请注意,我并未测试过这些内容,所以可能会有笔误。但是,这应该可以将您引导到正确的方向!

2
这里有一些拼写错误,但是这似乎是唯一一个处理用户以任意顺序填写UITextFields的可能情况的答案。当我坐在电脑前时会进行编辑来修复拼写错误。+1 - geraldWilliam

1
如果您在Interface Builder中使用IBOutletCollection(UITextField)设置文本框,您将拥有一个文本框数组,可以使用KVC访问文本值并连接它们。
//interface
...
@property (nonatomic, strong) IBOutletCollection(UITextField) NSArray *textBoxes;


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

iOS 4中的IBOutletCollection使用方法

如果您以编程方式创建文本框,则在创建它们时将它们添加到数组中。


这绝对是我正在寻找的。有没有办法对数组中的文本框进行排序?因为据我所知,它相当随机。 - lfitzgibbons
如果 IBOutletCollection 出现了随机顺序,那么你可以将其声明为 NSMutableArray 并在 viewDidLoad 中根据你的标准(例如 tag排序 数组。 - Joe

0

NSMutableStringappendString:方法是你的好朋友。

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

2
或者 [strings componentsJoinedByString:@""] - Joe
那么,我仍然需要在数组中键入每个文本框名称,这正是我试图避免的,因为有30个。如果不全部键入,这是否不可能实现? - lfitzgibbons
使用NSMutableArray并使用数组的addObject:方法将字符串从您的界面添加到数组中。一旦您将所有字符串添加到数组中,可以使用for循环遍历它,并根据Alex上面的代码使用NSMutableStringappend方法。 - davidf2281

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接