我的分类有一些问题,把NSMutableAttributedString分割成一半--它在NSMakeRange(...)上崩溃了
#import <Foundation/Foundation.h>
@interface NSMutableAttributedString (StringSplit)
- (NSMutableAttributedString *)lastHalfLinesOfAttributedString;
@end
#import "NSAttributedString+StringSplit.h"
@implementation NSMutableAttributedString (StringSplit)
- (NSMutableAttributedString *)lastHalfLinesOfAttributedString
{
NSLog(@"lastHalfLinesOfAttributedString with length:%d from index: %d", [self length], [self length]/2);
NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init];
[result insertAttributedString:[self attributedSubstringFromRange:NSMakeRange([self length]/2, [self length]-1)] atIndex:0];
return result;
}
@endlastHalfLinesOfAttributedString长度:1020从索引: 510 2013-07-02 17:43:16.209 hackers_ssh36675:c07 *终止应用程序由于非正常异常'NSRangeException',原因:'NSConcreteMutableAttributedString attributedSubstringFromRange:越界‘* First
发布于 2013-07-02 15:58:28
NSMakeRange的第二个参数表示length (从第一个参数中的开始索引中计算)。
所以你想要NSMakeRange([self length] / 2, ([self length] + 1) / 2)。
顺便说一句,这种分割字符串的方法只有在字符串中没有组合字符序列或代理项对时才能正确工作。
https://stackoverflow.com/questions/17429996
复制相似问题