[Solved] objective-c Parameters not passes properly


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!

solved objective-c Parameters not passes properly