通过Objective-C中的引用进行的论证 [英] Arguments by reference in Objective-C

查看:77
本文介绍了通过Objective-C中的引用进行的论证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过引用传递NSString,但是它不起作用.

I'm trying to pass an NSString by reference but it doesn't work.

这是功能:

+(void)fileName:(NSString *) file
{
    file = @"folder_b";
}

这是电话:

NSString *file;

[function fileName:file];

nslog(@"%@",file);    // and there is nothing in the string....

我该怎么做才能通过引用传递我的字符串?

What I must do to pass my string by reference?

推荐答案

我相信您正在寻找:

+ (void)fileName:(NSString **)file
{
  *file = @"folder_b";
}

这里真正要做的是我们正在使用指向对象的指针 的指针.在C(是,只是普通C)指南中查找指针取消引用"以获取更多信息.

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中的引用进行的论证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆