我正在尝试自定义一个 NSObject
来接受一个附加参数,但是每当我尝试添加它时,我都会得到:
NSInvalidArgumentException: -[TEATimeRange initWithStart:end:fill:]: unrecognized selector sent to instance`
这是 .h
文件:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface TEATimeRange : NSObject
@property (nonatomic) NSDate *start;
@property (nonatomic) NSDate *end;
@property (nonatomic) IBInspectable UIColor *fill;
+ (instancetype)timeRangeWithStart:(NSDate *)startTime end:(NSDate *)endTime fill:(UIColor *)fillColor;
- (id)initWithStart:(NSDate *)startTime end:(NSDate *)endTime fill:(UIColor *)fillColor;
@end
和 .m
文件:
#import "TEATimeRange.h"
@implementation TEATimeRange
+ (instancetype)timeRangeWithStart:(NSDate *)startTime end:(NSDate *)endTime fill:(UIColor *)fillColor
{
return [[TEATimeRange alloc] initWithStart:startTime end:endTime fill:fillColor];
}
- (id)initWithStart:(NSDate *)startTime end:(NSDate *)endTime fill:(UIColor *)fillColor
{
self = [super init];
if (self) {
_start = startTime;
_end = endTime;
_fill = fillColor;
}
return self;
}
@end
调用实例(在 Swift 中):
TEATimeRange(start: someDateTime, end: someDateTime?.addingTimeInterval(TimeInterval(someTime)), fill: .orange)
如果我删除对 fill:
的引用,它会按预期运行,所以我很困惑为什么。
我已经尝试过 SO 搜索,但由于我只有 3 天的 iOS 开发经验,我还没有找到任何解决方案,因此非常感谢任何帮助。 (即使这是一件愚蠢的事情!)
最佳答案
对于访问此页面的 future 开发者,我的问题已通过以下方式解决:
- 清理构建文件夹(产品 > 清理构建文件夹或 ⇧ ⌘ K)
- 重新启动 Xcode
- 重新运行应用
如果这不起作用,请参阅上面@OOPer 和@rmaddy 的评论以获得进一步的指导
关于ios - 更改 NSObject 定义导致无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55698718/