我正在与含有编码字符的字符串的问题。具体而言,如果字符串已编码的字符它最终将变为无效,而“正常”串将不

在h文件:

@interface DirViewController : TTThumbsViewController 
<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    NSString *sourceFolder;
    NSString *encodedSourceFolder;
}

@property (nonatomic, retain) NSString *sourceFolder;
@property (nonatomic, retain) NSString *encodedSourceFolder;

在.m文件:

- (id)initWithFolder:(NSString*)folder query:(NSDictionary*)query {

    if (self = [super init]) {

        sourceFolder = folder;

    }

  return self;
}

到现在为止似乎一切都按预期运行。在viewDidLoad中我有以下:

sourceFolderCopy = [self urlEncodeValue:(sourceFolder)];

//I also have this button, which I'll refer to later:
UIBarButtonItem *importButton = [[UIBarButtonItem alloc] initWithTitle:@"Import/Export" style:UIBarButtonItemStyleBordered 
                                                                target:self
                                                                action:@selector(importFiles:)];
self.navigationItem.rightBarButtonItem = importButton;

,它使用下面的方法来字符串编码(如果它有欲编码的字符):

- (NSString *)urlEncodeValue:(NSString *)str { 

    NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes (kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8); 

    return [result autorelease]; 

}

如果我的NSLog结果,我得到的预期值。如果字符串像一个空格字符,我得到一个字符串与编码。如果字符串没有任何字符需要编码,它只是给我原始的字符串。

我有通过打开动作片开头的图像导入过程的导航栏上的按钮。一旦动作片开始的方法,我的字符串是无效的 - 但只有当它包含编码的字符。如果它只是一个“正常”的字符串,一切都很好,并如预期的行为。我是关在我的编码?起初,我认为这可能是内存的问题,但我想不出为什么这样做只会影响编码字符串。

这里的动作片被定义,其中(与首先我可以看到编码的字符串成为无效)的NSLog的语句是其中它崩溃:

- (IBAction)importFiles:(id)sender {

NSLog(@"logging encodedSF from import files:");
NSLog(@"%@",encodedSourceFolder);//crashes right here
NSLog(@"%@",sourceFolder);

if (shouldNavigate == NO)
{
    NSString *msg = nil;
    msg = @"It is not possible to import or export images while in image selection mode.";

    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:@"Unable to Import/Export" 
                          message:msg 
                          delegate:self 
                          cancelButtonTitle:@"OK" 
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    [msg release];
}   

else{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:@"What would you like to do?" 
                                  delegate:self 
                                  cancelButtonTitle:@"Cancel"
                                  destructiveButtonTitle:nil 

                                  otherButtonTitles:@"Import Photos (Picker)", @"Export Photos", nil, nil];

    [actionSheet showInView:self.view];
    [actionSheet release];
  }
}

我没有得到任何崩溃的错误去到控制台。通过使用断点我能够看到encodedSourceFolder是在动作片材的方法无效。

没有正确的解决方案

其他提示

您应该复制您的文件夹中的字符串传递您的initWithFolder:查询:法这样或创建一个新的字符串:

- (id)initWithFolder:(NSString*)folder query:(NSDictionary*)query {

    if (self = [super init]) {

        sourceFolder = [folder copy];

    }

    return self;
}

否则,你的字符串被自动释放的其他地方。

待办事项的使用retainNSString性质。使用copy

@property (nonatomic, copy) NSString *sourceFolder;

有几个问题/答案在这里解释这个进一步,比如克里斯·汉森的回应时:

的NSString属性:复制或保留

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top