首页 > 解决方案 > 更改 NSObject 定义导致无法识别的选择器

问题描述

我正在尝试自定义一个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 开发经验,我无法找到任何解决方案,因此将不胜感激。(即使这是一件愚蠢的事情!)

标签: iosobjective-cswiftnsobject

解决方案


对于访问此页面的未来开发人员,我的问题已通过以下方式解决:

  1. 清理构建文件夹(产品 > 清理构建文件夹或⇧</kbd> ⌘</kbd> K)
  2. 重启 Xcode
  3. 重新运行应用程序

如果这不起作用,请参阅上面来自 @OOPer 和 @rmaddy 的评论以获得进一步的指导


推荐阅读