微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/c/116528.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐