0

我遇到了一些代码问题。我把它缩小到这个问题:首先,reverseString 和 2 都是NSMutableStrings _input1and _input2are NSStrings,我试图将零添加到最小的字符串,但它不能正常工作,这就是我得到的。reverseString@"123"和 reverseString2 是@"34567"

 //they get initialized back into the original strings
_input1=reversedString;
_input2=reversedString2;
//appends 0 to the shortest value
while ([_input1 length]>[_input2 length]){
    _input2=[_input2 stringByAppendingString:@"0"];
    _length=[_input1 length];
}
while ([_input1 length]<[_input2 length]){
    _input1=[_input1 stringByAppendingString:@"0"];
    _length=[_input2 length];
}

//converts the string to an NSArray
for (int i=0; i <([_input1 length]); i++) {
    NSString *TempStr = [_input1 substringWithRange:NSMakeRange(i, 1)];
    [one addObject:[TempStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}


for (int i=0; i <([_input2 length]); i++) {
    NSString *TempStr2 = [_input2 substringWithRange:NSMakeRange(i, 1)];
    [two addObject:[TempStr2 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}

现在我注意到,当它通过这个循环时,最小的一个,_input1被设置为,@""而不是在末尾添加零。顺便说一句,这是在一个班级内。

这也是我收到的错误:

objc [2291]:方法缓存损坏。这可能是给无效对象的消息,或者是其他地方的内存错误。objc[2291]: receiver 0x100300830, SEL 0x7fff8a689779, isa 0x7fff727b8bd0, cache 0x7fff727b8be0, buckets 0x7fff89b9b09c, mask 0x1, occupied 0x0, wrap bucket 0x7fff89b9b09c
objc[2291]: receiver 0 bytes, buckets 0 bytes
objc[2291]: selector 'length'
(lldb)

4

4 回答 4

0

只需尝试以下代码

if([_input1 length] > [_input2 length])
{
   for (int i = 0 ; i < [_input1 length] - [_input2 length] ; i ++)
     _input2 = [_input2 stringByAppendingString:@"0"];
}
else
{
   for (int i = 0 ; i < [_input2 length] - [_input1 length] ; i ++)
     _input1 = [_input1 stringByAppendingString:@"0"];
}
于 2013-10-24T05:15:23.907 回答
0

试试这样: -

   NSString *input1=@"123";
   NSString * input2=@"34567";
    NSMutableArray *one=[NSMutableArray array];
        NSMutableArray *two=[NSMutableArray array];

    //appends 0 to the shortest value
    while ([input1 length]>[input2 length]){
        input2=[input2 stringByAppendingString:@"0"];
        //length=[input1 length];
    }
    while ([input1 length]<[input2 length]){
        input1=[input1 stringByAppendingString:@"0"];
       // length=[input2 length];
    }

    for (int i=0; i <([input1 length]); i++) {
        NSString *TempStr = [input1 substringWithRange:NSMakeRange(i, 1)];
        [one addObject:[TempStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    }

            NSLog(@"%ld",[one count]);
    for (int i=0; i <([input2 length]); i++) {
        NSString *TempStr2 = [input2 substringWithRange:NSMakeRange(i, 1)];
        [two addObject:[TempStr2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    }
                    NSLog(@"%ld",[two count]);
于 2013-10-24T05:43:34.097 回答
0

好吧,您的要求不是很清楚,但这是您提出的代码的更简洁版本

NSString *string1 = @"foo";
NSString *string2 = @"foobar";

// Compute the desired length
NSUInteger length = MAX(string1.length, string2.length);
// We will pad using this string
NSString *paddingString = @"0";

// Pad both strings to the same length
string1 = [string1 stringByPaddingToLength:length withString:paddingString startingAtIndex:0];
string2 = [string2 stringByPaddingToLength:length withString:paddingString startingAtIndex:0];

// Build two arrays containing the characters, percent escaped
NSMutableArray *charactersArray1 = [NSMutableArray arrayWithCapacity:string1.length];
NSMutableArray *charactersArray2 = [NSMutableArray arrayWithCapacity:string2.length];
for (NSInteger i = 0; i < string1.length; i++) {
    [charactersArray1 addObject:[[string1 substringWithRange:(NSRange){ i, 1 }] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [charactersArray2 addObject:[[string2 substringWithRange:(NSRange){ i, 1 }] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
NSLog(@"String 1: %@\nString 2: %@", charactersArray1, charactersArray2);

结果将是

字符串 1: [ f, o, o, 0, 0, 0 ]
字符串 2: [ f, o, o, b, a, r ]

于 2013-10-24T18:10:39.517 回答
0

我发现了我的问题,_input1 和 _input2 是错误的指针,我不得不修复它,抱歉所有的混乱,最后我让我的代码工作了!

于 2013-10-29T14:24:59.023 回答