![](/img/trans.png)
[英]Is there any way to load const unsigned char from plist in iOS?
[英]XCode IOS method input from const unsigned char for an array
当前,我有许多实例方法来生成一些数据,对于要传递给我的输入的单个方法,我想更改这些数据,编译器告诉我数组初始化程序必须是初始化程序列表或字符串文字。
我将字符串传递给这样的方法:-
[self buildrawdata2:(const unsigned char *)"0ORANGE\0"];
这是在数组使用设置为“ 0ORANGE \\ 0”的字符串时使用的方法,我传递过来的字符串也从最后开始缺少“ \\ 0”,我相信这是因为其控制字符/转义序列,无论如何有保留它并像下面硬编码的字符串一样传递它:-
- (void)buildrawdata2:(const unsigned char *)inputString2;
{
NSLog(@"ViewController::buildrawdata2");
NSLog(@"ViewController::buildrawdata2 - inputstring2: %s", inputString2);
//this works when set like this
const unsigned char magic2[] = "0ORANGE\0";
const uint8_t pattern1 = {0xFC};
const uint8_t pattern2 = {0xE0};
uint8_t rawdata2[56];
uint8_t index = 0;
int byte = 0;
int bit = 0;
while (magic2[byte] != 0x00) {
while (bit < 8) {
if (magic2[byte] & (1<<bit)) {
//add pattern2 to the array
rawdata2[index++] = pattern2;
}else{
//add pattern1 to the array
rawdata2[index++] = pattern1;
}
// next bit please
bit++;
}
//next byte please
byte++;
//reset bit index
bit = 0;
}
NSLog(@"buildrawdata2::RawData %@", [NSData dataWithBytes:rawdata2 length:56]);
}
看来我已经解决了这个问题,很高兴听到其他人对此方法的看法或改进建议。
我没有使用传递给方法的字符串来尝试直接更新数组初始化程序,而是使用字符串来确定应使用哪个数组初始化程序。 为此,我必须在if块之前创建一个指针,以便可以从if块内为其分配字符串。
const unsigned char *magic = NULL;
if (inputString == @"0APPLES") { magic = (const unsigned char*) "0APPLES\0";}
else if (inputString == @"0ORANGE") { magic = (const unsigned char*) "0ORANGE\0";}
最近也尝试过这种方式,它也可以工作:-
const unsigned char apples[] = "0APPLES\0";
const unsigned char orange[] = "0ORANGE\0";
const unsigned char *magic;
if (inputString2 == @"0APPLES") { magic = apples;}
else if (inputString2 == @"0ORANGE") { magic = orange;}
然后可以这样调用该方法:
[self buildrawdata1:@"0APPLES"];
[self buildrawdata1:@"0ORANGE"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.