Objective-C 中的引用参数
我试图通过引用传递 NSString
但它不起作用。
这是函数:
+(void)fileName:(NSString *) file
{
file = @"folder_b";
}
这是调用:
NSString *file;
[function fileName:file];
nslog(@"%@",file); // and there is nothing in the string....
我必须做什么才能通过引用传递字符串?
I'm trying to pass an NSString
by reference but it doesn't work.
This is the function:
+(void)fileName:(NSString *) file
{
file = @"folder_b";
}
and this is the call:
NSString *file;
[function fileName:file];
nslog(@"%@",file); // and there is nothing in the string....
What I must do to pass my string by reference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想返回一个值,则返回一个值。 Cocoa/iOS 中的引用传递很大程度上限于
NSError**
。鉴于:
然后做:
然后完成它。
如果您需要一次返回多个值,则需要一个结构,或者更常见的是一个类。
在 Objective-C 中,通过引用传递听起来就像你做错了一样。
Objective-C 中的引用传递主要用于返回有关可恢复故障的
NSError*
信息,其中方法本身的返回值指示请求的任务是成功还是失败(您可以传递 < code>NULL 作为NSError**
参数,以允许该方法优化创建所述错误元数据)。通过引用传递还用于检索对象的内部状态,其中返回值实际上是多值。即 AppKit 中的方法如下所示。在这些情况下,按引用传递参数通常是可选的或充当辅助返回值。
它们在 API 中的使用非常少。按引用传递当然有用,但是 - 如上所述 - 在应用程序代码中这样做应该非常罕见。在许多情况下(以及可能在下面的某些情况下),更好的模式是创建一个可以封装状态的类,然后返回该类的实例,而不是通过引用传递。
If you want to return a value, then return a value. Pass by reference in Cocoa/iOS is largely limited to
NSError**
.Given:
Then do:
And be done with it.
If you need to return more than one value at a time, that begs for a structure or, more often, a class.
In Objective-C, pass by reference smells like you are doing it wrong.
Pass by reference in Objective-C is reserved largely for returning
NSError*
information about a recoverable failure, where the return value of the method itself indicates whether or not the requested task succeeded or failed (you can passNULL
as theNSError**
argument to allow the method to optimize away creating said error metadata).Pass by references is also used to retrieve interior state of objects where the return value is effectively a multi-value. I.e. methods from AppKit like the following. In these cases, the pass-by-reference arguments are typically either optional or are acting as secondary return values.
They are used quite sparingly across the API. There is certainly use for pass by reference, but -- as said above -- doing so should be quite rare and rarer still in application code. In many cases -- and in some of the cases below, potentially -- a better pattern would be to create a class that can encapsulate the state and then return an instance of said class instead of pass by reference.
我相信您正在寻找:
这里真正所做的是我们正在使用一个指向对象的指针的指针。检查 C(是的,只是简单的 C)指南中的“指针取消引用”以获取更多信息。
(...但是正如反复指出的那样,在这个特定的示例中,根本没有理由通过引用传递:只需返回一个值。)
I believe you're looking for:
What's really done here is we're working with a pointer to a pointer to an object. Check C (yup, just plain C) guides for "pointer dereference" for further info.
(...But as has been pointed out repeatedly, in this particular example, there's no reason to pass by reference at all: just return a value.)
将指针传递给对象是 Objective C(和 C)通过引用传递的方式。
我同意“bbum”的观点,即感知到需要通过引用传递是一个思考你正在做什么的信号;然而,绝不是没有正当理由通过引用传递。
每当您有一个需要返回多个值的函数或方法时,您不应该随意创建类。考虑为什么要返回多个值,以及如果为此创建一个类有意义,那么就这样做。否则,只需传入指针即可。
-只是我的2分钱
Passing a pointer to your object is the Objective C (and C) way of passing by reference.
I agree with 'bbum' that a perceived need to pass by reference is a signal to think about what you are doing; however, it is by no means the case that there are not legitimate reasons to pass by reference.
You should not create classes willy-nilly every time you have a function or method that needs to return more than one value. Consider why you are returning more than one value and if it makes sense to create a class for that then do so. Otherwise, just pass in pointers.
-Just my 2 cents
试试这个
并将文件作为
&file
发送:希望这会起作用。
Try this
and send the file as
&file
like:hope this will work.
我怀疑这是因为 NSString 是不可变的。您是否尝试过 NSMutableString?
I suspect this is because NSString is immutable. Have you tried NSMutableString?