1

I have a Swift class:

@objc
public class MZAActiveState: NSObject {
    @objc static let privateSharedInstance = MZAActiveState()
..
    @objc
    public class func shared() -> MZAActiveState {
        return privateSharedInstance
    }
    @objc var color: UIColor? = .red
}

In my Objective-C class I have included the Swift header (and the Singleton and its "shared" method is declared in this file):

#import "SketchWorkshop-Swift.h"

and I refer to the static shared instance in a method:

- (void) doSomething:(id)sender
{
        [self.colorPickerController setInitialColor:[MZAActiveState shared].color];

}

I get no compiler warnings or build errors, but the color does not get set. If I place a breakpoint on the line and type:

(lldb) po [MZAActiveState shared].color;

I get this:

error: use of undeclared identifier ‘MZAActiveState'

I would expect to be able to see [MZAActiveState shared], since it is static and exposed (I believe) to Objective-C. What am I missing?

These links did not answer the question:

Related Question,

Another Related Question,

Here's a Third

Mozahler
  • 4,958
  • 6
  • 36
  • 56

1 Answers1

1

The problem is that you have a nil color

@objc var color: UIColor?

to

@objc var color: UIColor = UIColor.red

It's worth saying that objective-c doesn't respond to nil settings and doesn't crash at all

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Thanks for responding. I assigned a value to the color in the initializer, but still get the same results. The code I posted is a simplified extract. I'll flesh out my example after trying a few more things. – Mozahler Jul 12 '18 at 15:23
  • I have created this demo https://github.com/ShKhan9/ObjcSwift it does the same job – Shehata Gamal Jul 12 '18 at 15:55
  • 1
    Thank you. You have proved that my problem lies elsewhere. Upvoted and accepted. – Mozahler Jul 12 '18 at 16:04