0

I added some Obj-C code (the excellent Expressions) to my Swift project using Xcode's Add files... but it did not ask me if I wanted to make a bridging header. So I made one myself in the Obj-C code's group, edited it to #import the single header I needed, and made sure that file was referenced in the Swift Compiler in Build Settings. I then looked through the Obj-C code and made sure the .m files were in the target - they were, and they're listed in Compile Sources.

The header in question contains this:

@interface NSNumber (Expression)
+ (NSNumber *)numberByParsingExpression:(NSString *)expression;
+ (NSNumber *)numberByParsingExpression:(NSString *)expression withVariables:(NSDictionary *)varDictionary;
@end

Now I am trying to call this code using the same basic syntax as this post:

let result = NSNumber.numberByParsingExpression(f.1)

along with several variations on the theme. But it won't compile, "Type 'NSNumber' has no member 'numberByParsingExpression'".

Did I miss a step here?

1

2 Answers 2

2

According to https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

You can create a bridging header yourself by choosing File > New > File > (iOS, watchOS, tvOS, or OS X) > Source > Header File.

You’ll need to edit the bridging header file to expose your Objective-C code to your Swift code. In your Objective-C bridging header file, import every Objective-C header you want to expose to Swift. For example:

In Build Settings, in Swift Compiler - Code Generation, make sure the Objective-C Bridging Header build setting under has a path to the bridging header file. The path should be relative to your project, similar to the way your Info.plist path is specified in Build Settings. In most cases, you should not need to modify this setting. Any public Objective-C headers listed in this bridging header file will be visible to Swift. The Objective-C functionality will be available in any Swift file within that target automatically, without any import statements. Use your custom Objective-C code with the same Swift syntax you use with system classes.

If you already did this correctly, and it still isn't working, try deleting the projects derived data, and clean building your project.

Sign up to request clarification or add additional context in comments.

Comments

1

Ok, this turns out to be an Xcode peccadillo.

When you create the header file within the group, it actually places it physically in the source folder. So in my case the header was created in /project/subproject/.h although it appeared within Xcode to be part of the base folder, /project/.h.

So in fact there were two headers, one in the right place with nothing in it, and another in the wrong place that was the one that was being edited within Xcode. So you have to look at the file inspector to make sure it placed the bridging header in the right place!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.