Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

ios - objective-c Parameters not passes properly

I have the following code:

[self swap:set :begin :begin+i];  //swap

calling the following swap method:

-(void) swap:(NSMutableString *)set:(NSInteger)first:(NSInteger)second 
{
   NSRange rangeSecond = NSMakeRange((NSUInteger) second, 1);
   NSRange rangeFirst = NSMakeRange((NSUInteger) first, 1);

   NSString *chsecond =  [set substringWithRange:rangeSecond ];

   [set replaceCharactersInRange:rangeSecond 
                      withString:[set substringWithRange:rangeFirst]];
   [set replaceCharactersInRange:rangeFirst 
                      withString:chsecond];
}

The very first time this swap routine is called, it crashes at the statement:

[set replaceCharactersInRange:rangeSecond 
                   withString:[set substringWithRange:rangeFirst]];

without leaving any debug information. Before calling swap I can hover over set, begin and i and they all have valid values. After swap is called when I hover over "first" it says (data variable, no debug info)first (unknown type)

This program was working fine and this happened suddenly. Any idea why this is happening? Thanks

Here is the whole chain of calls. start StartPermutate calls permute which calls swap. Is there any problems in the way I pass the parameters?

-(void) permute: (NSMutableString *) set :(NSInteger ) begin  :(NSInteger ) end {
int i;
int range = end - begin;
if(range == 1) {
    if([self is_word_valid:set]) {
        [self addWord:selectedWords : set];
    }
 } else {
    for(i=0; i<range; i++) {
        NSLog(@"set = '%@', first = '%@', second = '%@'", set, begin, begin + i);
        [self swap:set :begin :begin+i];  //swap
        [self permute:set :begin+1 :end]; // recursion
        [self swap:set :begin :begin+i];  // swap back
    }
 }
}    

-(NSMutableArray *)StartPermutate: (NSMutableString *) letters {

 [self permute:letters :0 :(NSInteger)[letters length]];
 return selectedWords;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you have asked the same question else where. So here is the answer which also applies here.

I found a few things that should be changed in your code. Here is what I did to make your swap function work.

This is the function:

-(void) swapCharacters: (NSMutableString *)set withInteger: (int)first andInteger: (int)second{

NSLog(@"swap: set = '%@', first = '%i', second = '%i'", set, first, second);
NSRange rangeSecond = NSMakeRange(second, 1);
NSRange rangeFirst = NSMakeRange(first, 1);

[set replaceCharactersInRange:rangeSecond withString:[set substringWithRange:rangeFirst]];
NSLog(@"swap: set = '%@', first = '%i', second = '%i'", set, first, second);
}

This is how the function was called:

FooClass *fooObjectVar = [[FooClass alloc] init];

NSMutableString *myString = [[NSMutableString alloc] initWithString:@"Hello"];

[fooObjectVar swapCharacters:myString withInteger:0 andInteger:0];

[fooObjectVar release];
[myString release];

This is the output:

2011-12-30 14:19:00.501 StackOverflowHelp[748:903] swap: set = 'Hello', first = '0', second = '0'
2011-12-30 14:19:00.504 StackOverflowHelp[748:903] swap: set = 'Hello', first = '0', second = '0'

*Notice that with functions in objective-c, the name is like a description

*Instead of using NSInteger, I used a normal int because an NSInteger is not necessary here

*When using NSLog or string formatting, %@ is for objects (NSString,NSInteger...), %i is for int, %f is for float and %d is for double

I hope that helped, happy coding!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...