编码的 nsstring 变得无效,“正常”保留 nsstring
我遇到了包含编码字符的字符串的问题。具体来说,如果字符串具有编码字符,它最终会变得无效,而“正常”字符串则不会。
在 .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 在操作表方法中无效。
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(":/?#[]@!amp;’()*+,;="), 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该像这样在 initWithFolder:query: 方法中复制传入的文件夹字符串,或者使用以下命令创建一个新字符串:
否则您的字符串会在其他地方自动释放。
You should copy your passed in folder string in your initWithFolder:query: method like this or create a new string with:
Otherwise your string gets autoreleased elsewhere.
不要不要对
NSString
属性使用retain
。使用copy
:这里有几个问题/答案可以进一步解释这一点,例如 Chris Hanson 的回复:
NSString 属性:复制还是保留?
Do not use
retain
forNSString
properties. Usecopy
:There are several questions/answers here that explain this further, such as Chris Hanson's response at:
NSString property: copy or retain?