Layer.com在swift中解锁用户



我正试图让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

我得到了同样的错误,所以我决定只是迭代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")
    }
}

最新更新