Pregunta

I am having trouble with some code. I narrowed it down to this problem: first of all, reverseString and 2 are both NSMutableStrings _input1 and _input2 are NSStrings, i'm trying to add zeros to the smallest string but it's not working correctly, this is what I got. reverseString is @"123" and reverseString2 is @"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]];
}

Now I noticed that when it goes through this loop, the smallest one, _input1, gets set to @"" instead of adding zeros to the end. This is within a class, by the way.

This is also an error I receive:

objc[2291]: Method cache corrupted. This may be a message to an invalid object, or a memory error somewhere else. 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)

¿Fue útil?

Solución 4

I figured out my problem, _input1 and _input2 were bad pointers and i had to fix it, sorry for all the confusion, in the end i got my code to work!

Otros consejos

Just try with following code

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

Try like this:-

   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]);

Well your requirements are not very clear, but here's a cleaner version of the code you proposed

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);

The result will be

String 1: [ f, o, o, 0, 0, 0 ]
String 2: [ f, o, o, b, a, r ]

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top