NSString ReplaceCharactersInRange 崩溃

发布于 2024-12-23 10:27:17 字数 546 浏览 3 评论 0原文

我有以下代码:

-(void) swapCharacters: (NSMutableString *) set : (NSInteger) first : (NSInteger) second    {

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

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

NSLog 打印为:set = 'happy', first = '0', secondary = '0' 但程序在 [set rangeFirst = ....] 处崩溃,没有任何调试信息。

感谢您的任何帮助。

I have the following code:

-(void) swapCharacters: (NSMutableString *) set : (NSInteger) first : (NSInteger) second    {

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

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

NSLog print is : set = 'happy', first = '0', second = '0'
But the program crashes at [set rangeFirst = ...........] without any debug info.

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思故 2024-12-30 10:27:17

我发现您的代码中应该更改一些内容。这是我为使您的交换功能发挥作用所做的工作。

这是函数:

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

这是函数的调用方式:

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

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

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

[fooObjectVar release];
[myString release];

这是输出:

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'

*注意,对于 Objective-C 中的函数,名称就像描述

*我没有使用 NSInteger,而是使用普通 int,因为 NSInteger 不是这里必需

*当使用 NSLog 或字符串格式时,%@ 用于对象(NSString,NSInteger...),%i 用于 int , %f 代表 float,%d 代表 double

我希望有所帮助,编码愉快!

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!

望她远 2024-12-30 10:27:17

我只是尝试你的代码,它像这样为我工作

-(void) swapCharacters: (NSMutableString *) set first: (NSInteger) first second: (NSInteger) second    {

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

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

,我这样称呼它

NSMutableString *s = [NSMutableString stringWithString:@"Hello"];
[self swapCharacters:s first:0 second:3];
NSLog(@"s == %@", s);

,这是我得到的 NSLog :

2011-12-30 15:13:48.822 tabbar[908:207] 交换:设置 = 'Hello',第一个 = '0',第二个 = '3'
2011-12-30 15:13:48.823 tabbar[908:207] s == HelHo


您在调用该方法时是否将 NSMutableString 或 NSString 传递给该方法?
传递 NSString 这是我能够使代码崩溃的唯一方法。

I just try your code and it's working for me like this

-(void) swapCharacters: (NSMutableString *) set first: (NSInteger) first second: (NSInteger) second    {

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

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

And I'm calling it like this

NSMutableString *s = [NSMutableString stringWithString:@"Hello"];
[self swapCharacters:s first:0 second:3];
NSLog(@"s == %@", s);

And this is the NSLog I get :

2011-12-30 15:13:48.822 tabbar[908:207] swap: set = 'Hello', first = '0', second = '3'
2011-12-30 15:13:48.823 tabbar[908:207] s == HelHo


Are you passing a NSMutableString or a NSString to that method when you are calling it?
Passing a NSString it's the only way I'm able to make that code crash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文