您的位置:首页 > 移动开发 > Objective-C

Cannot assign value of type 'AnyObject' to type 'HereIsTheTypeName?'

2016-09-14 22:32 1101 查看
This is a type covert issue. for example:

Cannot assign value of type ‘AnyObject’ to type ‘CAAnimationDelegate?’

if let delegate: AnyObject = completionDelegate {

rotateAnimation.delegate = delegate

}

Solution 1: if you are sure the your is type of HereIsTheTypeName , you can make a force cast like this:

rotateAnimation.delegate = delegate as! HereIsTheTypeName

(rotateAnimation.delegate = delegate as! CAAnimationDelegate

)

Solution2: use flatMap and an individual optional cast of each object if you are using array like this:

stringArray = tempStringArray.flatMap{ $0 as? String }

flatMap reference:

https://developer.apple.com/reference/swift/sequence/1641503-flatmap

Official exmaple:

let possibleNumbers = [“1”, “2”, “three”, “///4///”, “5”]

let mapped: [Int?] = numbers.map { str in Int(str) }

// [1, 2, nil, nil, 5]

let flatMapped: [Int] = numbers.flatMap { str in Int(str) }

// [1, 2, 5]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐