Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you have asked the same question else where. So here is the answer which also applies here.</p> <p>I found a few things that should be changed in your code. Here is what I did to make your swap function work.</p> <p>This is the function:</p> <pre><code>-(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); } </code></pre> <p>This is how the function was called:</p> <pre><code>FooClass *fooObjectVar = [[FooClass alloc] init]; NSMutableString *myString = [[NSMutableString alloc] initWithString:@"Hello"]; [fooObjectVar swapCharacters:myString withInteger:0 andInteger:0]; [fooObjectVar release]; [myString release]; </code></pre> <p>This is the output:</p> <pre><code>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' </code></pre> <p>*Notice that with functions in objective-c, the name is like a description</p> <p>*Instead of using NSInteger, I used a normal int because an NSInteger is not necessary here</p> <p>*When using <strong>NSLog</strong> or string formatting, <strong>%@</strong> is for objects (NSString,NSInteger...), <strong>%i</strong> is for int, <strong>%f</strong> is for float and <strong>%d</strong> is for double</p> <p>I hope that helped, happy coding!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload