字符串到字符串 EXC_BAD_ACCESS 为什么?
我错过了什么?
NSString * configPath = nil;
-(IBAction)setPlistPathAndWriteData:(id)sender{
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setDirectory:@"/Volumes/"];
[panel setNameFieldStringValue:@"config.plist"];
[panel setRequiredFileType:@"plist"];
NSInteger ret = [panel runModal];
if ( ret == NSFileHandlingPanelOKButton ) {
NSString *filePath= [[panel URL] path];
// with this works fine
//configPath = [NSString stringWithFormat:@"/Volumes/Macintosh HD/config.plist"];
// with this EXC BAD ACCESS
configPath = [NSString stringWithFormat:@"%@", filePath];
[self writeData];
}
}
-(void)writeData {
SET_TEMP_PLIST
NSTask *task = [[NSTask alloc] init];
NSPipe *pipe = [[NSPipe alloc] init];
NSFileHandle *writeHandle = [pipe fileHandleForWriting];
NSData *configData = [NSPropertyListSerialization dataFromPropertyList:tmpPlist format:
NSPropertyListXMLFormat_v1_0 errorDescription:nil];
[task setLaunchPath:@"/usr/libexec/authopen"];
////////////////////////////////////////////////////////EXC_BAD_ACCESS HERE////////
[task setArguments:[NSArray arrayWithObjects:@"-c", @"-w", configPath, nil]];
[task setStandardInput:pipe];
[writeHandle writeData:configData];
[task launch];
close([writeHandle fileDescriptor]);
[task waitUntilExit];
[task release];
}
编辑
好吧...可以很好地使用此代码:
NSString *filePath= [[[panel URL] path] retain];
const char * cString = [filePath UTF8String];
configPath = [[NSString stringWithUTF8String:cString] retain];
但这不是完美的方法..认为
whats I missed?
NSString * configPath = nil;
-(IBAction)setPlistPathAndWriteData:(id)sender{
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setDirectory:@"/Volumes/"];
[panel setNameFieldStringValue:@"config.plist"];
[panel setRequiredFileType:@"plist"];
NSInteger ret = [panel runModal];
if ( ret == NSFileHandlingPanelOKButton ) {
NSString *filePath= [[panel URL] path];
// with this works fine
//configPath = [NSString stringWithFormat:@"/Volumes/Macintosh HD/config.plist"];
// with this EXC BAD ACCESS
configPath = [NSString stringWithFormat:@"%@", filePath];
[self writeData];
}
}
-(void)writeData {
SET_TEMP_PLIST
NSTask *task = [[NSTask alloc] init];
NSPipe *pipe = [[NSPipe alloc] init];
NSFileHandle *writeHandle = [pipe fileHandleForWriting];
NSData *configData = [NSPropertyListSerialization dataFromPropertyList:tmpPlist format:
NSPropertyListXMLFormat_v1_0 errorDescription:nil];
[task setLaunchPath:@"/usr/libexec/authopen"];
////////////////////////////////////////////////////////EXC_BAD_ACCESS HERE////////
[task setArguments:[NSArray arrayWithObjects:@"-c", @"-w", configPath, nil]];
[task setStandardInput:pipe];
[writeHandle writeData:configData];
[task launch];
close([writeHandle fileDescriptor]);
[task waitUntilExit];
[task release];
}
EDIT
well... works fine with this code:
NSString *filePath= [[[panel URL] path] retain];
const char * cString = [filePath UTF8String];
configPath = [[NSString stringWithUTF8String:cString] retain];
but this is not perfect method.. thought
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的应用程序崩溃可能是因为
configPath
为nil
,稍后在writeData
中您尝试使用nil
初始化一个新数组作为第三个对象:我建议您复制
path
返回的字符串:这可能运行没有任何问题。
Your app crashes likely because
configPath
isnil
, and later inwriteData
you try to initialize a new array withnil
as the third object :I suggest you copy the string returned by
path
:This may run without any problem.