本文介绍了从 NSAttributedString 子类调用超级的 initWithAttributedString: 方法时出现无法识别的选择器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力完成最简单的任务...子类化一个 NSAttributedString.尝试调用超类的 initWithAttributedString: 方法导致无法识别的选择器发送到实例错误.

I'm tearing my hair out trying to do the simplest of tasks... subclassing an NSAttributedString. Trying to call the super class's initWithAttributedString: method is causing an unrecognized selector sent to instance error.

MODAttributedString.h:

MODAttributedString.h:

#import <Foundation/Foundation.h>

@interface MODAttributedString : NSAttributedString

@property (nonatomic, retain) NSDictionary *links;

+ (MODAttributedString*) attributedStringWithFormat:(NSString*)text args:(id)argOne, ... NS_REQUIRES_NIL_TERMINATION;
+ (MODAttributedString*) attributedStringWithFormat:(NSString*)text attributes:(NSDictionary*)attributeDict;

@end

导致崩溃的代码(稍后我将解释我将 alloc 与 init 分开的原因):

The code that is causing the crash (I'll explain the reason I split the alloc from the init in a moment):

MODAttributedString *modString = [MODAttributedString alloc];
// Pausing debugger here and typing 'po modString' causes gdb error
modString = [modString initWithAttributedString:attributedString];

我唯一的线索是跳过 alloc 调用,当我尝试 po modString 时,出现此错误:

My only clue is that stepping over the alloc call, when I try to po modString, I'm given this error:

"正在调试的程序在从 gdb 调用的函数中遇到 ObjC 异常.如果你不希望异常抛出来中断 gdb 调用的函数将 objc-exceptions-interrupt-hand-call-fns 设置为关闭.GDB 已将上下文恢复到调用之前的状态.要更改此行为,请使用set unwindonsignal off"将放弃对包含函数 (_NSPrintForDebugger) 的表达式的评估."

"The program being debugged hit an ObjC exception while in a function called from gdb.If you don't want exception throws to interrupt functions called by gdbset objc-exceptions-interrupt-hand-call-fns to off.GDB has restored the context to what it was before the call.To change this behavior use "set unwindonsignal off"Evaluation of the expression containing the function (_NSPrintForDebugger) will be abandoned."

如果我暂时将 MODAttributedString 的超类更改为 UIView,alloc 不会导致 gdb 错误(我在 init 之前停止调试器,这显然不适用于属性字符串以外的任何其他内容).然而,像 NSArray、NSDictionary 和 NSAttributedString 这样的常见类都失败了,并出现相同的错误.

If I temporarily change the super class of MODAttributedString to a UIView, the alloc does not cause the gdb error (I stop the debugger prior to the init, which would obviously not work for anything other than an attributed string). However, common classes like NSArray, NSDictionary and NSAttributedString all fail with the same error.

在调用 [MODAttributedString alloc] 的方法中,我使用 NSAttributedString 作为它自己的独立类就好了.我确定我也在这个 .m 文件中包含了 MODAttributedString 标头.

In the method that calls the [MODAttributedString alloc] I use NSAttributedString as its own standalone class just fine. I am sure I'm including the MODAttributedString header in this .m file as well.

我使用的是 Xcode 4.2 和 iPhone 5 模拟器.我已经多次清理该项目,尝试创建一个新项目,尝试同时使用 LLVM GCC 4.2 和 Apple LLVM 3.0,重新启动 Xcode 并重新启动我的机器都没有成功.在发布之前,我大量搜索了这个特定问题,但我只发现了与属性相关的问题,从未发现与超类的公共方法相关的问题.

I'm using Xcode 4.2 and the iPhone 5 simulator. I've cleaned the project multiple times, tried creating a new project, tried using both LLVM GCC 4.2 and Apple LLVM 3.0, restarted Xcode and restarted my machine all to no success. I searched for this particular issue heavily before posting, but I only found issues related to properties, never to a superclass's public methods.

这是构建设置问题吗?配置错误?编译器错误?我已经将常见的 Apple 类子类化了数百次,出于某种原因,这是我第一次遇到问题.有没有其他人遇到过类似的问题?这可能是一个非常简单的修复方法,但我似乎无法自己解决.

Is this a build settings issue? A configuration error? A compiler bug? I've subclassed common Apple classes hundreds of times, and for some reason this is the first time I've ever had an issue. Has anyone else ever had a similar problem? It's probably a really simple fix, but I just can't seem to figure it out on my own.

提前致谢!

推荐答案

这不是最简单的事情".您不能继承 NSAttributedString - 它是类集群的一部分.这意味着(除其他外)实例化时返回的类不一定是您要求的类.参见 究竟什么是所谓的类"集群"在 Objective-C 中?

It isn't "the simplest of things". You can't subclass NSAttributedString - it's part of a class cluster. This means (among other things) that the class returned when you instantiate is not necessarily the class you asked for. See What exactly is a so called "Class Cluster" in Objective-C?

在一个类集群中创建子类是可能的,但非常痛苦和困难,但我的建议是编写一个包装类;你会快乐得多.Cocoa 对未处理方法的动态重定向使这变得非常容易.

It is possible, with great pain and difficulty, to subclass within a class cluster, but my advice is to write a wrapper class instead; you'll be much happier. Cocoa's dynamic redirection of unhandled methods makes this very easy.

这篇关于从 NSAttributedString 子类调用超级的 initWithAttributedString: 方法时出现无法识别的选择器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:40