Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 UIActivity Subclass problems with override

I just upgraded from swift 2 to swift 3 and I am running into some problems with overriding a property.

In swift 2 you could set the activity type like this

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 override activityType like this

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

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

One solution i found is to add the annotation @nonobjc to it like so:

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

While this helps make the error go away, this property never gets called... This is a problem because when the user completes the activity and completionWithItemsHandler() gets called, the activity type is considered nil.

One work around I found is to use an objective-c extension. It works; it gives me type "horse" like I wanted.

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

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

My question is how to do it in a pure swift.

like image 705
DerrickHo328 Avatar asked Feb 06 '23 07:02

DerrickHo328


1 Answers

In pure Swift3 I am able to compile it using

override var activityType: UIActivityType {
    return UIActivityType(rawValue: self.customActivityType.rawValue)
}
like image 119
Susim Samanta Avatar answered Feb 08 '23 21:02

Susim Samanta