2012-04-23 79 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