来自' [NSObject:AnyObject]?'不相关的类型' NSDictionary'总是失败

时间:2015-11-03 04:47:52

标签: swift nsdictionary

此行let userInfo = notification.userInfo as! NSDictionary我收到警告:Cast from '[NSObject : AnyObject]?' to unrelated type 'NSDictionary' always fails

我尝试使用let userInfo = notification.userInfo as! Dictionary<NSObject: AnyObject>替换let userInfo = notification.userInfo as! NSDictionary。但我收到一个错误:Expected '>' to complete generic argument list。如何修复警告。

Xcode 7.1 OS X Yosemite

这是我的代码:

func keyboardWillShow(notification: NSNotification) {

    let userInfo = notification.userInfo as! NSDictionary //warning

    let keyboardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
    let keyboardBoundsRect = self.view.convertRect(keyboardBounds, toView: nil)

    let keyboardInputViewFrame = self.finishView!.frame

    let deltaY = keyboardBoundsRect.size.height

    let animations: (()->Void) = {

        self.finishView?.transform = CGAffineTransformMakeTranslation(0, -deltaY)
    }

    if duration > 0 {



    } else {

        animations()
    }


}

2 个答案:

答案 0 :(得分:4)

NSNotification的userInfo属性已被定义为(n个可选)字典。

所以你根本不需要施放它,只需打开它。

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        ...
    }
}

所有其余代码应该按原样运行。

答案 1 :(得分:3)

您正试图强制转换为NSDictionary的可选项。尝试:

import java.math.BigInteger;
public static BigInteger power(int a,int b){
BigInteger b1 = new BigInteger(Integer.toString(a));  
BigInteger b2 = new BigInteger("1");
for(int i = 0; i < b; i++){
b2=b2.multiply(b1);
}
return b2;//return a^b
}

这对我有用。