微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 子类化NSMutableAttributedString在init上返回SIGABRT

我在我的一个项目中创建了一个NSMutableAttributedString的子类,以创建一个字符串,该字符串不断地将每个字符更改为init中数组中给出的颜色之一,但是当我尝试调用init方法时,我在initWithString上获得了一个sigabrt:方法.

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

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”];,我得到同样的错误

* Terminating app due to uncaught exception ‘NSinvalidargumentexception’,reason: ‘-[RainbowString initWithString:]: unrecognized selector sent to instance 0x166778c0’

更新

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

Test.h

#import <Foundation/Foundation.h>

@interface Test : NSMutableAttributedString

@end

我跑了:

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

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

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

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

解决方法

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

原文地址:https://www.jb51.cc/iOS/328371.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐