gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:40:17 25 4
gpt4 key购买 nike

我正在尝试将 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];

此外,您需要在完成后释放 input,或者使其自动释放。

关于objective-c - 将 const char* 转换为 NSString * 并转换回来 - _NSAutoreleaseNoPool(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10284011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com