Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在xcode中使用数组反转字符串_Objective C_Arrays_String_Stack - Fatal编程技术网

Objective c 在xcode中使用数组反转字符串

Objective c 在xcode中使用数组反转字符串,objective-c,arrays,string,stack,Objective C,Arrays,String,Stack,这是我在stackoverflow中的第一个问题! 我试图在xcode中使用两个数组反转字符串,我设置了界面,得到了一个按钮、一个文本字段和一个标签。 触摸按钮时,进入文本字段的内容都会反转! 我得到了代码,在纸面上对我来说似乎是正确的,问题是当我用“HELLO”测试应用程序时,myArray的内容是“he”,reverseArray是“O L”。 如果有人提供帮助,我将不胜感激。我已经厌倦了跟踪此代码:((( 代码如下: @interface ViewController () @end

这是我在stackoverflow中的第一个问题! 我试图在xcode中使用两个数组反转字符串,我设置了界面,得到了一个按钮、一个文本字段和一个标签。 触摸按钮时,进入文本字段的内容都会反转! 我得到了代码,在纸面上对我来说似乎是正确的,问题是当我用“HELLO”测试应用程序时,myArray的内容是“he”,reverseArray是“O L”。 如果有人提供帮助,我将不胜感激。我已经厌倦了跟踪此代码:((( 代码如下:

@interface ViewController ()
@end


@implementation ViewController
@synthesize textField,string1,string2,reverseArray,myArray;
@synthesize Label1;
- (IBAction)Reverse:(UIButton *)sender {
reverseArray=[[NSMutableArray alloc]init];
string1=[[NSString alloc]init];
string2=[[NSString alloc]init];
string1=textField.text;
myArray=[[NSMutableArray alloc]init];
    for (int i=0; i<=string1.length-1; i++) {
    [myArray insertObject:[[NSString alloc] initWithFormat:@"%c",[string1 characterAtIndex:i]] atIndex:i];

}
    for (int j=0; j<=myArray.count-1; j++) {
    [reverseArray insertObject:[myArray objectAtIndex:myArray.count-1] atIndex:j];
    [myArray removeLastObject];


}
NSLog(@"%@",myArray);
NSLog(@"%@",reverseArray);
@界面视图控制器()
@结束
@实现视图控制器
@合成textField、string1、string2、reverseArray、myArray;
@合成Label1;
-(iAction)反向:(UIButton*)发送器{
reverseArray=[[NSMutableArray alloc]init];
string1=[[NSString alloc]init];
string2=[[NSString alloc]init];
string1=textField.text;
myArray=[[NSMutableArray alloc]init];

对于(int i=0;i对于第二个循环,您使用myArray.count作为“for”循环的结束条件,但是myArray.count在循环的每个迭代中都会减少一个,因为您每次迭代都会从myArray中删除最后一个对象。请考虑一下:

First iteration:  j=0; myArray.count - 1 = 4
Second iteration: j=1; myArray.count - 1 = 3
Third iteration:  j=2; myArray.count - 1 = 2
它在第四次迭代时停止,因为j=3>myArray.count-1=1

在第二个循环中尝试类似的方法(注意:我现在不在xCode前面,所以下面的代码块中可能会有错误。请恕我直言):

for( int j = string1.length -1; j >= 0; j-- ) {
    [reverseArray addObject:[myArray objectAtIndex:j]];
}