Попытка фильтрации (поиска) в основном хранилище данных людей по имени с буквой D в их имени. Первый код работает нормально, но когда вы переходите к параметру во втором коде, он падает с потоком 1 sigabrt.
class func filterData() -> [Patient]?{
let context = getContext()
let fetchRequest:NSFetchRequest<Patient> = Patient.fetchRequest()
var patient:[Patient]? = nil
var predicate = NSPredicate(format: "name contains[c] %@", "D")
fetchRequest.predicate = predicate
do{
patient = try context.fetch(fetchRequest)
return patient
}catch{
return patient
}
}
class func filterData(x: String, y: String) -> [Patient]?{
let context = getContext()
let fetchRequest:NSFetchRequest<Patient> = Patient.fetchRequest()
var patient:[Patient]? = nil
var predicate = NSPredicate(format: "\(y) contains[c] %@", "\(x)")
fetchRequest.predicate = predicate
do{
patient = try context.fetch(fetchRequest)
return patient
}catch{
return patient
}
}
Единственная разница между двумя блоками кода - строки 1 и 5.
точка останова указывает на сбой в строке 5 второго блока кода
Консоль
2018-05-09 01:08:40.105003-0400 coreDataApp[45029:6524223] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string " contains[c] %@"'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a8891e6 __exceptionPreprocess + 294
1 libobjc.A.dylib 0x000000010672c031 objc_exception_throw + 48
2 Foundation 0x000000010613e8bd _qfqp2_performParsing + 7771
3 Foundation 0x000000010613c9fb +[NSPredicate predicateWithFormat:arguments:] + 46
4 libswiftFoundation.dylib 0x0000000109e8eaf0 _T0So12NSExpressionC10FoundationEABSS6format_s7CVarArg_pdtcfcTf4xnn_nTm + 112
5 libswiftFoundation.dylib 0x0000000109dee1ff _T0So12NSExpressionC10FoundationEABSS6format_s7CVarArg_pdtcfCTm + 79
6 libswiftFoundation.dylib 0x0000000109dee1a6 _T0So11NSPredicateC10FoundationEABSS6format_s7CVarArg_pdtcfC + 38
7 coreDataApp 0x0000000105e1111f _T011coreDataApp04CoreB7HandlerC06filterB0SayAA7PatientCGSgSS1x_SS1ytFZ + 1071
8 coreDataApp 0x0000000105e14845 _T011coreDataApp14ViewControllerC6getteryypF + 1493
9 coreDataApp 0x0000000105e16781 _T011coreDataApp14ViewControllerC6getteryypFTo + 81
10 UIKit 0x0000000107516448 -[UIApplication sendAction:to:from:forEvent:] + 83
11 UIKit 0x0000000107691804 -[UIControl sendAction:to:forEvent:] + 67
12 UIKit 0x0000000107691b21 -[UIControl _sendActionsForEvents:withEvent:] + 450
13 UIKit 0x0000000107690a69 -[UIControl touchesEnded:withEvent:] + 580
14 UIKit 0x000000010758b11f -[UIWindow _sendTouchesForEvent:] + 2729
15 UIKit 0x000000010758c821 -[UIWindow sendEvent:] + 4086
16 UIKit 0x0000000107530370 -[UIApplication sendEvent:] + 352
17 UIKit 0x0000000107e7157f __dispatchPreprocessedEventFromEventQueue + 2796
18 UIKit 0x0000000107e74194 __handleEventQueueInternal + 5949
19 CoreFoundation 0x000000010a82bbb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
20 CoreFoundation 0x000000010a8104af __CFRunLoopDoSources0 + 271
21 CoreFoundation 0x000000010a80fa6f __CFRunLoopRun + 1263
22 CoreFoundation 0x000000010a80f30b CFRunLoopRunSpecific + 635
23 GraphicsServices 0x000000010d234a73 GSEventRunModal + 62
24 UIKit 0x00000001075150b7 UIApplicationMain + 159
25 coreDataApp 0x0000000105e19707 main + 55
26 libdyld.dylib 0x000000010bb0a955 start + 1
27 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
РЕДАКТИРОВАТЬ 2: так я вызываю метод filterdata () с параметрами из контроллера предупреждений
var ruckus = String()
var rico = String()
@IBAction func getter(_ sender: Any) {
let getter = UIAlertController(title: "Kingdom Search", message: nil, preferredStyle: .alert)
getter.addTextField{ (textField) in
textField.placeholder = "type of search"
}
getter.addTextField{ (textField) in
textField.placeholder = "search term"
}
let action = UIAlertAction(title: "Search", style: .default){ (_) in
let king = getter.textFields!.first?.text!
let phyl = getter.textFields!.last?.text!
self.ruckus = king!
self.rico = phyl!
}
getter.addAction(action)
present(getter, animated: true, completion: nil)
patient = CoreDataHandler.filterData(x: ruckus, y: rico)
for i in patient!{
self.labelshow.text = i.name!
self.lebelshow.text = i.problem!
}
}