【问题标题】:Losing bytes when file is written写入文件时丢失字节
【发布时间】:2013-09-12 11:48:02
【问题描述】:

我正在使用以下代码将 NSData 对象写入文件:

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"cert" ofType:@"p12"];
NSData *data = [NSData dataFromBase64String:cert];
NSLog(@"Data length is %i", [data length]);
[data writeToFile:thePath atomically:YES]; //doesn't matter atomically YES or NO, same result

然后打印出来

数据长度为 3078

但是,当我读取检索到的文件时,我丢失了 70 个字节!

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"cert" ofType:@"p12"];
NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
NSLog(@"PKCS12Data length is %i", [PKCS12Data length]);

打印出来的:

PKCS12数据长度为3008

那么这里发生了什么?还有其他方法可以创建包含所有数据的文件吗?

【问题讨论】:

    标签: ios ssl-certificate nsdata nsbundle


    【解决方案1】:

    您无法写入应用程序包,因为它是只读的。将您的 cert.p12 文件写入 Documents 文件夹。 (我猜您的应用程序包中已经有一个 cert.p12 文件,其大小为 3008。您是否检查了 writeToFile 的 BOOL 返回:?)

    【讨论】:

    • 将此标记为正确答案,因为现在两个 NSData 对象的大小相同。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多