NSMutableAttributedString

NSMutableAttributedString

本文介绍了Sublassing NSMutableAttributedString 在初始化时返回 SIGABRT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一个项目中创建了一个 NSMutableAttributedString 的子类来制作一个字符串,该字符串不断将每个字符更改为 init 数组中给定的颜色之一,但是当我尝试调用 init 方法时,我得到一个 sigabrtinitWithString: 方法上.

I created a subclass of NSMutableAttributedString in one of my projects to make a string that continually changes each character to one of the colors given in an array on init, but when I try to call the init method, I get a sigabrt on the initWithString: method.

#import <Foundation/Foundation.h>

@interface RainbowString : NSMutableAttributedString

@property (nonatomic) NSArray* colors;
@property (nonatomic) NSTimeInterval duration;
@property (nonatomic) NSTimer* timer;

- (id)initStringWithColors:(NSArray*)colors withString:(NSString*)string;
- (id)initStringWithColors:(NSArray*)colors withCycleDuration:(NSTimeInterval)duration withString:(NSString*)string;

- (void)stop;
- (void)start:(NSTimeInterval)duration;

@end

initWithColors:

- (id)initStringWithColors:(NSArray *)colors withString:(NSString *)string
{
    self = [super initWithString:string];
    if(self)
    {
        [self setColors:colors];
        [self cycle];
    }

    return self;
}

即使我只是调用 [[RainbowString alloc] initWithString:@"Hello"];,我也会得到同样的错误:

Even if I just call [[RainbowString alloc] initWithString:@"Hello"];, I get the same error:

* 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:-[RainbowString initWithString:]:无法识别的选择器发送到实例 0x166778c0'

更新

好的,为了测试这个,我创建了一个 NSMutableAttributedString 的测试子类,绝对没有内容.我刚刚创建了子类并保持原样.

Update

Okay, so just to test this, I created a test subclass of NSMutableAttributedString with absolutely no content. I just created the subclass and left it plain.

#import <Foundation/Foundation.h>

@interface Test : NSMutableAttributedString

@end

我跑了:

[[NSMutableAttributedString alloc] initWithString:@"Hello"];

运行和编译都很好.但后来我跑了:

That ran and compiled fine. But then I ran:

[[Test alloc] initWithString:@"Hello"];

同样的错误.我不允许子类化 NSMutableAttributedString 之类的东西吗?

Same error. Am I not allowed to subclass NSMutableAttributedString or something?

推荐答案

你的结论是正确的.NS(Mutable)AttributedString 是一个 类簇,并且子类化它们不起作用.可惜 Apple 文档没有明确将其标识为一个.

Your conclusion is correct. NS(Mutable)AttributedString is a class cluster, and subclassing those doesn't work. Pity the Apple documentation doesn't clearly identify it as one.

这篇关于Sublassing NSMutableAttributedString 在初始化时返回 SIGABRT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 01:15