objective-c – 将const char *转换为NSString *并转换回来 – _NSAutoreleaseNoPool()

前端之家收集整理的这篇文章主要介绍了objective-c – 将const char *转换为NSString *并转换回来 – _NSAutoreleaseNoPool()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将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];

此外,您需要在完成输入后释放输入,或者将其自动释放.

原文链接:https://www.f2er.com/c/116528.html

猜你在找的C&C++相关文章