假设我有C++函数和Objy-C成员。 函数获取 std::string 把它转换成 NSstring* ,并在离开之前使用此变量…
std::string
NSstring*
我应该期待 * 在年底释放 autoreleasepool 范围?
*
autoreleasepool
void myclass::myfunc(context& ctx) { @autoreleasepool { std::string path = ctx.getData().path; NSString *nsPath = [NSString stringWithUTF8String:path.c_str()]; ... (do something with nsString, Should it be released after leaving the scope ?) } }
不,你不需要。根据规则,如果要通过以下方式之一增加变量的保留计数,则只需释放该变量:
如果您通过除上述方式以外的任何方式获取变量,则不拥有该变量,因此不需要释放该变量。
字符串通过返回 [NSString stringWithUTF8String:path.c_str()] 是自动释放的字符串。当前运行循环完成后将释放它。所以你不需要释放它。
[NSString stringWithUTF8String:path.c_str()]