1

I have looked at a lot of posts on this topic, and I still can't see what I am doing wrong. I have a view controller in objective-c that is trying to access an NSObject code snippet in Swift. I have Debugs Module YES and both the objective-c Bridging Header and the Swift header are at the correct places in Build Settings. Here are the relevant parts of the code:

View Controller.m

    //
//  ViewController.m
//  swiftTest
//
//  Created by Nelson Capes on 2/19/17.
//  Copyright © 2017 Nelson Capes. All rights reserved.
//

#import "ViewController.h"
#import "swiftTest-Swift.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    MyClass
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

Swift code:

    //
//  MyClass.swift
//  swiftTest
//
//  Created by Nelson Capes on 2/19/17.
//  Copyright © 2017 Nelson Capes. All rights reserved.
//

import Foundation
@objc public class MyClass:NSObject{
    var property:String = ""
    func method() {
        print(self.property)
    }
}

In ViewController.m, starting to type MyClass receives the following warning from the compiler:

/Users/nelson/swiftTest/swiftTest/ViewController.m:20:5: Use of undeclared identifier 'MyClass'.

Note that I declare the Swift class as @objc and public. My understanding from Apple's documentation is that this should make any vars and functions visible to objective-c code in the same target. Project module name is 'swiftTest.'

Can anyone shed some light on this? I've been trying to get it to work for almost an entire day. Thanks!

Nelson Capes
  • 411
  • 3
  • 12
  • 1
    Is that your real code? With just `MyClass` in `viewDidLoad`? That cannot compile. What do you expect it to do? – Martin R Feb 19 '17 at 18:42
  • Are you trying to write Swift syntax inside `viewDidLoad` of an Objective-C file? I'm pretty sure you can use the Swift class, but you still need to use the Obj-C syntax `MyClass *swiftObject = [[MyClass alloc] init];` - Or however your initializers are setup. – Pierce Feb 19 '17 at 19:28
  • No, it isn't the real code. As I said in the post, as soon as I enter MyClass the compiler won't let me add the rest of the code. But here is the raw, uncompiled code: – Nelson Capes Feb 20 '17 at 15:22
  • @MartinR, No, it isn't the real code. As I said in the post, as soon as I enter MyClass the compiler wouldn't let me add the rest of the code. But here is the raw, uncompiled code, and strangely it is now working. I don't know what is different but it is the same thing I ran earlier when I got the "use of undeclared identifier" error: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. MyClass *myclass = [[MyClass alloc]init]; myclass.property = @"Hello, Swift"; [myclass method]; } – Nelson Capes Feb 20 '17 at 15:31

1 Answers1

0

try @objc public var property:String = ""

@objc public has a benefit of a compiler error if you try to do smthing that something can not be represented in objc you'd get

Property cannot be marked @objc because its type cannot be represented in Objective-C

If you have no objc accessible symbols then objc bridger would prtimize your class out. Which is what you saw.

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66