1

I'm trying to use an open source solution which relies on Availability macros, Objective-C files I added to my Swift project import the necessary header:

#import "Availability.h"
(tried changing "" for <> as well)

I understand that its a part of Foundation framework, which I added in the Linked Frameworks and Libraries section. However, all the Availability-specific lines of code are ignored and I end up having Duplicate interface definition errors, whereas these are supposed to be platform/iOS version-specific definitions.

I've tried adding/removing the framework, Cleaning/Cleaning build folder, as per all the suggestions in this question and others, tried restarting Xcode - nothing helped.

How do I use Availability in Objective-C files imported to Swift project using Bridging header?

Setup: Xcode 7.3

Community
  • 1
  • 1
Alexander Telegin
  • 665
  • 1
  • 7
  • 22

1 Answers1

1

So you have a Swift project, and you added the Apple "Availability.h" from here.

You then want to use these macros in Objective-C or Swift. This is easy enough, but it's important to note you cannot use a C macro in Swift directly. You will need an objective-C wrapper.

//  AvailableTester.h
#import <Foundation/Foundation.h>

@interface AvailableTester : NSObject
- (BOOL) testAvailabilityMacroLessThaniPhone3;
- (BOOL) testAvailabilityMacroGreaterThaniPhone3;
@end

and the implementation:

//  AvailableTester.m

#import "AvailableTester.h"
#import "Availability.h"

@implementation AvailableTester

- (BOOL) testAvailabilityMacroLessThaniPhone3
{
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_3_0
    return YES;
#else
    return NO;
#endif
}

- (BOOL) testAvailabilityMacroGreaterThaniPhone3
{
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_3_0
    return YES;
#else
    return NO;
#endif
}

@end

Then in your bridge header:

#import "AvailableTester.h"

Now you can use it from Swift:

    let test = AvailableTester()
    var result = test.testAvailabilityMacroLessThaniPhone3()
    print("Result1 was \(result)")

    result = test.testAvailabilityMacroGreaterThaniPhone3()
    print("Result2 was \(result)")
David S.
  • 6,567
  • 1
  • 25
  • 45
  • Thanks for your reply! Unfortunately, it's not what I'm looking for, I want to use Available in Objective-C, not Swift. And I can't import it properly to my Objective-C header files, it just doesn't work – Alexander Telegin Aug 15 '16 at 18:45
  • Do you have it in your project? It's not part of Apple's frameworks -- it's a standalone file. Download it from the link and drag it into your project. – David S. Aug 15 '16 at 19:04
  • I was a bit confused here, saw some posts stating it's part of Foundation framework. In the project I'm using, which is building fine but its a pure Objective-C one, the files were located here: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/Availability.h There is a header search path: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include I tried adding it to my project - no luck. Copied Availability file, trying to build project, will get back to you with the results – Alexander Telegin Aug 15 '16 at 19:13
  • Okay, adding the files locally solved the problem of non-functional Availability, and in general yours is a useful answer for Swift users so I'll accept it as a correct one :) My code still not working, though, I'm really fed up with trying to make Objective-C work with Swift properly, the logic is vastly different and I'd rather just rewrite everything in Swift – Alexander Telegin Aug 16 '16 at 15:12