In activityViewController(_:itemForActivityType:) 'activityType' is sometimes nil but not optional
| Originator: | justindarc | ||
| Number: | rdar://31353832 | Date Originated: | 3/30/2017 |
| Status: | Open | Resolved: | |
| Product: | Product Version: | iOS 10.2 | |
| Classification: | Reproducible: |
Area:
UIKit
Summary:
According to the documentation for UIActivityItemSource ( https://developer.apple.com/reference/uikit/uiactivityitemsource/1620453-activityviewcontroller ), activityViewController(_:itemForActivityType:) can sometimes be called with a nil `activityType`. However, the declaration does not indicate it as being an optional value. This causes a compiler warning when checking if `activityType` is nil. However, if Swift compiler optimizations are turned on, the comparison to nil no longer happens which causes the app to crash.
Steps to Reproduce:
1. Implement `activityViewController(_:itemForActivityType:)` in a UIActivityItemSource as follows:
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
if activityType != nil {
print(activityType.rawValue)
} else {
print("activityType was nil")
}
}
2. Build and run with Swift compiler optimizations turned on
Expected Results:
"activityType was nil" is logged to the console when the activity view is invoked with a nil `activityType`
Actual Results:
App crashes when activity view is invoked with a nil `activityType`
Version:
iOS 10.2
Notes:
Because the documentation states that `activityType` can sometimes be nil, the UIActivityItemSource instance method declaration should accept `activityType` as an optional value like this:
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType?) -> Any?
Configuration:
iPhone 6s
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!