我遇到了一些代码问题。我把它缩小到这个问题:首先,reverseString和2都是NSMutableStrings
_input1
和_input2
都是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]:接收器0x100300830,SEL 0x7fff8a689779,isa 0x7fff727b8bd0,缓存0x7fff727b8be0,桶0x7fff89b9b09c,掩码0x1,占用0x0,换行桶0x7fff89b9b09c
objc [2291]:接收器0字节,桶0字节
objc [2291]:选择器&#39;长度&#39;
(lldb)
答案 0 :(得分: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"];
}
答案 1 :(得分: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]);
答案 2 :(得分: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]
答案 3 :(得分:0)
我想出了我的问题,_input1和_input2是坏指针,我不得不修复它,抱歉所有的困惑,最后我的代码工作了!