I am trying to use a C++ library named MP4v2 in Swift. It is mostly working in that I can can call some functions, use some classes, etc.
我试图在Swift中使用名为MP4v2的C ++库。它主要是工作,我可以调用一些函数,使用一些类,等等。
I am having trouble with a particular function that returns a void pointer. It is NULL
on failure, or some other value on success. There is a constant defined to check with, but neither that nor checking for nil works.
我遇到一个返回void指针的特定函数的问题。失败时为NULL,或成功时为其他值。有一个常量定义来检查,但既不检查也不检查nil是否有效。
if file != MP4_INVALID_FILE_HANDLE {
throws /<path_to_project>/main.swift:19:12: Use of unresolved identifier 'MP4_INVALID_FILE_HANDLE'
, but it is DOES exist (other constants work).
throws /
if file != NULL
just causes the same problem, and if file != nil
never is true, even if the function failed. What am I doing wrong?
if file!= NULL只会导致同样的问题,如果file!= nil永远不是真的,即使函数失败了。我究竟做错了什么?
1 个解决方案
#1
1
Looking at MP4v2 documentation, here is the definition of the macro to check for invalid handle:
查看MP4v2文档,这里是检查无效句柄的宏的定义:
#define MP4_INVALID_FILE_HANDLE ((MP4FileHandle)NULL)
The reason it cannot be used in Swift is because it involves a NULL
. In fact, if you define something like
它无法在Swift中使用的原因是因为它涉及NULL。事实上,如果你定义类似的东西
#define MY_NULL NULL
in your Objective-C(++) code and try to use it in Swift, Swift will suggest that you use nil
instead.
在您的Objective-C(++)代码中尝试在Swift中使用它,Swift会建议您使用nil代替。
The handle type MP4FileHandle
is
句柄类型MP4FileHandle是
typedef void * MP4FileHandle
So, if you are calling a function like
所以,如果你正在调用类似的函数
MP4FileHandle aCPPFunction()
You should be able to check the return value as follows in Swift:
您应该能够在Swift中检查返回值,如下所示:
let h : MP4FileHandle = aCPPFunction()
if h != nil
{
// The handle is valid and can be given as an argument to
// other library functions.
}
else
{
// The handle is NULL
}
I understand you tried this. It should work, please double-check. If for whatever strange reason this doesn't work for you, there are some other options:
我知道你试过这个。它应该工作,请仔细检查。如果由于任何奇怪的原因这对你不起作用,还有其他一些选择:
- Write a simple helper function in C, C++, Objective-C or Objective-C++ to check if the handle is valid and return a integer flag, which should be easily understood by Swift.
- Check
h.hashValue
. If it is 0, then the handle is invalid, otherwise it is valid. This is a bad undocumented hack, but it has worked for me. I would stay away from this one.
用C,C ++,Objective-C或Objective-C ++编写一个简单的辅助函数来检查句柄是否有效并返回一个整数标志,Swift应该很容易理解。
检查h.hashValue。如果为0,则句柄无效,否则有效。这是一个糟糕的无证件黑客,但它对我有用。我会远离这一个。
#1
1
Looking at MP4v2 documentation, here is the definition of the macro to check for invalid handle:
查看MP4v2文档,这里是检查无效句柄的宏的定义:
#define MP4_INVALID_FILE_HANDLE ((MP4FileHandle)NULL)
The reason it cannot be used in Swift is because it involves a NULL
. In fact, if you define something like
它无法在Swift中使用的原因是因为它涉及NULL。事实上,如果你定义类似的东西
#define MY_NULL NULL
in your Objective-C(++) code and try to use it in Swift, Swift will suggest that you use nil
instead.
在您的Objective-C(++)代码中尝试在Swift中使用它,Swift会建议您使用nil代替。
The handle type MP4FileHandle
is
句柄类型MP4FileHandle是
typedef void * MP4FileHandle
So, if you are calling a function like
所以,如果你正在调用类似的函数
MP4FileHandle aCPPFunction()
You should be able to check the return value as follows in Swift:
您应该能够在Swift中检查返回值,如下所示:
let h : MP4FileHandle = aCPPFunction()
if h != nil
{
// The handle is valid and can be given as an argument to
// other library functions.
}
else
{
// The handle is NULL
}
I understand you tried this. It should work, please double-check. If for whatever strange reason this doesn't work for you, there are some other options:
我知道你试过这个。它应该工作,请仔细检查。如果由于任何奇怪的原因这对你不起作用,还有其他一些选择:
- Write a simple helper function in C, C++, Objective-C or Objective-C++ to check if the handle is valid and return a integer flag, which should be easily understood by Swift.
- Check
h.hashValue
. If it is 0, then the handle is invalid, otherwise it is valid. This is a bad undocumented hack, but it has worked for me. I would stay away from this one.
用C,C ++,Objective-C或Objective-C ++编写一个简单的辅助函数来检查句柄是否有效并返回一个整数标志,Swift应该很容易理解。
检查h.hashValue。如果为0,则句柄无效,否则有效。这是一个糟糕的无证件黑客,但它对我有用。我会远离这一个。