xcode 如何将 NSData 转换为 const uint8_t?或在通过 Wifi 传递 NSData 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6124317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:06:45  来源:igfitidea点击:

How can i convert NSData to const uint8_t?or getting error while passing a NSData over Wifi

iphoneobjective-cxcodenetwork-programmingwifi

提问by Christina

i have a array with two strings:

我有一个包含两个字符串的数组:

NSArray *sendingArray = [[NSArray alloc]initWithObjects:@"CAT",@"DOG",nil];

and i converted the array to NSData:

我将数组转换为 NSData:

NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:sendingArray];

now i need to convert my NSData(myData)to const uint8_t

现在我需要将我的转换NSData(myData)const uint8_t

i am doing this code to send an array of values through Wifi network.

我正在执行此代码以通过 Wifi 网络发送一组值。

i send successfully a string over wifi using the below code(my string is "string").

我使用以下代码通过 wifi 成功发送了一个字符串(我的字符串是“字符串”)。

 const uint8_t *message = (const uint8_t *)[string UTF8String]; 

    [_outStream write:bytes maxLength:len];

i wrote the below code to send NSData object but it got error.

我写了下面的代码来发送 NSData 对象,但它出错了。

code is:

代码是:

const uint8_t *bytes = (uint8_t)[myData bytes];//this line getting error.
    [_outStream write:bytes maxLength:len];

can i send my NSData(myData)through the above method without converting it to const uint8_t? if yes,please give me the code that i need to write.

我可以NSData(myData)通过上述方法发送我的而不将其转换为 const uint8_t 吗?如果是,请给我我需要编写的代码。

or can any one tell me the way to convert my NSData(myData)to const uint8_t

或者任何人都可以告诉我如何将我的转换NSData(myData)const uint8_t

回答by JeremyP

 const uint8_t *bytes = (const uint8_t*)[myData bytes];
 //                                   ^^ note the asterisk