简体   繁体   English

Objective C 中的 unsigned long , unsigned char 到 Swift

[英]unsigned long , unsigned char in Objective C to Swift

I am changing my Objective C project to Swift project.我正在将我的 Objective C 项目更改为 Swift 项目。 In objective CI used unsigned long, Unsigned char.在客观 CI 中使用了 unsigned long、Unsigned char。

const char * bytesIn = [self bytes];
unsigned long bytesLength = range.location + range.length;
unsigned long   bytesOffset = range.location;
NSMutableData * dataOut = [NSMutableData data];

BOOL currIsEOF = NO;
unsigned char currChar;         /* current character */
unsigned char charBuf[MAX_READ];    /* buffer of already read characters */
int count;                       /* number of characters in a run */

/* prime the read loop */
currChar = bytesIn[bytesOffset];
bytesOffset = bytesOffset + skip;
count = 0;

The above is the code.上面是代码。 Can Anybody help to change this to a Swift one.任何人都可以帮助将其更改为 Swift 的。 In Swift to my understanding there is no const char据我了解,在 Swift 中没有const char

Your code snippet looks like part of a larger function that is extracting some bytes out of an NSData object.您的代码片段看起来像是从NSData对象中提取一些字节的更大函数的一部分。 Swift providesData which bridges to NSData and provides a whole slew of functions to manipulate byte buffers Swift collections. Swift 提供了连接到NSDataData ,并提供了大量的函数来操作字节缓冲区 Swift 集合。 You should look at the larger function your snippet is from and recast the algorithm using the functions provided by Data .您应该查看您的代码片段来自的更大的函数,并使用Data提供的函数重铸算法。

BTW Swift provides unsigned types, for some reason it views them as Special-Use Numeric Types and also provides type aliases named after C types, see C Interoperability .顺便说一句,Swift 提供无符号类型,出于某种原因,它将它们视为特殊用途数字类型,并且还提供以 C 类型命名的类型别名,请参阅C 互操作性 So if you have algorithms which use unsigned types and/or Objective-C code for them, you can write/translate them into Swift – indeed it seems the result will be "special" to Swift folk when of course unsigned numbers are really natural ;-)因此,如果您有使用无符号类型和/或 Objective-C 代码的算法,您可以将它们编写/翻译成 Swift - 事实上,当无符号数字确实很自然时,结果对于 Swift 人来说似乎是“特殊的”; -)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM