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]
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]
相关文章推荐
- Value of type java.lang.String cannot be converted to JSONObject
- Json解析异常处理方式(JSONException: Value of type java.lang.String cannot be converted to JSONObject)
- CXF报错[1 counts of IllegalAnnotationExceptions]and[Two classes have the same XML type name]and[Use @XmlType.name and @XmlType.namespace to assign different names to them]
- "Failed to add file '(name here)': object of the same name already exists
- line 1 of the inf file winnt.xpe is invalid setup cannot continue. press any key to exit
- JSONException: Value of type java.lang.String cannot be converted to JSONObject
- Json解析异常处理方式(JSONException: Value of type java.lang.String cannot be converted to JSONObject)
- Cannot assign value of type 'java.lang.String' to property 'file' of type 'org.apache.struts.upload.FormFile'
- JSONException: Value of type java.lang.String cannot be converted to JSONObject
- org.json.JSONException: Value ok of type java.lang.String cannot be converted to JSONObject:
- Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#<Object>‘的解决方法
- new JSONObject(str)无法解析 报错:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
- Access restriction: The method typeNameToClass(String) from the type ObjectHandler is not accessible due to restriction on required library
- org.json.JSONException: Value ok of type java.lang.String cannot be converted to JSONObject: [Caugh
- Unable to get value of the property '2': object is null or undefined
- Value of type java.lang.String cannot be converted to JSONObject 异常
- Json解析异常处理方式(JSONException: Value of type java.lang.String cannot be converted to JSONObject)
- Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
- Value of type java.lang.String cannot be converted to JSONObject
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.