Swift 3 UIActivity subclass tasks with override

I just upgraded from swift 2 to swift 3 and I am having problems redefining the property.

In swift 2, you can set the type of activity as follows

override func activityType() -> String? {
   return "com.a.string"
}

However, in swift 3 (in Xcode 8 beta 6) they introduced NS_STRING_ENUM, and now we redefined actionType like this

override var activityType() -> UIActivityType? {
    return UIActivityType.customActivityType
}

The problem is that Xcode will complain about this: Property cannot be an override of @objc because its type cannot be represented in Objective-C

One solution found is to add the @nonobjc annotation to it as follows:

@nonobjc override var activityType() -> UIActivityType? {
    return UIActivityType.customActivityType
}

Although this helps to clear the error, this property is never called ... This is a problem because when the user completes his activity and the call toWithItemsHandler () ends, the activity type is considered nil.

, , - objective-c. ; "", .

@interface Custom_UIActivity (custom)
- (UIActivityType)activityType;
@end

@implementation Custom_UIActivity (custom)
- (UIActivityType)activityType {
    return @"horses";
}

- .

+4
1

Swift3

override var activityType: UIActivityType {
    return UIActivityType(rawValue: self.customActivityType.rawValue)
}
+8

Source: https://habr.com/ru/post/1652524/


All Articles