objective-c 如何在目标 C (iphone) 中通过引用传递值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1344476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:08:19  来源:igfitidea点击:

How to pass values by reference in objective C (iphone)

objective-c

提问by Darren

I have a very basic question. I'm a new iPhone programmer. My question is can anybody tell me how can I pass values by reference to a function in obj. C? I know how to do it in VB and C#. But don't know how to do it in Obj c.

我有一个非常基本的问题。我是一名新的 iPhone 程序员。我的问题是任何人都可以告诉我如何通过引用 obj 中的函数来传递值。C?我知道如何在 VB 和 C# 中做到这一点。但不知道如何在Obj c 中做到这一点。

Thanks

谢谢

回答by Darren

Pass-by-reference in Objective-C is the same as it is in C.

Objective-C 中的引用传递与 C 中的相同。

The equivalent to the following C# code:

等效于以下 C# 代码:

void nullThisObject(ref MyClass foo)
{
    foo = null;
}

MyClass bar = new MyClass();
this.nullThisObject(ref bar);
assert(bar == null);

is

- (void)nilThisObject:(MyClass**)foo
{
    [*foo release];
    *foo = nil;
}

MyClass* bar = [[MyClass alloc] init];
[self nilThisObject:&bar];
NSAssert(bar == nil);

and

- (void)zeroThisNumber:(int*)num
{
    *num = 0;
}

int myNum;
[self zeroThisNumber:&myNum];
NSAssert(myNum == 0);

回答by Peter N Lewis

If you use Objective C++, which you can do by naming your file with extension .mm, or telling Xcode to compile all source as Objective C++, then you can pass references in the same way you do with C++, eg:

如果您使用 Objective C++,您可以通过将文件命名为扩展名 .mm,或者告诉 Xcode 将所有源代码编译为 Objective C++,那么您可以像使用 C++ 一样传递引用,例如:

- (OSStatus) fileFileInfo: (FileInfo&)fi;

回答by ttvd

There is no passing by reference (in C++ sense) in Objective C. You can pass your objects by pointer, and for primitive types by value.

在 Objective C 中没有通过引用传递(在 C++ 意义上)。您可以通过指针传递对象,对于原始类型,可以通过值传递。

Example:

例子:

void func(NSString* string)
{
  ...
}

void func(NSInteger myint)
{
 ...
}

..
NSString* str = @"whatever";
NSInteger num = 5;

func(str);
func(num); 

回答by Niranjala S

Try this way :

试试这个方法:

    -(void)secondFunc:(NSInteger*)i
    {
      *j=20;
     //From here u can change i value as well
     //now i and j value is same (i.e i=j=20)
    }
    -(void) firstFunc
    {
       NSInteger i=5;
      [self secondFunc :&i];
     //i value is 20
    }

Hope it helps!!

希望能帮助到你!!

回答by Chuck

It depends on what you mean. The closest thing C (and thus Objective-C) has to a reference type is pointers. As the name implies, pointers point to data that exists somewhere else. In order to get the thing being pointed to, you have to dereference the pointer. Objects in Objective-C are never directly accessed — you always use pointers to talk to them. You can also pass pointers to other types than objects.

这取决于你的意思。C(以及Objective-C)与引用类型最接近的东西是指针。顾名思义,指针指向存在于其他地方的数据。为了得到被指向的东西,你必须取消对指针的引用。Objective-C 中的对象永远不会被直接访问——你总是使用指针来与它们交谈。您还可以将指针传递给对象以外的其他类型。

Pointers are kind of a tricky subject. I would definitely recommend reading up on C to get a feel for them.

指针是一个棘手的话题。我绝对会推荐阅读 C 以了解它们。

回答by stephen

There's an argument to be had if you want to get more than one address from a resulting function call. You can do so instead by creating a wrapper class and passing that instead. For example:

如果您想从结果函数调用中获得多个地址,则需要有一个参数。您可以通过创建一个包装类并传递它来代替。例如:

@interface AddressWrapper : NSObject {
    NSArray     *array;
    NSString    *string;
}

@property (nonatomic, assign)   NSArray *array;
@property (nonatomic, assign)   NSString *string;
@end

In *.m:

在 *.m:

@implementation AddressWrapper
@synthesize array, string;
@end



-(void) getAddresses: (AddressWrapper*) wrapper {
    wrapper.array = myNSArray;
    wrapper.string = myNSString;
}