简体   繁体   English

Objective-C中引用的参数

[英]Arguments by reference in Objective-C

I'm trying to pass an NSString by reference but it doesn't work. 我试图通过引用传递NSString但它不起作用。

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? 我必须做什么来通过引用传递我的字符串?

If you want to return a value, then return a value . 如果要返回值,则返回一个值 Pass by reference in Cocoa/iOS is largely limited to NSError** . 在Cocoa / iOS中通过引用传递主要限于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. 在Objective-C中,通过引用传递气味就像你做错了。


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

Pass by references is also used to retrieve interior state of objects where the return value is effectively a multi-value. 通过引用传递还用于检索对象的内部状态,其中返回值实际上是多值。 Ie methods from AppKit like the following. 即AppKit中的方法如下。 In these cases, the pass-by-reference arguments are typically either optional or are acting as secondary return values. 在这些情况下,pass-by-reference参数通常是可选的或充当辅助返回值。

They are used quite sparingly across the API. 它们在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);

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. 检查C(是的,只是简单的C)指南“指针取消引用”以获取更多信息。

(...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.) (...但正如已经反复指出的那样,在这个特定的例子中,根本没有理由通过引用:只返回一个值。)

Try this 尝试这个

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

and send the file as &file like: 并将文件作为&file发送,如:

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

hope this will work. 希望这会奏效。

Passing a pointer to your object is the Objective C (and C) way of passing by reference. 将指针传递给对象是通过引用传递的Objective C(和C)方式。

I agree with 'bbum' that a perceived need to pass by reference is a signal to think about what you are doing; 我同意'bbum'认为需要通过引用传递是一个信号来思考你在做什么; 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 - 我的2美分

I suspect this is because NSString is immutable. 我怀疑这是因为NSString是不可变的。 Have you tried NSMutableString ? 你试过NSMutableString吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM