copyWithZone 错误 -[DataObject setECGCount:]: 无法识别的选择器发送到实例
copyWithZone error -[DataObject setECGCount:]: unrecognized selector sent to instance
我的应用程序因以下错误而崩溃:
-[Data Object set ECGCount:]: unrecognized selector sent to instance 0x281671ad0
我正在尝试将我的主 viewController class 之间的数据传递到 DataObject class 中,然后最终将此信息传递到 IntervalGraph class.
我正在使用 copyWithZone 执行此操作,但是卡住了。
这里是 viewContoller.h Class:
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
@interface ViewController : UIViewController<CPTPlotDataSource>
@property (nonatomic, assign) int eCGCount;
@property (nonatomic, assign) int eCGheartRate;
@end
以及viewController.m
中的相关代码
@property (nonatomic) DataObject *maxRecordedHR, *minRecordedHR, *currentData, *continuousData, *seizureData;
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter addObserver:self
selector:@selector(sensorDataUpdated)
name:@"LYRDeviceCommunicatorDataUpdated"
object:nil];
self.rollingAverageHRArray = [[NSMutableArray alloc]initWithCapacity:180];
self.currentData = [[DataObject alloc]init];
self.continuousData = [[DataObject alloc]init];
self.seizureData = [[DataObject alloc]init];
}
//fired by a 1 second timer
-(void)startIntervalGraph {
[self sensorDataUpdated];
if (self.currentData.heartrate == 0 )
{NSLog(@"No data");
} else {
self.rollingAverageLabel.text = [NSString stringWithFormat:@"rolling 3 min average %1.1f", self.currentData.rollingaverageheartrate];
[self.intervalGraph addDataObject:self.currentData]; // something wrong here
[self.intervalGraph updateGraph];
NSLog(@"Updating Interval Graph");
}
}
-(void) sensorDataUpdated {
//Heart Rate
self.currentData.heartrate = self.eCGheartRate;
NSLog(@"Current Data %d", self.currentData.heartrate);
}
-(void)setECGCount:(int)eCGCount
{
self.eCGheartRate = (int) eCGCount;
eCGCount = self.eCGCount;
NSLog(@"eCGCount %d", self.eCGCount);
}
然后DataObject.hclass
#import <Foundation/Foundation.h>
@interface DataObject : NSObject <NSCopying>
@property int heartrate;
@property float rollingaverageheartrate;
@property float rrinterval;
@property NSTimeInterval occurrence;
@property NSInteger minHRsetting;
@property NSInteger maxHRsetting;
@property int alarmtripped;
@end
和DataObject.m
#import "DataObject.h"
#import "CRPC_300SDK.h"
#import "ViewController.h"
@implementation DataObject
- (id)copyWithZone:(NSZone *)zone
{
id copy = class alloc] init];
if (copy) {
// Set primitives
[copy setECGCount:self.heartrate]; //THIS IS WHERE THE CRASH OCCURS.
//[copy setRrinterval:self.rrinterval];
[copy setRollingaverageheartrate:self.rollingaverageheartrate];
[copy setOccurrence:self.occurrence];
[copy setMinHRsetting:self.minHRsetting];
[copy setMaxHRsetting:self.maxHRsetting];
[copy setAlarmtripped:self.alarmtripped];
}
return copy;
}
当我打电话时
[copy setECGCOUNT:self.heartrate];
应用程序崩溃并出现以下错误:
2020-08-20 11:58:57.884543+0100 PulseGuardian[3927:2317144]
-[DataObject setECGCount:]: unrecognized selector sent to instance 0x281671ad0
2020-08-20 11:58:57.885908+0100 PulseGuardian[3927:2317144] *
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[DataObject setECGCount:]:
unrecognized selector sent to instance 0x281671ad0'
我该如何解决这个问题?
正如@Larme 在评论中指出的那样,我在
中引用了错误的方法
DataObject.m
代码如下:
[copy setECGCount:self.heartrate];
什么时候应该读到:
[copy setHeartrate:self.heartrate];
更改代码后,应用程序将正常运行。代表我犯了一个愚蠢的错误,但我会把它留在这里,以防将来它对其他人有帮助。
我的应用程序因以下错误而崩溃:
-[Data Object set ECGCount:]: unrecognized selector sent to instance 0x281671ad0
我正在尝试将我的主 viewController class 之间的数据传递到 DataObject class 中,然后最终将此信息传递到 IntervalGraph class.
我正在使用 copyWithZone 执行此操作,但是卡住了。
这里是 viewContoller.h Class:
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
@interface ViewController : UIViewController<CPTPlotDataSource>
@property (nonatomic, assign) int eCGCount;
@property (nonatomic, assign) int eCGheartRate;
@end
以及viewController.m
中的相关代码@property (nonatomic) DataObject *maxRecordedHR, *minRecordedHR, *currentData, *continuousData, *seizureData;
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter addObserver:self
selector:@selector(sensorDataUpdated)
name:@"LYRDeviceCommunicatorDataUpdated"
object:nil];
self.rollingAverageHRArray = [[NSMutableArray alloc]initWithCapacity:180];
self.currentData = [[DataObject alloc]init];
self.continuousData = [[DataObject alloc]init];
self.seizureData = [[DataObject alloc]init];
}
//fired by a 1 second timer
-(void)startIntervalGraph {
[self sensorDataUpdated];
if (self.currentData.heartrate == 0 )
{NSLog(@"No data");
} else {
self.rollingAverageLabel.text = [NSString stringWithFormat:@"rolling 3 min average %1.1f", self.currentData.rollingaverageheartrate];
[self.intervalGraph addDataObject:self.currentData]; // something wrong here
[self.intervalGraph updateGraph];
NSLog(@"Updating Interval Graph");
}
}
-(void) sensorDataUpdated {
//Heart Rate
self.currentData.heartrate = self.eCGheartRate;
NSLog(@"Current Data %d", self.currentData.heartrate);
}
-(void)setECGCount:(int)eCGCount
{
self.eCGheartRate = (int) eCGCount;
eCGCount = self.eCGCount;
NSLog(@"eCGCount %d", self.eCGCount);
}
然后DataObject.hclass
#import <Foundation/Foundation.h>
@interface DataObject : NSObject <NSCopying>
@property int heartrate;
@property float rollingaverageheartrate;
@property float rrinterval;
@property NSTimeInterval occurrence;
@property NSInteger minHRsetting;
@property NSInteger maxHRsetting;
@property int alarmtripped;
@end
和DataObject.m
#import "DataObject.h"
#import "CRPC_300SDK.h"
#import "ViewController.h"
@implementation DataObject
- (id)copyWithZone:(NSZone *)zone
{
id copy = class alloc] init];
if (copy) {
// Set primitives
[copy setECGCount:self.heartrate]; //THIS IS WHERE THE CRASH OCCURS.
//[copy setRrinterval:self.rrinterval];
[copy setRollingaverageheartrate:self.rollingaverageheartrate];
[copy setOccurrence:self.occurrence];
[copy setMinHRsetting:self.minHRsetting];
[copy setMaxHRsetting:self.maxHRsetting];
[copy setAlarmtripped:self.alarmtripped];
}
return copy;
}
当我打电话时
[copy setECGCOUNT:self.heartrate];
应用程序崩溃并出现以下错误:
2020-08-20 11:58:57.884543+0100 PulseGuardian[3927:2317144] -[DataObject setECGCount:]: unrecognized selector sent to instance 0x281671ad0
2020-08-20 11:58:57.885908+0100 PulseGuardian[3927:2317144] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DataObject setECGCount:]: unrecognized selector sent to instance 0x281671ad0'
我该如何解决这个问题?
正如@Larme 在评论中指出的那样,我在
中引用了错误的方法DataObject.m
代码如下:
[copy setECGCount:self.heartrate];
什么时候应该读到:
[copy setHeartrate:self.heartrate];
更改代码后,应用程序将正常运行。代表我犯了一个愚蠢的错误,但我会把它留在这里,以防将来它对其他人有帮助。