Preventing duplicate entry messages - ios

I have two classes that are pretty much identical but one is made for OS X and the other one is made for iOS. Then I have a header file like this:
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
#import "ClassForOSX.h"
#else
#import "ClassForIOS.h"
#endif
The problem is because both classes are included in the project, xcode compiles both and I have a lot of duplicate entries on the errors, because methods have the same name on both classes.
Is there a way to prevent a class from compiling, even if it is included on the project, unless the class is meant for the target?
Yes I know I can include/exclude classes from targets but I am dealing with hundreds of classes in both sides and multiple targets and that would generate a lot of include/exclude operations that will eventually go wrong. I was wondering if there is some solution in code that can just allow a class to compile if the target is right.

Use targets is best for your case. But You can do it in code
note add or remove __MAC_OS_X_VERSION_MIN_REQUIRED at Preprocessor Macros at target->Build Settings
ClassForIOS.h file
#ifndef __MAC_OS_X_VERSION_MIN_REQUIRED
#import <Foundation/Foundation.h>
#interface ClassForIOS : NSObject
// interface
#end
#endif
ClassForIOS.m file
#ifndef __MAC_OS_X_VERSION_MIN_REQUIRED
#import "ClassForIOS.h"
#implementation ClassForIOS
// implementation code
#end
#endif
ClassForOSX.h file
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
#import <Foundation/Foundation.h>
#interface ClassForOSX : NSObject
// interface
#end
#endif
ClassForOSX.m file
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
#import "ClassForOSX.h"
#implementation ClassForOSX
// implementation code
#end
#endif

Related

Calling Obj-C method from C

I've got a library written in C, where I need to push a call to a method written in Obj-c.
I don't want to modify the original code too much, so I decided to create a "bridge" class to handle the calls between C and ObjC:
DRMBridge.h
#ifndef DRMBridge_h
#define DRMBridge_h
#include "DRMBridgeObjC.h"
void bridge_test();
#endif
DRMBridge.c
#import "DRMBridge.h"
void bridge_test() {
ctest();
}
Above is compiled as C
Now here's my objc code:
DRMBridgeObjC.h
#ifndef DRMBridgeObjC_h
#define DRMBridgeObjC_h
#import <Foundation/Foundation.h>
#interface DRMBridgeObjC : NSObject
+(void) test;
#end
void ctest();
#endif
DRMBridgeObjC.m
#import "DRMBridgeObjC.h"
#implementation DRMBridgeObjC : NSObject
+(void) test {
NSLog(#"OH YEAH!");
}
#end
void ctest() {
[DRMBridgeObjC test];
}
Quite simple.
In the C library I want to call my code from I've added:
#include "DRMBridge.h"
into the .h file and
bridge_test();
in .c file.
Now the best part, when I compile I get:
In file included from /Users/krystian/projects/mdb-reader-lib/reader-Include/ios/MDBReader/Class/DRM/DRMBridge.c:5:
In file included from /Users/krystian/projects/mdb-reader-lib/reader-Include/ios/MDBReader/Class/DRM/DRMBridge.h:8:
In file included from /Users/krystian/projects/mdb-reader-lib/reader-Include/ios/MDBReader/Class/DRM/DRMBridgeObjC.h:12:
In file included from /Applications/Xcode64.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:
/Applications/Xcode64.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:400:1: error: expected identifier or '('
#class NSString, Protocol;
^
[...]
I went looking, and found this: ios - Parse Issues in NSObjCRuntime, NSZone, and NSObject
However my pch file looks like this:
#import <Availability.h>
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
I think the issue I have is that the C compiler tries to compile my ObjC file first, instead of the other way around.
I've tried changing the order of files inside Compile Sources to make sure that my .m file is above .c file, but still no go.
Something else I have found which made me lost:
when I follow this answer: https://stackoverflow.com/a/20461780/487605 and change the library itself to .m, and call [DRMBridgeObjC test] code from there - it works.... Compiles fine, no errors given, works fine.
This implies to me, that there's something screwed up with my DRMBridge, but what?
Thanks
Krystian
You've seen the solution already. Use #ifdef __ OBJC__ in your Objective-C header file so that only the plain C bits are compiled when including the file from C
DRMBridge.h
#ifndef DRMBridge_h
#define DRMBridge_h
#include <CoreFoundation/CoreFoundation.h> //or put in .pch
CF_EXPORT void bridge_test();
#endif
DRMBridge.m
#import "DRMBridge.h"
#import "DRMBridgeObjC.h"
void bridge_test() {
ctest(); //or [DRMBridgeObjC test]; if you like
}
Thats all. And you can include DRMBridge.h in whatever you like: in .c, in .m, in .cpp.

How to conditionally conform to delegate protocol?

If I am including frameworks in my iOS project that are only available in (for example) iOS 9, but I am still supporting iOS 8, how do I conditionally conform to a delegate protocol depending on the iOS version? For example, I understand I can include the framework conditionally like this:
#import <Availability.h>
#ifdef __IPHONE_9_0
#import <Something/Something.h>
#endif
But what if that framework also needs to conform to a delegate protocol?
#interface ExampleController () <UITextViewDelegate, SomethingDelegate>
How do I only include "SomethingDelegate" if I'm on iOS 9?
Thanks!
Well, in roughly the same manner:
#interface ExampleController () <UITextViewDelegate
#ifdef __IPHONE_9_0
, SomethingDelegate
#endif
>
By the way, this is not the way you should check if the device is running iOS 9 - this only checks if your Xcode supports iOS 9.
The answer of #Glorfindel is more clean, and I would support it, but just to have an alternative answer.
#ifdef __IPHONE_9_0
#import <Something/Something.h>
#define DELEGATES UITextViewDelegate, SomethingDelegate
#else
#define DELEGATES UITextViewDelegate
#endif
#interface ExampleController : UIViewController <DELEGATES>
And there is also a question, what are you going to do with methods belonging to SomethingDelegate protocol, also #ifdef/#endif or just keep them "as is", as they never be called.
This is a good task for a category, with its own files. The contents of those files can be entirely ifdefd out.
//ExampleController+SomethingDelegate.h
#ifdef __IPHONE_9_0
#import <Something/Something.h>
#interface ExampleController (SomethingDelegate) <SomethingDelegate>
#end
#endif
//ExampleController+SomethingDelegate.m
#import "ExampleController+SomethingDelegate.h"
#ifdef __IPHONE_9_0
#implementation ExampleController (SomethingDelegate) <SomethingDelegate>
- (BOOL)somethingShouldMakePancakes:(Something *)something;
#end
#endif
This reads much better than splitting the declaration across multiple lines with a macro in the middle, and keeps all the relevant methods in one place, under one ifdef.

Exclude class in target if Macro is set to 1

I defined a preprocessor macro like:
#define ENABLE_SPECIAL_FEATURES 1
So now I am able to include or exclude Code snippets from beeing in the target or not. But I want to have a possibility to do the same with a complete class. So if the macro is set to "1", the class should be included or not.
Is that possible?
UPDATE:
Is the same mechanism possible with frameworks?!
Simply wrap the entire class declaration (.h file) and definition (.m) within that macro value
YourClass.h:
#if ENABLE_SPECIAL_FEATURES
#import <Whatever.h>
#interface YourClass : NSObject
....
#end
#endif // ENABLE_SPECIAL_FEATURES
YourClass.m:
#if ENABLE_SPECIAL_FEATURES
#import "YourClass.h"
#implementation YourClass
....
#end
#endif // ENABLE_SPECIAL_FEATURES
This way you won't even see the class despite the header and implementation file being compiled into the project.

cannot find interface declaration for 'NSObject', superclass of 'GPXType'

I have done some research on that issue , but I have not found anything similar just yet.
I am using iOS GPX framework to draw the path on map using GPX file. I have import iOS GPX.framework on my project. but I have face an issue.
Please Guide me, If anyone has any advice...
Just modify the header file, add this line on top of the file
#import <Foundation/Foundation.h>
Seems they thought that you will have a PCH file, where Foundation and UIKit will be imported, but Xcode 6 removed PCH default support, so the problem came. (See my previous answer)
Finally I have solved my problem
I have import #import < UIKit/UIKit.h> and change my Xcode 6 Architectures $(ARCHS_STANDARD_32_BIT).
Thanks so much Guys.
You haven't imported the header file #import ...
When compiling for both iOS and OsX, I had similar issue that I have resolved by importing TargetConditionals.h. The final thing looks like this:
#import <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#elif TARGET_OS_MAC
#import <Cocoa/Cocoa.h>
#endif
#interface MyClass : NSObject
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
// Define UIKit methods
#elif TARGET_OS_MAC
// Define Cocoa methods
#endif
- (void)reloadRegisteredComponents;
#end

How can I declare a config.h file, add it to my App-Prefix.pch, and then use it globally in my app?

I want to create a Config.h file to house all my static const strings that should be global to my application.
I've created a new Config.h file, but there are a few things I'm unaware of.
1) How do I declare variables. A or B?
A)
#define hotelURLString4 = #"http://blah.herokuapp.com/api/v1/hotels/";
B)
static NSString * const hotelURLString2 = #"http://blah.herokuapp.com/api/v1/hotels/";
2) I can't seem to use this file. If I try to import the Config.h directly into a file of mine, I get a "Config.h file not found" error in xcode. If I include it in my AppName-Prefix.pch up at the top via...
#import <Availability.h>
#import "Config.h"
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
That does not seem to work either. How do I do this.
3) How do I access the variable declared/defined. Do I call Config.hotelURLString, or [Config hotelURLString]... or how do I access it?
===================================================================
============================ UPDATE ===============================
1) I created my header like this... am I not doing something correctly, because I tried again and it won't work either.
2) This is my AppName-Prefix.pch file.
#import <Availability.h>
#import "Config.h"
#import "MyHeader.h"
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
3) Here is my Project Navigator
To answer question 1, method B is probably better, although you should name your variable more like:
kHoteUrlString2
And preferably something more descriptive then just the numbers, but the main point here is the lower case k and the uppercase first letter. This is a C/ObjC naming convention for constants.
As for question 2, it sounds like your file isn't actually in the project. Some more details are needed to answer this part of the question, and I'll update my answer if the question has more details added.
As for question 3, you use the variable exactly as you would as if you had declared it at the top of whatever file you're using. Objective-C doesn't have namespaces.

Resources