7

我正在尝试将 const char* 转换为 NSString * 然后将其转换回来。它有效,但我得到:

__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]; 

我迷路了,请帮帮我。

4

1 回答 1

16

如果您在后台线程中执行此操作,请添加 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在完成后发布,或使其自动发布。

于 2012-04-23T16:00:49.880 回答