谁能告诉我这段代码有什么问题?它引发以下错误并导致应用程序崩溃:
reason: 'keypath Studies.patients.PatientName not found in entity <NSSQLEntity Studies id=3>'
Run Code Online (Sandbox Code Playgroud)
码:
- (void)viewDidLoad {
[super viewDidLoad];
test_coredataAppDelegate *appDelegate = (test_coredataAppDelegate *)[[UIApplication sharedApplication] delegate];
self.context = appDelegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Studies" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
/**/
NSLog(patientName);
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:
@"(Studies.patients.PatientName == %@ )",patientName]];
NSError *error;
self.StudiessList = [_context executeFetchRequest:fetchRequest error:&error];
self.title = @"patients";
[fetchRequest release];
}
Run Code Online (Sandbox Code Playgroud) 我尝试以多种不同的方式创建 NSFetchRequest,每次出现此错误时:
“在范围内找不到类型‘NSFetchRequest’”
以下是我尝试过的具体方法:
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Task")
let fetchRequest: NSFetchRequest<Task> = Task.NSFetchRequest()
let fetchRequest = Task.fetchRequest() as! NSFetchRequest<Task>
Run Code Online (Sandbox Code Playgroud)
知道为什么这会给我这个错误吗?我知道这不太可能;iklely 但我在 Xcode 12 beta 上,这可能是 Xcode 的错误吗?我遵循了一个教程,因为我只是在学习 SwiftUI(以及一般的 Swift),所以在我看来这段代码应该可以工作。我已经在很多地方寻找过这个问题的答案,所以如果很明显,我很抱歉我错过了。
我想先按NSFetchRequest日期订购,然后如果它按名称匹配同一天的订单.我使用a UIDatePicker来获取日期并使用Core Data保存它
[self.managedObject setValue:self.datePicker.date forKey:self.keypath];
Run Code Online (Sandbox Code Playgroud)
并排序NSFetchRequest如下:
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"day" ascending:NO];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
Run Code Online (Sandbox Code Playgroud)
现在我的问题是它只按日期排序,而不是按名称排序,因为UIDatePicker存储在Core Data中的日期也是小时.因此,即使在同一天,由于小时不同,在同一天没有按"名称"排序.那么如何在核心数据中只保存日期mm/dd/yyyy而不是de小时UIDatePicker?
或者您是否考虑过任何其他解决方案?
我想要一份关于两者之间比较的详细清单.我所知道的事情:
executeFetchRequest:
performFetch:
fetchedObjects返回一个托管对象数组performFetch初始化该进程.performFetch第二次 如果我错了请纠正我并附上清单.谢谢.
core-data objective-c nsfetchedresultscontroller nsfetchrequest
在一个应用程序上工作,我有一大堆托管对象,我想要获取一些随机实例.
我的问题是,有什么方法可以使用NSPredicate和NSFetchRequest随机返回几个对象.
我看到你实际上可以使用数据建模器将NSFetchRequest添加到实体中,任何使用此方法进行随机获取的方法?
还有什么是确定表"计数"的最佳方法,这样我就可以设置随机数生成器的界限.
如果您需要更多详细信息,请告诉我们.
谢谢!
缺口
目的:我想从数据库(核心数据)中获取一个属性(从实体)到数组的值.
例
实体名称=员工
Attribute = employeeID
我只想将所有employeeID填充到数组/集中.
题
鉴于以下是我的实现,我只觉得它是一种圆形的方式,我想知道是否有更好的方法来做到这一点.
码
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Employees"];
fetchRequest.resultType = NSDictionaryResultType;
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"employeeID", nil]];
NSError *error = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest
error:&error];
NSMutableArray *employeeIDs = [NSMutableArray array];
for(id currentRecord in results)
{
[employeeIDs addObject:[currentRecord objectForKey:@"employeeID"]];
}
Run Code Online (Sandbox Code Playgroud) 例如,我有一个名为"Friends"的实体的托管对象模型,而朋友有firstName.我想得到firstName等于"George"的所有朋友.我怎样才能做到这一点?
我有一个核心数据库,我正在尝试使用块谓词创建一个获取请求,但是我得到一个未知谓词错误:
注意: employeeToHouse是我在子类化NSManagedObject时为我创建的House类型的属性
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Place"];
request.predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
Place *sortPlace = (Place *)evaluatedObject;
CLLocation *placeLocation = [[CLLocation alloc] initWithLatitude:sortPlace.latitude.doubleValue
longitude:sortPlace.longitude.doubleValue];
CLLocationDistance distance = [placeLocation distanceFromLocation:self.userLocation];
sortPlace.distanceToUser = [NSNumber numberWithDouble:distance];
if(distance<3000)
{
return YES;
}
return NO;
}];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@ "distanceToUser"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
[self.loadingView removeSpinner];
[self setupFetchedResultsControllerWithFetchRequest:request];
Run Code Online (Sandbox Code Playgroud)
然后我得到这个错误:
NSInvalidArgumentException',原因:'谓词的未知谓词类型:BLOCKPREDICATE(0x70ad750)'
难道我做错了什么?
任何帮助赞赏.
Xcode自动更新到8 ...我正在针对IOS 9.3
让所有的代码都被转换,但现在有一件事情已经破裂,我在类似的问题中尝试了各种建议!我取请求被以前的工作现在已经断裂.
我的目标是获得一份清晰的清单.该应用程序崩溃了:
let results = try context.fetch(fetchRequest)
Run Code Online (Sandbox Code Playgroud)
控制台中描述的错误为:
Could not cast value of type 'NSKnownKeysDictionary1' (0x10fd29328) to 'MyApp.BodyType' (0x10eebc820).
Run Code Online (Sandbox Code Playgroud)
这是功能
func getBodyTypes() {
let context = ad.managedObjectContext
let fetchRequest = NSFetchRequest<BodyType>(entityName: "BodyType")
fetchRequest.propertiesToFetch = ["name"]
fetchRequest.returnsDistinctResults = true
fetchRequest.resultType = NSFetchRequestResultType.dictionaryResultType
do {
let results = try context.fetch(fetchRequest)
for r in results {
bodyTypes.append(r.value(forKey: "name") as! String)
}
} catch let err as NSError {
print(err.debugDescription)
}
}
Run Code Online (Sandbox Code Playgroud)
如果下面的行是隐藏的,它不会破坏,但后来我没有得到我想要的!
fetchRequest.resultType = NSFetchRequestResultType.dictionaryResultType
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用所有结果(4300)并将它们作为一个bandaid修复程序循环,但这并不是解决这个问题的正确方法,特别是因为它之前正在工作!
我有2个实体,任务和列表.每个任务与一个名为"list"的List对象具有一对一的关系,并且与List有一个反向关系,它与Task有一个to-many关系,称为"tasks".
我正在尝试使用带有NSPredicate的获取请求来获取属于指定List的所有Task对象:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"list=%@", theList];
[fetchRequest setPredicate:predicate];
Run Code Online (Sandbox Code Playgroud)
(其中"theParent"是对List对象的引用).但是,这不会返回任何提取的对象.如果我取出谓词,那么返回对象(所以我知道它们存在,并且通过NSLogging theList我知道它有与之关联的Task对象).
谢谢
nsfetchrequest ×10
core-data ×7
iphone ×5
nspredicate ×4
objective-c ×3
ios ×2
swift ×2
arrays ×1
cocoa-touch ×1
date ×1
distinct ×1
predicate ×1
sorting ×1
uidatepicker ×1
xcode ×1