Reputation: 1
I am having project in XCode version: 15.4 using Swift for iOS development -> uiKit. I have added the c++ class to my project.
MyStaticClass.h:
#ifndef MYSTATICCLASS_H
#define MYSTATICCLASS_H
class MyStaticClass {
public:
static bool isEven(int number);
};
#endif
MyStaticClass.cpp
#include "MyStaticClass.h"
#include <iostream>
bool MyStaticClass::isEven(int number) {return (number % 2 == 0);}
Then i created wrapper for this class in objective C as
// MyStaticClass_Wrapper.h
#import <Foundation/Foundation.h>
@interface MyStaticClassWrapper : NSObject
+ (BOOL)isEven:(int)number;
@end
// MyStaticClass_Wrapper.mm
#import <Foundation/Foundation.h>
#import "MyStaticClass_Wrapper.h"
#include "MyStaticClass.h"
@implementation MyStaticClassWrapper
+ (BOOL)isEven:(int)number {
// Uncomment the line below to use the C++ implementation
// return MyStaticClass::isEven(number);
return true;
}
@end
**Note: ** The objective file is .mm not .m
I added the bridging Header file as:
#import "MyStaticClass_Wrapper.h"
and i added its path in Build Setting -> Objective-C Bridging Header
Added PrefixHeader.pch:
// PrefixHeader.pch
//#import <Foundation/Foundation.h>
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#import <Availability.h>
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#endif
typedef enum{
bFileTypeDefault = 1,
bFileTypePhoto,
bFileTypeVideo,
bFileTypeAudio,
bFileTypeDocuments,
} baseFileType;
BOOL \_isFakeAccount;
#endif
then added the prefix header path to Build Setting -> Prefix Header in setting.
when i build it:
Error : Unnamed type used 'BOOL'
when i uncomment "#import <Foundation/Foundation.h>"
Error : Along with Unnamed type used 'BOOL' .... 'NSString' also encounters same error
How to resolve it, am stucked with this error.
i tried commenting the BOOl, creating .m file to declare global variable seperately. Didn't worked. When i comment .pch file content it works fine.
Upvotes: 0
Views: 46
Reputation: 2947
The .pch file is prepended to every file you compile (c, c++, or objc) for that target, so if you are importing an ObjC header, it should be inside the #ifdef __OBJC__
section. Otherwise the import will be unknown to non-objc files being compiled. Foundation/Foundation.h is an ObjC specific header.
Upvotes: 0