1

我对 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 Support/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];
       }
   }

它在线崩溃 if(![fileManager removeItemAtPath:strDeleteFilePath error:&error]) 并给出以下错误 ExE_BAD..ACCESS...

错误 ExE_Bad.....

先感谢您。

4

3 回答 3

3

使用self. strDeleteFilePath而不是strDeleteFilePath.

于 2013-04-12T05:32:23.090 回答
0

试试这个

-(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-12T05:32:32.620 回答
0

替换这个:

 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");
}
于 2013-04-12T06:06:15.643 回答