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

后端 未结 6 843
长发绾君心
长发绾君心 2020-12-02 09:03

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

相关标签:
6条回答
  • 2020-12-02 09:07

    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:

    - (OSStatus) fileFileInfo: (FileInfo&)fi;
    
    0 讨论(0)
  • 2020-12-02 09:09

    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.

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

    0 讨论(0)
  • 2020-12-02 09:22

    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:

    @implementation AddressWrapper
    @synthesize array, string;
    @end
    
    
    
    -(void) getAddresses: (AddressWrapper*) wrapper {
        wrapper.array = myNSArray;
        wrapper.string = myNSString;
    }
    
    0 讨论(0)
  • 2020-12-02 09:28

    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.

    Example:

    void func(NSString* string)
    {
      ...
    }
    
    void func(NSInteger myint)
    {
     ...
    }
    
    ..
    NSString* str = @"whatever";
    NSInteger num = 5;
    
    func(str);
    func(num); 
    
    0 讨论(0)
  • 2020-12-02 09:32

    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!!

    0 讨论(0)
  • 2020-12-02 09:34

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

    The equivalent to the following C# code:

    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);
    
    0 讨论(0)
提交回复
热议问题