I have a project where I write the production code in Swift and my unit tests in Objective-C (because I'm using OCMock I write my tests in Objective-C).
我有一个项目,用Swift编写产品代码,用Objective-C编写单元测试(因为我使用OCMock,所以我用Objective-C编写测试)。
What I'm trying to do is to write a Buffer
object where the writeBytes
method takes a Byte
array and a length, like this:
我要做的是编写一个缓冲区对象,其中writeBytes方法使用一个字节数组和一个长度,如下所示:
func writeBytes(data: [Byte], length: Int)
In my unit test target I want to test this method, but the problem is that it won't convert this method to Objective-C. By looking in my -Swift.h
file I see the following:
在单元测试目标中,我想测试这个方法,但问题是它不会将这个方法转换为Objective-C。通过看我的-斯威夫特。h文件如下:
@interface Buffer : Object
@property (nonatomic) NSInteger capacity;
@property (nonatomic) NSInteger size;
- (instancetype)initWithCapacity:(NSInteger)capacity OBJC_DESIGNATED_INITIALIZER;
- (void)reset;
- (NSInteger)size;
- (BOOL)isFilled;
- (BOOL)isEmpty;
@end
As you can see the writeBytes
method is missing. I've tried different data types to see if it appears: UInt8
, UnsafePointer<UInt8>
, but none of them work.
如您所见,writeBytes方法丢失了。我尝试了不同的数据类型来查看它是否出现:UInt8、UnsafePointer
For the sake of debugging I tried using a String
array and that gets converted to Objective-C without any problems.
为了调试,我尝试使用一个字符串数组,它被转换为Objective-C,没有任何问题。
What data type should I use for my Byte
array to make the writeBytes
method available in Objective-C?
我应该使用什么数据类型的字节数组来使writeBytes方法在Objective-C中可用?
2 个解决方案
#1
3
The Swift type [Byte]
is not representable in Objective-C, but you can work with a pointer to the bytes:
Swift类型[Byte]在Objective-C中不能表示,但是可以使用指向字节的指针:
func writeBytes(data: ConstUnsafePointer<Byte>, length: Int) { ... }
var data : [Byte] = [1, 2, 3]
writeBytes(data, length: data.count)
This mapped to Objective-C as
它映射到Objective-C as
- (void)writeBytes:(Byte const *)data length:(NSInteger)length;
so that you can call it like
你可以这样称呼它
Byte data[] = {1, 2, 3};
[buffer writeBytes:data length:3];
#2
1
Use NSData. You can create one from an array of [UInt8]
in Swift as:
使用NSData。您可以从[UInt8]的数组中创建一个,如:
import Foundation
var u8s = [UInt8]()
// ...
var data = NSData(bytes:&u8s, length:u8s.count)
FYI this is the opposite of Swift - converting from ConstUnsafePointer<()> where the questioner was looking for the way to convert NSData to [UInt8]
.
提示:这与从ConstUnsafePointer<()>到Swift的转换是相反的,在这个转换中,提问者正在寻找将NSData转换为[UInt8]的方法。
#1
3
The Swift type [Byte]
is not representable in Objective-C, but you can work with a pointer to the bytes:
Swift类型[Byte]在Objective-C中不能表示,但是可以使用指向字节的指针:
func writeBytes(data: ConstUnsafePointer<Byte>, length: Int) { ... }
var data : [Byte] = [1, 2, 3]
writeBytes(data, length: data.count)
This mapped to Objective-C as
它映射到Objective-C as
- (void)writeBytes:(Byte const *)data length:(NSInteger)length;
so that you can call it like
你可以这样称呼它
Byte data[] = {1, 2, 3};
[buffer writeBytes:data length:3];
#2
1
Use NSData. You can create one from an array of [UInt8]
in Swift as:
使用NSData。您可以从[UInt8]的数组中创建一个,如:
import Foundation
var u8s = [UInt8]()
// ...
var data = NSData(bytes:&u8s, length:u8s.count)
FYI this is the opposite of Swift - converting from ConstUnsafePointer<()> where the questioner was looking for the way to convert NSData to [UInt8]
.
提示:这与从ConstUnsafePointer<()>到Swift的转换是相反的,在这个转换中,提问者正在寻找将NSData转换为[UInt8]的方法。