2012-04-23 97 views
6

我試圖將常量char *轉換爲NSString *,然後將其轉換回來。它的工作原理,但我得到:將const char *轉換爲NSString *並轉換回來_NSAutoreleaseNoPool()

__NSAutoreleaseNoPool(): Object 0x100550a40 of class NSCFArray autoreleased with no pool in place - just leaking 
__NSAutoreleaseNoPool(): Object 0x100551730 of class NSCFString autoreleased with no pool in place - just leaking 
__NSAutoreleaseNoPool(): Object 0x100551f10 of class NSCFData autoreleased with no pool in place - just leaking 

的代碼是:

const char* convert = "hello remove this: *"; 

NSString *input = [[NSString alloc] initWithUTF8String:convert];// convert 

//remove * FROM THE STRING   
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"*"]; 

// REPLACE * WITH NOTHING     
input = [[input componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""]; 

// CONVERT BACK   
const char *converted_back = [input UTF8String]; 

我迷路了,請幫我出。

+0

顯示你所有的代碼,我想我知道問題是什麼,但我需要先檢查你的整個代碼。 – 2012-04-23 16:07:20

回答

15

如果您在後臺線程中執行此操作,請添加一個NSAutoReleasePool。

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
const char* convert = "hello remove this: *"; 
NSString *input = [[[NSString alloc] initWithUTF8String:convert] autorelease];// convert 
//remove * FROM THE STRING   
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"*"]; 
// REPLACE * WITH NOTHING     
input = [[input componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""]; 
// CONVERT BACK   
const char *converted_back = [input UTF8String]; 
[pool drain]; 

此外,您還需要釋放input你用它做後,或使其自動釋放。

+0

我正在做一個頭文件,然後我包括/進口到NSApplicationDelegate源內,你能告訴我一個例子,因爲我是新來的.. – user1341993 2012-04-23 16:02:35

+0

即時得到節目接收信號:「EXC_BAD_ACCESS」。 – user1341993 2012-04-23 16:05:09

+0

@ user1341993 - 請參閱編輯 – MByD 2012-04-23 16:07:22