我尝试使用一对多关系将数据插入实体。我的实体名称为Student
和Detail
。 Student
与Detail
有多个关系,Detail
到student
有一种关系。关系名称为detailRelationship
,从Student
到Detail
& studentRelationship
从Detail
到Student
。
StudentCoreDataclassProperties
#import "Student+CoreDataClass.h"
NS_ASSUME_NONNULL_BEGIN
@interface Student (CoreDataProperties)
+ (NSFetchRequest<Student *> *)fetchRequest;
@property (nullable, nonatomic, copy) NSString *number;
@property (nullable, nonatomic, retain) NSSet<Detail *> *detailRelationship;
@end
@interface Student (CoreDataGeneratedAccessors)
- (void)addStudentRelationshipObject:(Detail *)value;
- (void)removeStudentRelationshipObject:(Detail *)value;
- (void)addStudentRelationship:(NSSet<Detail *> *)values;
- (void)removeStudentRelationship:(NSSet<Detail *> *)values;
@end
DetailCoreDataclassProperties
#import "Detail+CoreDataClass.h"
NS_ASSUME_NONNULL_BEGIN
@interface Detail (CoreDataProperties)
+ (NSFetchRequest<Detail *> *)fetchRequest;
@property (nullable, nonatomic, copy) NSString *name;
@property (nullable, nonatomic, copy) NSString *address;
@property (nullable, nonatomic, retain) Student *studentRelationship;
//@property (nullable, nonatomic, retain) NSSet<Detail *> *studentRelationship;
@end
@interface Detail (CoreDataGeneratedAccessors)
- (void)addDetailRelationshipObject:(Detail *)value;
- (void)removeDetailRelationshipObject:(Detail *)value;
- (void)addDetailRelationship:(NSSet<Detail *> *)values;
- (void)removeDetailRelationship:(NSSet<Detail *> *)values;
@end
插入数据的代码
- (IBAction)submit:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
_mainContext = [appDelegate manageObjectContext];
Detail *detailtEntity = (Detail *)[NSEntityDescription insertNewObjectForEntityForName:@"Detail" inManagedObjectContext:_mainContext];
_studentClass = (Student *)[NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:_mainContext];
detailtEntity.name = _nameText.text;
detailtEntity.address = _addressText.text;
detailtEntity.address = _addressText1.text;
detailtEntity.name = _nameText1.text;
_studentClass.number = _numberText.text;
NSMutableSet *mySet = [[NSMutableSet alloc] init];
[mySet addObject: _studentClass];
[detailtEntity addDetailRelationship:mySet];//crash app here
detailtEntity.studentRelationship = _studentClass;
NSError *error = nil;
[_mainContext save:&error];
if (![_mainContext save:&error]) {
NSLog(@"error in adding data %@, %@", error, [error userInfo]);
abort();
}
[self.navigationController popToRootViewControllerAnimated:YES];
}
告诉我这样的错误:&#39; NSInvalidArgumentException&#39;,原因: &#39; - [详细信息addDetailRelationship:]:发送到的无法识别的选择器 实例0x6000000ad740&#39;
答案 0 :(得分:0)
您正在发送带有签名的邮件
import matplotlib.pyplot as plt
import numpy as np
# Random test data
np.random.seed(123)
all_data = [np.random.normal(0, 5, 100) for std in range(1, 21)]
fig, ax = plt.subplots(nrows=1, figsize=(9, 4))
# rectangular box plot
bplot = ax.boxplot(all_data, 0, '', 0, patch_artist=True)
cm = plt.cm.get_cmap('rainbow')
colors = [cm(val/len(all_data)) for val in range(len(all_data))]
for patch, color in zip(bplot['boxes'], colors):
patch.set_facecolor(color)
plt.show()
但不存在具有该签名的方法。相反,你的签名是
- (void)addDetailRelationship:(NSSet<Student *> *)values,
(请注意从- (void)addDetailRelationship:(NSSet<Detail *> *)values
到Detail *
的更改。因此,您的应用会因无法识别的选择器异常而崩溃。我不知道您的程序逻辑是什么,但您需要要么使用此签名创建方法,要么传入正确类型的参数。
答案 1 :(得分:0)
核心数据为您管理关系逆转。如果您设置了详细信息的学生关系,核心数据会自动将其设置为学生之一&#39;细节。没有任何需要崩溃的东西,所以只需删除
即可 [detailtEntity addDetailRelationship:mySet];//crash app here
(它崩溃的原因是你需要在学生上添加详细的关系 - [_studentClass addDetailRelationship:mySet]。但正如我上面所说,你根本不需要这条线路。