Cannot configure RestKit with CoreData - ios

I did setup my project to use RestKit and CoreData: added CoreData framework in 'Link binary with libraries' and "pod 'RestKit', '~> 0.24.0'" to my Podfile.
Everything looked fine until I tried to use RKObjectManager's method
managedObjectRequestOperationWithRequest:managedObjectContext:success:failure:
Xcode gives me the following error:
No visible #interface for 'RKObjectManager' declares the selector 'managedObjectRequestOperationWithRequest:managedObjectContext:success:failure:'
There is a #ifdef RKCoreDataIncluded preprocessor directive before this method in RKObjectManager.h, and RKCoreDataIncluded is defined this way:
#ifdef _COREDATADEFINES_H
#if __has_include("RKCoreData.h")
#define RKCoreDataIncluded
#import "RKManagedObjectStore.h"
#import "RKManagedObjectRequestOperation.h"
#endif
#endif
I can't figure out what else do I need to do to enable CoreData in RestKit?

As it turns out in order to use RestKit with CoreData you have to create precompiled header file for your project and add the following line:
#import <CoreData/CoreData.h>
This issue is discussed here: https://github.com/RestKit/RestKit/issues/1564

At compile time, RestKit expects that Core Data has already been imported. Therefore, simply import Core Data before RestKit:
#import <CoreData/CoreData.h>
#import <RestKit/RestKit.h>
There's no need to create a precompiled header, which would create a dependency between the libraries and every single class in your target.

Related

Objective-c class exposed via bridging header not visible from swift when archiving release

In my react native project, I try to expose a swift function to the javascript code. It works well in debug mode, but when trying to archiving I get a Use of undeclared type RCTEventEmitter error on my swift file.
The RCTEventEmitter is imported in my RNUtils.m file, and in the bridging header.
So I guess somewhere in the archiving, something fail with the bridging header, but I have no idea where.
If I ask for a non existing import, it will tell me that it can't compile the bridging header, so the file is taken into account during the build.
// Utils.m
#import "React/RCTBridgeModule.h"
#import "React/RCTConvert.h"
#import "React/RCTEventEmitter.h"
#interface RCT_EXTERN_REMAP_MODULE(RNUtils, RNUtils, RCTEventEmitter)
#end
// Utils.swift
import Foundation
#objc(RNUtils)
class RNUtils: RCTEventEmitter {
// my native code emitting events
}
// MyApp-Bridging-header.h
#ifndef MyApp_Bridging_Header_h
#define MyApp_Bridging_Header_h
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#endif
react-native 0.61.5
xcode 11.5
Swift 5
It appears I needed to add ìmport React` in every swift file. It's archiving fine now.

Swift framework - Use Swift class reference in Objective-C class

I am creating Swift framework in which I have to use Objective-C class. So I went through this link. This is the public header of my framework :
#import <UIKit/UIKit.h>
//! Project version number for Test.
FOUNDATION_EXPORT double TestVersionNumber;
//! Project version string for Test.
FOUNDATION_EXPORT const unsigned char TestVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <Test/PublicHeader.h>
#import <arpa/inet.h>
#import <ifaddrs.h>
#import <netdb.h>
#import <sys/socket.h>
#import <MyTest/MPAppDelegateProxy.h>
Now in class MPAppDelegateProxy, I have to use a Swift class which I have created. This is :
#import "MPAppDelegateProxy.h"
#import "MyTest.h"
#implementation MPAppDelegateProxy
+ (void)proxyAppDelegate {
[MPGlobal MPLog:#"App delegate not set, unable to perform automatic setup." file:#"MPAppDelegateProxy.m" function:#"proxyAppDelegate" line:32];
// rest of code
}
MPGlobal is one of my Swift class. But I am getting :
Use of undeclared identifier 'MPGlobal'
Note : I have added #objC before MPGlobal.
You need to import <Target>-Swift.h file.
This is known as Objective-C Generated Interface Header Name.
You can find it in your Target's build settings.
This file is auto generated by compiler and it needs to be imported in Objective-C files.
change the SWIFT_OBJC_INTERFACE_HEADER_NAME build setting and making it the same across different targets. To do so change the instruction that generates this property from $(SWIFT_MODULE_NAME)-Swift.h to $(PROJECT_NAME)-Swift.h as explained here
After doing this Clean Build Folder by pressing Alt and going into Product menu. Since name of header is shared among targets now it can be imported once in the .m ObjectiveC file and all targets can benefit from Swift classes.
If after building it still shows the error, ensure that the header can be reached from XCode by Cmd clicking on its name. It should open a file that contains code similar to this:
SWIFT_CLASS("_TtC27ProjectName_Summary11MyClass")
#interface MyClass : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
#end
If need to ensure that those headers are being generated open a terminal and use this command
find ~/Library/Developer/Xcode/DerivedData -name "*Swift.h"
You should see one header for each target
Another issue that happened to me after those changes is that it started giving errors on ObjectiveC code that I didn't touch. The problem was due to the position of the import, as reported here:
Exactly where at the top of a .m file you #import the hidden bridging
header can make a difference. The usual sign of trouble is that you
get an “Unknown type name” compile error, where the unknown type is a
class declared in Objective-C. The solution is to #import the .h file
containing the declaration for the unknown type in your Objective-C
files as well, before you #import the hidden bridging header. Having
to do this can be an annoyance, especially if the Objective-C file in
question has no need to know about this class, but it resolves the
issue and allows compilation to proceed.
At the very end the code compiles and runs on device and simulator!
Original answer
Also you can try this,
You needed to import the -Swift.h for for both the framework and the app target
For Example :
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <Foundation/Foundation.h>
#import "XLPagerTabStrip-Swift.h"
#import "RealmSwift-Swift.h"
...... // Add all frameworks, subclasses, and dependance ios frameworks
#import "MyProject-Swift.h"
You can read this article How to import file header and check paths

Project-Swift.h file not found

I have a new Swift project with a few files, I've needed to add some Objc code.
In Build Settings, my Objective-C Generated Interface Header Name is MyProject-Swift.h
Product Module Name and Product Name are both MyProject.
My Objective-C Bridging Header is MyProject/MyProject-Bridging-Header.h
The contents of my Bridging Header are:
#ifndef MyProject_Bridging_Header_h
#define MyProject_Bridging_Header_h
#import "Blakey.h"
#endif
Blakey.h is pretty simple:
#import Foundation;
#import "MyProject-Swift.h"
#class KeyPair;
#interface Blakey: NSObject
- (void)createKeyPairForSeed:(NSString *)seed;
#end
And Blakey.m
#import <Foundation/Foundation.h>
#import "Blakey.h"
#implementation Blakey
- (void)createKeyPairForSeed:(NSString *)seed;
{
}
#end
(side note: I'm aware my function returns a void, that will be changed later once this issue is fixed so it returns an actual value)
Why is Xcode throwing an error at the #import "MyProject-Swift.h" in Blakey.h?
Project-Swift.h is a file auto generated by Xcode on successful compilation of the project. Catch here is the word successful compilation If your project has any compilation error Project-Swift.h file will not be generated. So in a way it becomes a deadlock. Bestway comment out all the lines that have compilation error and then manage to get it compile without any errors. Only after that Project-Swift.h will be generated.
Additional information, Once the Project-Swift.h file is generated if you open it and if you happened to see that your swift class is not imported there thats because Project-Swift.h imports only the classes that extends from NSObject So plain Swift classes will not be imported.
ISSUE:
You need to import Project-Swift.h in .m file and not .h file. So modify your Blakey as
#import <Foundation/Foundation.h>
#import "Blakey.h"
#import "MyProject-Swift.h"
#implementation Blakey
- (void)createKeyPairForSeed:(NSString *)seed;
{
}
Finally remove #import "MyProject-Swift.h" from Blakey.h
#import Foundation;
#class KeyPair;
#interface Blakey: NSObject
- (void)createKeyPairForSeed:(NSString *)seed;
#end
I had similar issue and almost ended up spending a whole day trying to figure out what wrong with my app.
So following the solution that's helped me :
Clear derived data
Create a class in swift with prefix of #objc for example #objc class mySwiftClass{...}
Build the project again
Et voila.. Should work now.
Why to add #objc?
this #objc prefix, tells the compiler to generate to your swift class a header file. it will add it to the "MyModule-Swift.h" file
I realize this is an old thread, but I had similar issues after adding a new target to a project. I solved it by adding a preprocessor macro (Build Settings -> Apple Clang - Preprocessing) only in said target and then importing the Swift.h file conditionally, like this:
#if DEV_VERSION
#import "Project_DEV-Swift.h"
#else
#import "Project-Swift.h"
#endif
My main target is called Project and the new target is Project DEV (the space is replaced with an underscore in the import), and the preprocessor macro is called DEV_VERSION.
After doing this, both targets build just fine.
<product_name>-Swift.h file not found
It is a kind of bridge(adapter) between Swift and Objective-C. This file contains Swift's API for Objective-C which was marked [#objc and #objcMembers].
You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file.
The header's name is generated from a <product_name>-Swift.h
[Mixing Objective-C and Swift ]
I had a similar issue whereby it would have this issue for anything other than live.
I resolved the issue by hardcoding "Product module name" & "Product name" to my project name. This avoids the need to have preprocessor logic in every file that includes swift code as demonstrated in Pauli Kettunen's solution.

Xmppframework could not build module libxml

On DDXMLNode.h
My project import XMPPFramework by cocoapods ,like:
pod 'XMPPFramework', '~> 3.6.6'
I find some way like:
1.header search paths add "$(SDKROOT)/usr/include/libxml2"
2.Other Linker Flags add "-lxml2"
3.Framework Search Path add "/usr/lib/libxml2.dylib"
All not working!
Instead of changing path of the files, you can do what I have done to resolve my error in Xcode 7.2 beta. First check if you have file libxml2.tbd framework and libresolve.tbd framework; if not then add it to you project and replace few things:
#if !(TARGET_IPHONE_SIMULATOR)
//#import dnssd;
#import <dns_sd.h>
#else
//#import dnssdSimu;
#import <dns_sd.h>
#endif
Second thing you have to do is replace this one:
#if !(TARGET_IPHONE_SIMULATOR)
//#import libxml;
#import <libxml/tree.h>
#else
//#import libxmlSimu;
#import <libxml/tree.h>
#endif
This resolved my problem.
Ok, I'll say get down to business. I use Cocoapods at the beginning, everything goes well and do not configure something, but because of the need to modify the source code to meet certain requirements, so manually add XMPPFramework, but encountered all sorts of issues.
The First : Modlue 'libxmlSimu' not found
In order to solve this problem, I did two things:
Download the file https://github.com/robbiehanson/KissXML/blob/master/libxml/module.modulemap
and import to your project. Like this:
Configure Build Settings - Header Search Paths, like this:
The Second : Module 'KissXml' not found
By replace #import KissXml; to #import "DDXML.h"

Using RestKit in Swift

I have successfully added 'RestKit' pod into my project and also followed steps described in this article: Cocoapods in swift. I imported the RestKit into my ViewController class's source file:
import RestKit
But I don't know why I can't access it. It doen't show any of the classes in the RestKit. It shows:
'RestKit' No such module exists.
I tried again creating a new project and added 'RestKit'. and created a bridging header and imported 'RestKit' into ViewController claas's source file. Now i run the project
it shows errors like
// in this file Pods-SOCKit-prefix.pch
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#endif
#import "Pods-environment.h" // Pods-environment.h file not found
// and also in this file Pods-TransitionKit-prefix.pch
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#endif
#import "Pods-environment.h" // Pods-environment.h file not found
If someone knows what's the problem behind this, please explain it to me, and suggest a way to work with RestKit in Swift.
I'm not sure what the Cocoapods problem you are having. I added RestKit to my bridging header and didn't need to add import RestKit to my Swift file.
My bridging header looks like the following:
# if you are using Core Data:
#import <CoreData/CoreData.h>
#import <RestKit/RestKit.h>
# if you are using Core Data:
#import <RestKit/CoreData.h>

Resources