Question

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:

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

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

in the .m file:

- (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:

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. 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:

- (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.

No correct solution

OTHER TIPS

You should copy your passed in folder string in your initWithFolder:query: method like this or create a new string with:

- (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. Use copy:

@property (nonatomic, copy) NSString *sourceFolder;

There are several questions/answers here that explain this further, such as Chris Hanson's response at:

NSString property: copy or retain?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow