我对NSString变量有一个问题。
.h文件
NSString *strDeleteFilePath;
@property (nonatomic,retain) NSString* strDeleteFilePath;.m文件
@synthesize strDeleteFilePath;//之后,单击“删除”按钮
-(IBAction)deleteButton:(id)sender {
UIButton *bt=(UIButton *)sender;
strDeleteFilePath=[FunctionManager getDocumentDirectoryPath:@"MyPhotos"];
strDeleteFilePath=[NSString stringWithFormat:@"%@/%@",strDeleteFilePath,[arrSaveImage objectAtIndex:bt.tag]];
NSLog(@"strDeletePath=%@",strDeleteFilePath);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Are you sure you want to delete this photo" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];
}nslog在字符串中打印正确的路径如下:
strDeletePath=/Users/Samir/Library/Application支持/iPhone Simulator/6.0/Applications/A72B7488-ABCB-48EC-91D0-CEE87FA121FE/Documents/MyPhotos/Dt20130411164806.png
当单击警报视图中的“删除”按钮.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
if(![fileManager removeItemAtPath:strDeleteFilePath error:&error]) {
NSLog(@"Delete failed:%@", error);
} else {
NSLog(@"image removed: %@", strDeleteFilePath);
}
[self setScrollviewItem];
}
}它在网上崩溃,如果()并给出以下错误ExE_BAD..ACCESS.


谢谢,提前。
发布于 2013-04-12 05:32:23
使用self. strDeleteFilePath而不是strDeleteFilePath。
发布于 2013-04-12 05:32:32
尝尝这个
-(IBAction)deleteButton:(id)sender {
UIButton *bt=(UIButton *)sender;
strDeleteFilePath=[FunctionManager getDocumentDirectoryPath:@"MyPhotos"];
strDeleteFilePath=[[NSString alloc] initWithFormat:@"%@/%@",strDeleteFilePath,[arrSaveImage objectAtIndex:bt.tag]]; //change is here
NSLog(@"strDeletePath=%@",strDeleteFilePath);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Are you sure you want to delete this photo" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];
}发布于 2013-04-12 06:06:15
代之以:
if(![fileManager removeItemAtPath:strDeleteFilePath error:&error]) {
NSLog(@"Delete failed:%@", error);
} else {
NSLog(@"image removed: %@", strDeleteFilePath);
}通过以下方式:
if([fileManager fileExistsAtPath: strDeleteFilePath])
{
[fileManager removeItemAtPath: strDeleteFilePath error: nil];
}
else{
NSLog(@"File not found");
}https://stackoverflow.com/questions/15963725
复制相似问题