简体   繁体   English

编码的nsstring变为无效,保留“正常” nsstring

[英]Encoded nsstring becomes invalid, “normal” nsstring remains

I'm running into a problem with a string that contains encoded characters. 我遇到一个包含编码字符的字符串的问题。 Specifically, if the string has encoded characters it eventually becomes invalid while a "normal" string will not. 具体来说,如果字符串已经编码了字符,则最终将变得无效,而“正常”字符串则不会。

in the .h file: 在.h文件中:

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

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

in the .m file: 在.m文件中:

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

    if (self = [super init]) {

        sourceFolder = folder;

    }

  return self;
}

Up to now everything seems to run as expected. 到目前为止,一切似乎都按预期运行。 In viewDidLoad I have the following: 在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;

Which uses the following method to encode the string (if it has characters I want encoded): 它使用以下方法对字符串进行编码(如果它包含我要编码的字符):

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

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

    return [result autorelease]; 

}

If I NSLog result, I get the expected values. 如果我的NSLog结果,我得到预期的值。 If the string has characters like a white space, I get a string with encoding. 如果字符串中有类似空格的字符,我将得到一个带有编码的字符串。 If the string doesn't have any characters that need to be encoded, it just gives me the original string. 如果该字符串没有任何需要编码的字符,它只会给我原始的字符串。

I have a button on the nav bar which begins an image import process by opening an action sheet. 我在导航栏上有一个按钮,该按钮通过打开操作表来开始图像导入过程。 Once the method for the action sheet starts, my string is invalid - but only if it contains encoded characters. 操作表的方法启动后,我的字符串无效-但前提是它包含编码的字符。 If it is just a "normal" string, everything is fine and acts as expected. 如果只是一个“普通”字符串,那么一切都很好,并且可以按预期运行。 Am I off on my encoding? 我会关闭编码吗? At first I thought it might be a memory problem but I can't figure out why that would affect only encoded strings. 起初,我认为这可能是内存问题,但我不知道为什么这只会影响编码的字符串。

Here's where the action sheet is defined (and the first place I can see the encoded string becoming invalid) the NSLog statements are where it crashes: 这是定义操作表的位置(并且我首先可以看到编码的字符串变为无效),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];
  }
}

I don't get any crash errors going to the console. 我没有任何崩溃错误进入控制台。 By using breakpoints I was able to see that the encodedSourceFolder is invalid in the action sheet method. 通过使用断点,我可以看到在操作表方法中encodeSourceFolder无效。

You should copy your passed in folder string in your initWithFolder:query: method like this or create a new string with: 您应该在initWithFolder:query:这样的方法中复制传入的文件夹字符串,或使用以下命令创建新字符串:

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

    if (self = [super init]) {

        sourceFolder = [folder copy];

    }

    return self;
}

Otherwise your string gets autoreleased elsewhere. 否则,您的字符串将在其他地方自动释放。

Do not use retain for NSString properties. 不要使用retainNSString性质。 Use copy : 使用copy

@property (nonatomic, copy) NSString *sourceFolder;

There are several questions/answers here that explain this further, such as Chris Hanson's response at: 这里有几个问题/答案可以进一步解释这一点,例如克里斯·汉森(Chris Hanson)的回应:

NSString property: copy or retain? NSString属性:复制还是保留?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM