问题
I'm new in Swift and I would like if someone could tell me how can I use SNMP++ lib (written in C++) in a simple iOS application written in Swift? how to import the lib and begin coding? Thank You.
回答1:
Objective-C can be mixed with C++. That's called Objective-C++ (.mm files)
So if you can get the code into those files, in theory you can create a bridging header to deal with linking your swift files: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
回答2:
Swift 2.2 does not work with C++, and neither will Swift 3. So it's not coming any time soon which means you simply can't develop a Swift app with a C++ library.
However Objective-C works with C++, so although you don't have to write your entire app in Objective-C, any code that deals with a C++ library will have to be written in Objective-C (or in C...). You can then link your Objective-C code to you Swift app with a bridging header.
回答3:
In 2016, Dariusz Stojaczyk wrote C code to construct probes and deconstruct response of SNMP v1 messages hidden in a project focused on BER. That project contains the code necessary to take one or more SNMP OIDs and construct a full SNMP message, then take a response and decode it, but has no networking code to actually do this.
I took this code and added an ObjectiveC wrapper that can be used to use the existing code to construct a SNMPv1 message, send it to an endpoint using a Socket, and receive a response with a user settable timeout using a dispatch block.
This code is far simpler than any of the big libraries that offer additional services like walking, v2/v3, and traps. If all you want to do is read one or more OIDs, then you should appreciate this code.
来源:https://stackoverflow.com/questions/37146824/using-snmp-library-in-application-written-in-swift