Layer.com在swift中取消阻止用户

Layer.com在swift中取消阻止用户,swift,nspredicate,nsorderedset,Swift,Nspredicate,Nsorderedset,我想让NSPredicate在Swift工作。下面的代码段在“if”语句之前崩溃 func unblockUser() { let policies:NSOrderedSet = self.layerClient.policies let policyPredicate: NSPredicate = NSPredicate(format: "SELF.sentByUserID = " + otherUserId + " AND SELF.type = 0") let fi

我想让NSPredicate在Swift工作。下面的代码段在“if”语句之前崩溃

func unblockUser() {
    let policies:NSOrderedSet = self.layerClient.policies
    let policyPredicate: NSPredicate = NSPredicate(format: "SELF.sentByUserID = " + otherUserId + " AND SELF.type = 0")
    let filteredPolicies:NSOrderedSet = policies.filteredOrderedSetUsingPredicate(policyPredicate)
    if (filteredPolicies.count > 0) {
        var error: NSError?
        let policy:LYRPolicy = filteredPolicies.firstObject as! LYRPolicy
        let success:Bool = self.layerClient.removePolicy(policy, error: &error)
        if !success {
            println("Failed adding policy with error " + error!.localizedDescription);
        }
    }
}
以下是错误:

> *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<LYRPolicy 0x7fcb5a837b20> valueForUndefinedKey:]: this class is not key value coding-compliant for the key s3SL19VWhA.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000111debc65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000112f02bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000111deb8a9 -[NSException raise] + 9
    3   Foundation                          0x00000001124db82a -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226
    4   Foundation                          0x0000000112434b23 -[NSObject(NSKeyValueCoding) valueForKey:] + 251
    5   Foundation                          0x000000011246ffbf -[NSFunctionExpression expressionValueWithObject:context:] + 1071
    6   Foundation                          0x000000011246facf -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 278
    7   Foundation                          0x0000000112499840 -[NSCompoundPredicateOperator evaluatePredicates:withObject:substitutionVariables:] + 251
    8   Foundation                          0x00000001124996b9 -[NSCompoundPredicate evaluateWithObject:substitutionVariables:] + 295
    9   Foundation                          0x000000011246f951 _filterObjectsUsingPredicate + 348
    10  Foundation                          0x0000000112542423 -[NSOrderedSet(NSPredicateSupport) filteredOrderedSetUsingPredicate:] + 301
    11  EDM Cake                            0x000000010ecd3a01 _TFC8EDM_Cake26ConversationViewController11unblockUserfS0_FT_T_ + 1089
    12  EDM Cake                            0x000000010ecd82cb _TFFC8EDM_Cake26ConversationViewController16confirmBlockUserFS0_FT_T_U_FGSQCSo13UIAlertAction_T_ + 59
    13  EDM Cake                            0x000000010ecd8307 _TTRXFo_oGSQCSo13UIAlertAction__dT__XFo_iGSQS___iT__ + 23
    14  EDM Cake                            0x000000010ecd3001 _TPA__TTRXFo_oGSQCSo13UIAlertAction__dT__XFo_iGSQS___iT__ + 81
    15  EDM Cake                            0x000000010ecd8240 _TTRXFo_iGSQCSo13UIAlertAction__iT__XFo_oGSQS___dT__ + 32
    16  EDM Cake                            0x000000010ecd8288 _TTRXFo_oGSQCSo13UIAlertAction__dT__XFdCb_dGSQS___dT__ + 56
    17  UIKit                               0x00000001104bb9a1 -[UIAlertController _fireOffActionOnTargetIfValidForAction:] + 55
    18  UIKit                               0x00000001104bbee3 __85-[UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:]_block_invoke + 30
    19  UIKit                               0x000000011035f8b9 -[UIPresentationController transitionDidFinish:] + 1118
    20  UIKit                               0x0000000110361b3c __56-[UIPresentationController runTransitionForCurrentState]_block_invoke_2 + 133
    21  UIKit                               0x00000001109a0660 -[_UIViewControllerTransitionContext completeTransition:] + 110
    22  UIKit                               0x00000001102cc193 -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 326
    23  UIKit                               0x00000001102b30f6 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 209
    24  UIKit                               0x00000001102b342c -[UIViewAnimationState animationDidStop:finished:] + 76
    25  QuartzCore                          0x000000010ff83892 _ZN2CA5Layer23run_animation_callbacksEPv + 308
    26  libdispatch.dylib                   0x0000000113604614 _dispatch_client_callout + 8
    27  libdispatch.dylib                   0x00000001135eca1c _dispatch_main_queue_callback_4CF + 1664
    28  CoreFoundation                      0x0000000111d531f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    29  CoreFoundation                      0x0000000111d14dcb __CFRunLoopRun + 2043
    30  CoreFoundation                      0x0000000111d14366 CFRunLoopRunSpecific + 470
    31  GraphicsServices                    0x000000011531aa3e GSEventRunModal + 161
    32  UIKit                               0x00000001102538c0 UIApplicationMain + 1282
    33  EDM Cake                            0x000000010ed18f87 main + 135
    34  libdyld.dylib                       0x0000000113638145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[valueForUndefinedKey:]:此类不符合密钥s3SL19VWhA的键值编码。”
***第一次抛出调用堆栈:
(
0 CoreFoundation 0x0000000111debc65例外预处理+165
1 libobjc.A.dylib 0x0000000112f02bb7 objc_异常_抛出+45
2 CoreFoundation 0x0000000111deb8a9-[N异常提升]+9
3基金会0x000 000 01124dB82a- [NSbObjs](NSKEY ValueCudid)ValueFunDealdKEY::+ 226
4基金会0x000 000 0112434 B23 - [NSbObjs](NSKEY ValueCudid)ValueFoiKy::+ 251
5基金会0x000 000 011246FFBF- [ NSCONTRORM表达式ValueObjult:Currase:] + 1071
6基金会0x000 000 011246FACF- [ NSISTIMEON谓词评价对象:替换变量:] + 278
7基金会0x000 000 0112499 840-[ NSCOMPLATION谓词运算符评估谓词:使用对象:替换变量:] + 251
8基金会0x000 000 0112499 6B9- [ NSCOMPATION谓词评价对象:对象:置换变量:] + 295
9基金会0x000 000 011246F951,过滤对象+谓词+ 348
10基金会0x000 000 011254 2423 - [ NSORDReDeSET(NSPReDeCATESUpPATH)过滤] OrrordEdvestSuestIn谓词:] + 301
11 EDM Cake 0x000000010ecd3a01\u TFC8EDM\u Cake26对话视图控制器11UnbuckUserFS0\u FT\u T\u+1089
12 EDM Cake 0x000000010ecd82cb-TFFC8EDM-Cake26对话视图控制器16确认锁定用户FS0-U FT-U-U-FGSQCSo13UIAlertAction-U+59
13 EDM蛋糕0x000000010ecd8307_TTRXFo_oGSQCSo13UIAlertAction__dT__XFo_iGSQS__________+23
14 EDM蛋糕0x000000010ecd3001\u TPA\u TTRXFo\u oGSQCSo13UIAlertAction\u dT\u XFo\u iGSQS\u iT\u+81
15 EDM蛋糕0x000000010ecd8240–TTRXFo–iGSQCSo13UIAlertAction–UIT–Uxfo–oGSQS–UUUdt+32
16 EDM蛋糕0x000000010ecd8288–TTRXFo–oGSQCSo13UIAlertAction–UUDT–UUXFDCB–U dGSQS–UUUU dT–UU56
17 UIKit 0x00000001104bb9a1-[UIAlertController\u FireOffActionOnTargetValidForAction:+55
18 UIKit 0x00000001104bbee3 \uuuu 85-[UIAlertController\u dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:][u块\u调用+30
19 UIKit 0x000000011035f8b9-[UIPresentationController转换IDFinish:+1118
20 UIKit 0x0000000110361b3c\uuU 56-[UIPresentationController runTransitionForCurrentState]\u块\u调用\u 2+133
21 UIKit 0x00000001109a0660-[\u UIViewControllerTransitionContext completeTransition:][110
22 UIKit 0x00000001102cc193-[UIViewAnimationBlockDelegate\u DiEndBlockAnimation:finished:context:+326
23 UIKit 0x00000001102b30f6-[UIViewAnimationState sendDelegateAnimationDidStop:finished:+209
24 UIKit 0x00000001102b342c-[UIViewAnimationState animationDidStop:finished:+76
25夸脱核心0x000000010ff83892\u ZN2CA5layer23运行\u动画\u回调EPV+308
26 libdispatch.dylib 0x0000000113604614 _dispatch_client_callout+8
27 libdispatch.dylib 0x00000001135eca1c_dispatch_main_queue_callback_4CF+1664
28 CoreFoundation 0x000000011D531F9\uuu CFRUNLOOP\u正在为\u主\u调度\u队列\uu9提供服务
29 CoreFoundation 0x0000000111d14dcb\uuu CFRunLoopRun+2043
30 CoreFoundation 0x0000000111d14366 CFRunLoopRunSpecific+470
31图形服务0x000000011531aa3e GSEventRunModal+161
32 UIKit 0x00000001102538c0 UIApplicationMain+1282
33 EDM滤饼0x000000010ed18f87干管+135
34 libdyld.dylib 0x0000000113638145开始+1
)
libc++abi.dylib:以NSException类型的未捕获异常终止

我遇到了同样的错误,所以我决定只迭代策略的NSOrderedSet(layerClient.policies)。这是我的密码:

func blockUser(id: String) {
    let blockPolicy = LYRPolicy(type: .Block)
    blockPolicy.sentByUserID = id

    do {
        try layerClient?.addPolicy(blockPolicy)
        print("[LAYER] Successfully blocked user \(id)")
    } catch {
        print("[LAYER] Error blocking user \(id): \(error)")
    }
}

func unblockUser(id: String) {
    // First check to see if this user is already blocked
    var existingBlock: LYRPolicy?
    for policy in layerClient?.policies ?? NSOrderedSet() {
        let policy = policy as? LYRPolicy
        if policy?.sentByUserID == id && policy?.type == .Block {
            existingBlock = policy
            break
        }
    }

    if let existingBlock = existingBlock {
        do {
            try layerClient?.removePolicy(existingBlock)
            print("[LAYER] Successfully unblocked user \(id)")
        } catch {
            print("[LAYER] Error unblocking user \(id): \(error)")
        }
    } else {
        print("[LAYER] User \(id) was never blocked in the first place")
    }
}