将 const char* 转换为 NSString * 并转换回来 - _NSAutoreleaseNoPool() [英] convert const char* to NSString * and convert back - _NSAutoreleaseNoPool()

查看:86
本文介绍了将 const char* 转换为 NSString * 并转换回来 - _NSAutoreleaseNoPool()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 const char* 转换为 NSString *,然后再将其转换回来.它有效,但我明白了:

i'm trying to convert const char* to NSString * and then convert it back. It works, but i get:

__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.

If you are doing it in a background thread, add an 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,或者让它自动释放.

Also, you need to release input after you're done with it, or make it autoreleased.

这篇关于将 const char* 转换为 NSString * 并转换回来 - _NSAutoreleaseNoPool()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆