0

I have my app set for crossing over between Swift and Obj-C. Define Module is set to yes, I've imported the -Swift.h file into the viewcontroller I am working on. I am now trying to present the swift view controller from the objective-c view controller.

Here's some code that works, the swift view controller is presented and the background color is red:

 BulkProcessController* myBulkProcessController = [self.storyboard instantiateViewControllerWithIdentifier:@"bulkProcessController"];

 myBulkProcessController.view.backgroundColor = [UIColor redColor];

 [self presentChildViewController:myBulkProcessController];

However, in swift view controller I have a enum and var property set:

class BulkProcessController: UIViewController {

    enum ProcessType {
        case AddContact
        case DeleteContact
    }

    var myProcessType: ProcessType = .AddContact

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

Back to the instantiating part. If I change the code to this:

BulkProcessController* myBulkProcessController = [self.storyboard instantiateViewControllerWithIdentifier:@"bulkProcessController"];

ProcessType thisProcessType = [[[notification userInfo] objectForKey:@"Process Type"] intValue];
myBulkProcessController.myProcessType = ADDCONTACTS;

myBulkProcessController.view.backgroundColor = [UIColor redColor];

[self presentChildViewController:myBulkProcessController];

The line: myBulkProcessController.myProcessType = ADDCONTACTS; throws an error [NOTE: I know ADDCONTACTS is not a proper value, it is AddContact, that's a separate issue]. The error is myProcessType not found on object of type BulkProcessController.

And yet, if I put a break point right after instantiating the view controller I can see the property in the debug console:

enter image description here

Is this an issue of where the property is there but not available to Obj-C? I thought importing the -Swift.h file resolved that. If this is the case, how do I access the swift file properties from Obj-C? Or am I doing it wrong in the Swift file?

Any help would be appreciated.

PruitIgoe
  • 6,166
  • 16
  • 70
  • 137
  • The issue is because of Swift enums (ObjC supports only integer-backed enums). This answer should help: https://stackoverflow.com/questions/30480338/how-to-make-a-swift-string-enum-available-in-objective-c – Alladinian Jun 02 '17 at 11:08
  • So just add @objc to the enum declaration? – PruitIgoe Jun 02 '17 at 12:30
  • 1
    No, that will be not enough. Right now `ProcessType` cannot be expressed in ObjC, it must be `enum ProcessType: Int` instead. – Alladinian Jun 02 '17 at 12:36
  • 1
    OK, I fixed it to this: @objc public enum BulkProcessType: Int { case BulkAdd case BulkDelete } if you make something like that as the answer I'll give you the bump – PruitIgoe Jun 02 '17 at 12:37
  • Done. Glad that helped you :) – Alladinian Jun 02 '17 at 12:47

1 Answers1

1

The problem is that ProcessType cannot be expressed in ObjC (remember, it only supports integer-backed enums) so if you don't really need another type to back your enum you could just do something like this:

@objc
enum ProcessType: Int {
    case AddContact
    case DeleteContact
}

Sidenote: Please note that, by convention, enum cases in Swift start with a lowercase letter (so maybe addContact & deleteContact - or even just add & delete would be more appropriate)

Alladinian
  • 34,483
  • 6
  • 89
  • 91