gpt4 book ai didi

iphone - 从文档文件到 NSData

转载 作者:太空狗 更新时间:2023-10-30 03:27:36 26 4
gpt4 key购买 nike

我想从文档目录中抓取一个文件到 NSData 对象中,但是如果我这样做,我的 NSData 总是 NIL:

filepath = [[NSString alloc] init];
filepath = [self.GetDocumentDirectory stringByAppendingPathComponent:fileNameUpload];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:filepath];

-(NSString *)GetDocumentDirectory{
fileMgr = [NSFileManager defaultManager];
homeDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

return homeDir;

在这一点上,我总是得到一个异常,我的数据是NIL:

[request addRequestHeader:@"Md5Hash" value:[data MD5]];

我检查过,没有文件,但我不知道为什么!我之前创建了那个文件:

NSMutableString *xml = [[NSMutableString alloc] initWithString:[xmlWriter toString]];

NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//make a file name to write the data to using the documents directory:
NSMutableString *fileName = [NSMutableString stringWithFormat:@"%@/7-speed-",
documentsDirectory];

[xml writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];

通过以下方式解决:

[xml writeToFile:fileName
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];

最佳答案

检查文件是否存在:

if([[NSFileManager defaultManager] fileExistsAtPath:filepath)
{
NSData *data = [[NSFileManager defaultManager] contentsAtPath:filepath];
}
else
{
NSLog(@"File not exits");
}

关于iphone - 从文档文件到 NSData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13230163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com