我似乎已经在关于从字符串保存xml文件的问题上发生了一些问题(这是在iPhone上完成的) 文件本身存在并包含在项目中因此在工作区内),以及我从代码片段中得到的所有指示在模拟器上没有任何错误并且在iPhone上失败(错误513),但是在任何情况下都不保存文件!在Objective-C(iPhone)中将字符串保存到文件中
{
Hits = config->Hits;
NSString* filenameStr = [m_FileName stringByAppendingFormat: @".xml" ];
NSString* pData = [self getDataString]; // write xml format - checked out ok
NSError *error;
/* option 2 - does not work as well
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *xmlURL = [NSURL fileURLWithPath:[mainBundle pathForResource: m_FileName ofType: @"xml"]];
if(![pData writeToURL: xmlURL atomically: true encoding:NSUTF8StringEncoding error:&error])
{
NSLog(@"Houston - we have a problem %[email protected]\n",[error localizedFailureReason]);
return false;
}
*/
if(![pData writeToFile: filenameStr atomically: FALSE encoding:NSUTF8StringEncoding error:&error])
{
NSLog(@"Houston - we have a problem %[email protected]\n",[error localizedFailureReason]);
return false;
}
return true;
}
任何帮助,将不胜感激, -A
你可以给字符串解释什么是错误513? 另外,注意:在打印localizedFailureReason时,最好在NSLog语句中使用%@而不是%s。 %@格式说明符需要一个NSString *,而%s需要一个以null结尾的char *字符串。 – Tyler 2009-08-16 09:24:31
从技术上讲,%@使用任何NSObject类型,并调用字符串本身就是其自身的[object description];但它也适用于其他NSObject类型(例如数组,集合等) – AlBlue 2009-08-16 11:17:27