从C函数返回NSString [英] Return NSString from a C function
问题描述
在iOS开发中,是否可以使用 NSString
并从函数中将其返回?
In iOS development, is it possible to use NSString
and return it from a function?
例如
(NSString * ) foo {
return @"";
}
这不是客观的c方法,只是功能
This is not an objective c method, just function
推荐答案
是的,看到Objective-C对象只是指针,您可以创建一个C函数以返回一个:
Yes, seeing as objective-c objects are just pointers, you can create a C function to return one:
NS_RETURNS_RETAINED NSString *myFunction() {
return [[NSString alloc] init];
}
注意使用 NS_RETURNS_RETAINED
.这是对ARC和静态分析器的提示,该函数将保留值返回给接收器,并且它们有责任释放该值.
Notice the use of NS_RETURNS_RETAINED
. This is a hint to ARC and the static analyzer that this function returns a retained value to the receiver, and that it's their responsibility to release it.
如果返回的是自动释放的值,请尝试使用 NS_RETURNS_NOT_RETAINED
.
If you were returning an autoreleased value, try using NS_RETURNS_NOT_RETAINED
instead.
这篇关于从C函数返回NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!