子类化NSMutableAttributedString在init上返回SIGABRT

Dav*_*vid 7 objective-c nsattributedstring ios

我创建NSMutableAttributedString的子类,在我的项目做一个字符串,不断改变每个字符在初始化数组给出的一种颜色之一,但是当我尝试调用init方法,我得到sigabrtinitWithString:方法.

RainbowString.h

#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 
Run Code Online (Sandbox Code Playgroud)

initWithColors:

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

    return self;
}
Run Code Online (Sandbox Code Playgroud)

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

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

更新

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

Test.h

#import <Foundation/Foundation.h>

@interface Test : NSMutableAttributedString

@end
Run Code Online (Sandbox Code Playgroud)

我跑了:

[[NSMutableAttributedString alloc] initWithString:@"Hello"];
Run Code Online (Sandbox Code Playgroud)

那跑得很好.但后来我跑了:

[[Test alloc] initWithString:@"Hello"];
Run Code Online (Sandbox Code Playgroud)

同样的错误.我不允许继承NSMutableAttributedString或其他东西吗?

CRD*_*CRD 6

你的结论是正确的.NS(Mutable)AttributedString是一个类集群,并对它们进行子类化不起作用.遗憾的是,Apple文档并未将其明确标识为一个.