0

I just want to know how may I pass an array from Swift class to Objective C class by reference. I have idea for Swift to Swift by reference using inout keyword. But as ObjC uses pointers itself as object names. How may I inform Swift to pass the object by reference Instead of Value?

Code Snipshot

Swift:

class CheckReference: NSObject {    
    class func test() {

        let array : NSMutableArray! = nil
        ViewController.change(array) //ViewController.change(&array) doesn't work
        print(array)//does not changed 
    }
}

Objective C:

 + (void)changeArray:(NSMutableArray *)array
{
    if(!array)    
        array = [NSMutableArray array];
    [array addObject:@(2)];
}

Edit: For swift it works by changing methods as: But still Objective C interaction is not working.

class func test() {
    var array : NSMutableArray! = nil
    CheckReference.passByReference(arr: &array)
    print(array ?? "nothing")//prints 2

}
class func passByReference(arr: inout NSMutableArray!)
{
    arr = NSMutableArray()
    arr.add(2)
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Saad
  • 8,857
  • 2
  • 41
  • 51
  • Just convert it to `NSMutableArray` if you want to send to obj-c function – Tj3n Mar 17 '17 at 11:44
  • even tried that as well. Updated question – Saad Mar 17 '17 at 11:46
  • it still passes it by value. – Saad Mar 17 '17 at 11:47
  • Hm...have you tried to create the array in swift then pass it to obj-c? Your code above seems should be working though – Tj3n Mar 17 '17 at 11:49
  • yup. Tried. It creates another copy and the original has nothing changed in it – Saad Mar 17 '17 at 11:53
  • You are passing `nil`, which means that a new array is created in the Obj-C method (and not passed back). With `let array = NSMutableArray()` in the Swift code it should work. – Martin R Mar 17 '17 at 12:02
  • @MartinR yeah it worked. The same thing said by Tj3n – Saad Mar 17 '17 at 12:10
  • @Tj3n you can put this to answer I'll accept it. My Bad I didn't noticed this nil thing :) – Saad Mar 17 '17 at 12:12
  • guys one more thing. How Objective C handles then nil Pointers and returns objects in it??? e.g. passing an NSError = nil object and get a valid memory obtained object? – Saad Mar 17 '17 at 12:15

1 Answers1

1

Okay so my guess is that when you are passing nil, you are NOT passing a pointer, since its not pointing to anything, thus your value after that, does not changed.

If you want to pass nil, you may have to change the function to return a pointer to new NSMutableArray, and assign back to the array in your swift code

Create array first in Swift and pass to Obj-c ensure the object has been created and passing proper pointer

Tj3n
  • 9,837
  • 2
  • 24
  • 35
  • Solved my problem but How Objective C handles then nil Pointers and returns objects in it??? e.g. passing an NSError = nil object and get a valid memory obtained object? – Saad Mar 20 '17 at 07:06
  • `NSError` handling is more like `inout`, remember the `&` when you need to hook the `NSError` variable? Check [this](http://stackoverflow.com/questions/13061381/what-does-error-mean-in-objective-c) might help understand, for other case I think its just the different between swift and obj-c compiler against each other – Tj3n Mar 20 '17 at 08:08