简体   繁体   English

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

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

I have a very basic question. 我有一个非常基本的问题。 I'm a new iPhone programmer. 我是一名新的iPhone程序员。 My question is can anybody tell me how can I pass values by reference to a function in obj. 我的问题是,任何人都可以告诉我如何通过引用obj中的函数来传递值。 C? C? I know how to do it in VB and C#. 我知道如何在VB和C#中做到这一点。 But don't know how to do it in Obj c. 但不知道如何在Obj c中做到这一点。

Thanks 谢谢

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

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. 目标C中没有通过引用传递(在C ++意义上)。您可以通过指针传递对象,按值传递原始类型。

Example: 例:

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

void func(NSInteger myint)
{
 ...
}

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

func(str);
func(num); 

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

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

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!! 希望能帮助到你!!

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;
}

It depends on what you mean. 这取决于你的意思。 The closest thing C (and thus Objective-C) has to a reference type is pointers. C(因此Objective-C)与引用类型最接近的是指针。 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. Objective-C中的对象永远不会被直接访问 - 您总是使用指针与它们交谈。 You can also pass pointers to other types than objects. 您还可以将指针传递给除对象之外的其他类型。

Pointers are kind of a tricky subject. 指针是一个棘手的主题。 I would definitely recommend reading up on C to get a feel for them. 我肯定会建议你阅读C来感受他们。

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

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