4

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.

DerrickHo328
  • 4,664
  • 7
  • 29
  • 50
  • 1
    It seems to be a bug of Swift 3 (beta 6) and you can find some thread in [the Apple's dev forums](https://forums.developer.apple.com/message/170059#169260). Using Objective-C code seems to be the only workaround as for now. – OOPer Aug 25 '16 at 22:32

1 Answers1

8

In pure Swift3 I am able to compile it using

override var activityType: UIActivityType {
    return UIActivityType(rawValue: self.customActivityType.rawValue)
}
Susim Samanta
  • 1,605
  • 1
  • 14
  • 30