2013-10-24 141 views
0

我遇到了一些代碼的麻煩。我縮小到這個問題:首先,reverseString和2都是NSMutableStrings_input1_input2NSStrings,我試圖將零添加到最小的字符串,但它不能正常工作,這就是我得到的。 reverseString@"123"和reverseString2是@"34567"將零追加到NSString的末尾?

//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] :選擇 '長度'
(LLDB)

+0

但你的代碼工作正常,問題在哪裏? –

+0

所以你的意思是說在你的兩個數組中它顯示的是零對象嗎? –

+0

你的代碼對我來說工作正常。 –

回答

0

想出我的問題,_input1和_input2是壞的指針和我不得不修復我t,對於所有的困惑抱歉,最後我得到了我的代碼工作!

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"]; 
} 
+0

它需要繼續追加,直到有兩個相同的長度 – OPJ

+0

oky。我會改變... – iPatel

+0

@OPJ - 檢查我編輯的答案:) – iPatel

0

嘗試這樣的嘗試: -

NSString *[email protected]"123"; 
    NSString * [email protected]"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]); 
+0

你可以添加一些評論? – OPJ

+0

我不知道你爲什麼得到@「」但對我來說它工作正常。現在在上面的代碼中,我已經將一個API替換爲stringByAddingPercentEscapesUsingEncoding。請試試相同的? –

+0

我會稍微看一下,我正在升級我的xcode,也許這個代碼是問題 – OPJ

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]