NSString变量不是NSstring

时间:2013-04-12 05:27:39

标签: iphone ios xcode nsstring

我的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   模拟器/ 6.0 /应用/ A72B7488-ABCB-48EC-91D0-CEE87FA121FE /文档/ 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];
       }
   }

如果(![fileManager removeItemAtPath:strDeleteFilePath错误:& error])崩溃就行了,并给出以下错误ExE_BAD..ACCESS ...

error ExE_Bad.....

先谢谢你。

3 个答案:

答案 0 :(得分:3)

使用self. strDeleteFilePath代替strDeleteFilePath

答案 1 :(得分: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];
}

答案 2 :(得分: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");
}