在swift中传递参数?

时间:2018-05-09 05:02:50

标签: ios swift search core-data parameters

尝试过滤(搜索)人员的核心数据容器,名称中包含字母D.第一个代码工作正常,但是当您在第二个代码中传递params时,它会以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:这就是如何使用来自警报控制器的params调用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!

    } 

}

1 个答案:

答案 0 :(得分:2)

不要使用字符串插值来构建谓词格式字符串。 对于动态密钥,您需要[0]: "first name" [1]: "=> saran" [2]: "address" [3]: "@> my address" 格式:

%K

如果var predicate = NSPredicate(format: "%K contains[c] %@", key, value) 不是该属性的名称,这仍然会崩溃 实体。可以使用key来编译 检查并生成属性名称。