エンコードされたNSStringのは遺骨をNSStringの「ノーマル」、無効になります

StackOverflow https://stackoverflow.com/questions/2542871

  •  23-09-2019
  •  | 
  •  

質問

私は、エンコードされた文字を含む文字列での問題に実行していますよ。具体的には、文字列がエンコードされた文字を持っている場合、それは最終的に「正常な」文字列中に無効になりますではない。

.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;
}

そうでなければ、あなたの文字列を別の場所に自動解放されます。

ドゥretain特性のためのありませんを使用NSString。使用copyます:

@property (nonatomic, copy) NSString *sourceFolder;

などクリス・ハンソンの応答として、これはさらに説明ここではいくつかの質問/回答は、であります。

NSStringのプロパティ:?コピーまたは保持する

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top