本文介绍了如果NSData&lt;固定大小在可可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个问题:我使用以下代码从 NSData 获取数据,但我想从 [0; 2048]字节获取数据。如果我的数据> 2048 * 强文本 *,它可以运行正常,但如果我的数据,它会出错。所以我想添加更多的一些空间,如果我的数据< 2048 到足够的 2048字节。你可以帮我吗?非常感谢。I have a problem: I used below codes to get data from NSData, but I want to get data from [0;2048] bytes. If my data > 2048*strong text*, it can run fine but if my data < 2048, it'll wrong. So that I want to add more some space at last if my data < 2048 to enough 2048 bytes. Can you help me? Thanks so much. NSData *data = [NSData dataWithBytes:[arrayText UTF8String] length:[arrayText lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; NSData *datawrite = [data subdataWithRange:NSMakeRange(0, 2048)];如果 data> 2048字节,如果 data ,我的MAC应用程序挂起。请给我任何建议。提前感谢Above code is work fine if data > 2048 bytes, if data < 2048 bytes , my MAC app hangs. Please give me any suggestion. Thanks in advance推荐答案好的,这将从 data 。如果结果是< 2048字节,新数据将填充为2048字节。Ok, this will copy as many bytes as are available (up to 2048) from data. If the result is < 2048 bytes, the new data will be padded to 2048 bytes.NSMutableData * datawrite = [ data subdataWithRange:(NSRange){ 0, MIN( 2048, data.length ) } ] mutableCopy ] ;[ datawrite setLength:2048 ] ;但是,你能告诉我们更广泛的应用吗?这看起来像一个奇怪的用例,我想了解更多。BUT: Can you tell us the broader application? This seems like a strange use case and I'd like to understand more. 这篇关于如果NSData&lt;固定大小在可可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-19 00:08