Objective-C 中的引用参数

发布于 2024-09-11 12:26:13 字数 323 浏览 7 评论 0原文

我试图通过引用传递 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枕梦 2024-09-18 12:26:14

如果您想返回一个值,则返回一个值。 Cocoa/iOS 中的引用传递很大程度上限于 NSError**

鉴于:

+(void)fileName:(NSString *) file

然后做:

+(NSString *) fileName;

然后完成它。

如果您需要一次返回多个值,则需要一个结构,或者更常见的是一个类。

在 Objective-C 中,通过引用传递听起来就像你做错了一样。


Objective-C 中的引用传递主要用于返回有关可恢复故障的 NSError* 信息,其中方法本身的返回值指示请求的任务是成功还是失败(您可以传递 < code>NULL 作为 NSError** 参数,以允许该方法优化创建所述错误元数据)。

通过引用传递还用于检索对象的内部状态,其中返回值实际上是多值。即 AppKit 中的方法如下所示。在这些情况下,按引用传递参数通常是可选的或充当辅助返回值。

它们在 API 中的使用非常少。按引用传递当然有用,但是 - 如上所述 - 在应用程序代码中这样做应该非常罕见。在许多情况下(以及可能在下面的某些情况下),更好的模式是创建一个可以封装状态的类,然后返回该类的实例,而不是通过引用传递。

NSWorkspace.h:- (BOOL)getInfoForFile:(NSString *)fullPath application:(NSString **)appName type:(NSString **)type;
NSTextView.h:- (void)smartInsertForString:(NSString *)pasteString replacingRange:(NSRange)charRangeToReplace beforeString:(NSString **)beforeString afterString:(NSString **)afterString;
NSAttributedString.h:- (BOOL)readFromURL:(NSURL *)url options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict;
NSNib.h:- (BOOL)instantiateWithOwner:(id)owner topLevelObjects:(NSArray **)topLevelObjects NS_AVAILABLE_MAC(10_8);
NSSpellChecker.h:- (NSRange)checkGrammarOfString:(NSString *)stringToCheck startingAt:(NSInteger)startingOffset language:(NSString *)language wrap:(BOOL)wrapFlag inSpellDocumentWithTag:(NSInteger)tag details:(NSArray **)details NS_AVAILABLE_MAC(10_5);

If you want to return a value, then return a value. Pass by reference in Cocoa/iOS is largely limited to NSError**.

Given:

+(void)fileName:(NSString *) file

Then do:

+(NSString *) fileName;

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 pass NULL as the NSError** 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.

NSWorkspace.h:- (BOOL)getInfoForFile:(NSString *)fullPath application:(NSString **)appName type:(NSString **)type;
NSTextView.h:- (void)smartInsertForString:(NSString *)pasteString replacingRange:(NSRange)charRangeToReplace beforeString:(NSString **)beforeString afterString:(NSString **)afterString;
NSAttributedString.h:- (BOOL)readFromURL:(NSURL *)url options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict;
NSNib.h:- (BOOL)instantiateWithOwner:(id)owner topLevelObjects:(NSArray **)topLevelObjects NS_AVAILABLE_MAC(10_8);
NSSpellChecker.h:- (NSRange)checkGrammarOfString:(NSString *)stringToCheck startingAt:(NSInteger)startingOffset language:(NSString *)language wrap:(BOOL)wrapFlag inSpellDocumentWithTag:(NSInteger)tag details:(NSArray **)details NS_AVAILABLE_MAC(10_5);
早茶月光 2024-09-18 12:26:14

我相信您正在寻找:

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

这里真正所做的是我们正在使用一个指向对象的指针的指针。检查 C(是的,只是简单的 C)指南中的“指针取消引用”以获取更多信息。

(...但是正如反复指出的那样,在这个特定的示例中,根本没有理由通过引用传递:只需返回一个值。)

I believe you're looking for:

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

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.)

青朷 2024-09-18 12:26:14

将指针传递给对象是 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

貪欢 2024-09-18 12:26:14

试试这个

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

并将文件作为 &file 发送:

NSString *file;
[function fileName:&file];
nslog(@"%@",file);

希望这会起作用。

Try this

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

and send the file as &file like:

NSString *file;
[function fileName:&file];
nslog(@"%@",file);

hope this will work.

会发光的星星闪亮亮i 2024-09-18 12:26:14

我怀疑这是因为 NSString 是不可变的。您是否尝试过 NSMutableString?

I suspect this is because NSString is immutable. Have you tried NSMutableString?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文