我正在尝试将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];
我输了,请帮帮我.
解决方法
如果您在后台线程中执行此操作,请添加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];
此外,您需要在完成输入后释放输入,或者将其自动释放.