I´m not able to import ifaddrs to my Swift project. The compiler says:
No such module "ifaddrs"
Aim is to get iPhones IP address in the current wifi network at the end. Any suggestions?
Normally under your file ****-Bridging-Header.h (where **** is the name of your project - xcode creates it for you when you include objective c resources) you have to add:
#include <arpa/inet.h>
#include <sys/socket.h>
#include <ifaddrs.h>
After ifaddrs is added to the bridge file, the objective c class can be used in swift.
Related
OBJ-C ONLY...
That is,
An ObjC app imports ObjC static lib A.
Static lib A imports static lib B.
Static lib A has functions that call functions within lib B.
The app only calls functions in lib A and does not call functions in lib B.
Can I assume that lib A or B can be either Obj-C or Swift?
IE. Can an ObjC app import an ObjC-or-Swift static lib A that itself imports a second ObjC-or-Swift static lib B? (yes, 4 use case permutations)
the git repository https://github.com/CombineCppSwiftObjcInStaticLib i created for you is showing this..
your initial #objc func run_central() in BLE_central.swift is exposed, which triggers the precompiler to generate objc compatible headers (bridge) which then again makes it possible to call the function from a method inside .mm(objc++) or .m(objc) when this generated header is imported.
In fact Hub_lib inside the repo is a static ObjC++ lib mixed with Swift. It would work the other way around also. The headers are the key for success here. If you can provide some objc or c or c++ header to swift functions it becomes compatible and wise versa. I mean in general, thats the idea of headers. If you don't have headers, that does not mean you can not call some external stuff, it just means you would call it blind. A proper IDE will complain before you even try to do this evil stuff, unknown entry points aka unknown symbols etc.. So you go for a proper header - always.
To properly combine swift with other languages its good to know there are always two ways of bridging.
In case of Objective-C (and also Objective-C++) it is
Bridging into Swift (projectname-Bridging-Header.h),
and Bridging out of Swift (expose with #objc to trigger automatically internal generation of projectname-Swift.h file. So this header is "invisible" in the file browser on the left side. Nor will you find it in the repo as file, it is named by modulename which is the project-name). The last mentioned header you could even write manually yourself, with lots of troublesome back-draws.
Hint: Executable code is executable code. No matter what language, as far it is compiled for the right device architecture and has symbols to call and you know what to do with the data returned.
Another Hint: there is a way to handle C pointers in swift see docu which become swift datatypes which you can use to go the other way and declare functions to return those from swift.
And direct use of C in Swift is also possible. The compiler considers if you explicit mark some code as C. extern "C" { /* code */ } will cause the C++ compiler to remember, this is still C++ code to compile the function in such a way, it can be called from C (and Swift)
//Example.hpp //no target membership
#ifdef __cplusplus
#include <stdio.h>
class Example {
private:
const char * _name;
public:
Example(const char *name);
~Example(void);
int getLen(void);
};
#endif
There should be an Example.cpp and don't forget to tell Xcode you deal with c++ #ifdef __cplusplus + #endif
//Example.cpp //has target membership
#include "Example.hpp"
#ifdef __cplusplus
#include <stdio.h>
#include <string>
//code implementation according to example.hpp
Example::Example(const char *name) {
_name = name;
}
int Example::getLen() {
return (int)strlen(_name);
}
#endif
//ExampleWrapper.cpp //has target membership
#include "Example.hpp" //c++ header file
extern "C" int myCppFunction(const char *s)
{
// Create an instance of Example, defined in the library
// and call getLen() on it, return result.
return Example(s).getLen();
}
So this function needs to be declared in the bridging header to make use of it.
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
// for extern "C" functions declare them one by one here
// as implemented in ExampleWrapper.cpp
// becomes "func myCppFunction(_ s: UnsafePointer<Int8>!) -> Int32" in swift
int myCppFunction(const char * s);
and then call from swift..
os_log("The result from C++ is %u", myCppFunction("12345"))
So in fact, yes. Integrating a static lib A that calls static lib B in App is possible. Happy compiling as long you offer some header for each part that needs to know what is inside the head of the other lib. That is true for Apps as it is true for libs and frameworks under each other.
Edit here some important stuff to read about Swift Package Manager C support https://github.com/apple/swift-evolution/blob/master/proposals/0038-swiftpm-c-language-targets.md
As long as the libraries export Objective-C compatible symbols, it doesn't matter if they're written in Objective-C, or Swift, or C++, or any other compiled language.
And we know that the Swift compiler exports Objective-C compatible symbols for all declarations that are marked with #objc (either explicitly or implicitly).
From a consumer perspective it doesn't matter which language generated the libraries, as long as the Objective-C compiler/linker can consume the symbols exported by those libraries.
I am using the OpenCV iOS Framework in a project. I followed the necessary steps to include the framework into the project.
The Project is written using Swift 3.
One of my classes that contains the core functionality of the App is written in Objective-C++. I included the header of the class in my Bridge-header file but when trying to run the project I get the following error:
error core.hpp header must be compiled as C++
After researching online and on SO, the most common solution presented was to create a Wrapper class that would be imported in the bridge header. However, after following this article I face the same problem.
The Header file of my class looks like this:
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#define IMAGE_CLASS UIImage
#elif TARGET_OS_MAC
#import <Cocoa/Cocoa.h>
#define IMAGE_CLASS NSImage
#endif
#import <AGGeometryKit/AGKQuad.h>
#import <stdio.h>
#import <opencv2/opencv.hpp>
#import <Foundation/Foundation.h>
typedef void (^DebugBlock)(cv::Mat current_image, std::string current_image_name);
#interface ImageScanner : NSObject
/**
* Singleton for access to the scanner.
*
* #return Shared scanner.
*/
+ (instancetype)sharedScanner;
Does anyone have an idea what I might be missing?
Thank you in advance!
G.
had the same problem.. solved by importing any file that use openCV in the wrapperClass.mm file ...
SO Answer here
when trying to compile some c / c++ code for iOS. Getting
No member named 'memcpy' in namespace 'std::__1'; did you mean 'wmemcpy'?
Have tried the compiler settings with no luck.
This is in the memory file in the tool chain.
I needed to add
#include <cstring>
hope it helps
I am trying to use gettimeofday in Objective-C. However, i get these two issues:
"Implicit declaration of function 'gettimeofday' is invalid in C99"
"Declaration of 'gettimeofday' must be imported from module 'Darwin.POSIX.sys.time' before it is required"
I have performed these imports:
#import <UIKit/UIKit.h>
#include <time.h>
But the issue still persists?
Thanks in advance.
/JBJ
import <sys/time.h> instead of #include <time.h>
in C time.h declares functions like time(), clock() etc; and In POSIX (OS specification) it has all C structure and macro and it is extended with time interval functions in sys/time.h .
I'm trying to use the peertalk framework which has no documentation.
On their obj-c example they use the INADDR_LOOPBACK macro, and example is working.
But when i try to do the same in swift the system throw me an unresolved identifier error.
Anyone knows how to fix it?
http://www.gnu.org/software/libc/manual/html_node/Host-Address-Data-Type.html
Update for Swift 3: As of Swift 3, INADDR_LOOPBACK
is imported into Swift. Therefore it suffices to add
#include <netinet/in.h>
to the bridging header file, but a custom definition is not
needed anymore.
Old answer: For some reason, the macro definition
#define INADDR_LOOPBACK (u_int32_t)0x7f000001
from <netinet/in.h> is not imported into Swift.
The problem might be the (u_int32_t) cast, because
other macros like
#define INADDR_NONE 0xffffffff /* -1 return */
are imported.
One solution is to define
let INADDR_LOOPBACK = UInt32(0x7f000001)
in your Swift code. Alternatively, add
#include <netinet/in.h>
const uint32_t kInAddrLoopback = INADDR_LOOPBACK;
to the bridging header file and use kInAddrLoopback in the Swift code.
This is less error-prone because you don't have to repeat the constant.
From the Apple documentation.
Declare simple macros as global constants, and translate complex
macros into functions.