[英]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.