1

I'm trying to change the value of a global variable transferredData in the function didTransferData(_ data: Data?). My code looks like this:

var transferredData: [Double] = [0.0]
func didTransferData(_ data: Data?) {

        var dataArray = [CUnsignedChar](repeating:0, count: (data?.count)!)
        data?.copyBytes(to: &dataArray, count: dataArray.count)

        let dataInt = dataArray.filter({ Int(String($0)) != nil }).map({ Int(String($0))! })
        let dataDouble = dataInt.map { Double($0) }
        print("Data double: \(dataDouble)")

        transferredData = dataDouble
}

Printing transferredData inside of didTransferData returns the correct array, containing all values of dataDouble. But when I try to access it in this function later on

func returnData() -> [Double]{
    print("return Data: \(transferredData)")
    return transferredData
}

it returns [0.0]. I'm not sure if I'm missing something crucial here, but I thought that even if I change the value of a global variable in a function, the new value should be accessible for every other functions, too.

Thanks for any advice!!

3
  • Is this code all at the top-level or is it inside a class? Commented Mar 21, 2017 at 14:46
  • It's inside a class. Commented Mar 21, 2017 at 14:46
  • You might be interest in this stackoverflow.com/a/41560949/2303865 Commented Mar 21, 2017 at 15:14

2 Answers 2

2

You said that this code is inside a class. The variable transferredData is not a global, it is a member variable.

There is one of these per object of the class. I suspect that you are not using the same object to make these two calls.

You could make the member shared between all objects by declaring it static, but I think it would be better to leave as is and arrange to use the same object.

EDIT: based on comment

If you write the code

  let centralInstance = CentralViewController()

You will get a new object with its own transferredData member initially set to [0.0]. You need to either

  1. Get a reference to the same VC object that has the data
  2. Store the data some where else in an object that you can get back (since the VC might be gone)

To hack something that works (not recommended, but to help understand)

  1. You could move transferredData out of the class and make it an actual global variable. I would do this only to help you get past this issue and understand what's going on.
  2. You could also make it static, which makes all of the VC's share the same instance of the transferredData

When you understand what is going on, you can try to do something a little better (at least move it to its own model object, and not in the VC at all). The next simplest thing is some kind of singleton to manage it -- this is just a glorified global variable but puts you more on the road to a more typical solution.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm not sure I understood your answer in the right way. The two functions from above are in the same class CentralViewController. In another class I create an instance of CentralViewController via let centralInstance = CentralViewController(). But when I call transferredData with centralInstance.transferredData it gives me the empty array [0.0]. So, where's the error?
Show more code in your question. Show how you use each function and how the object is shared or not.
The important thing to understand is that transferredData is not a global variable. There is one of these for each instance of the class. If you create another object by calling the constructor then you get a new instance of the whole class including this variable. You probably want to store it somewhere else, not in the VC.
"The variable transferredData is not a global, it is a member variable." Strictly speaking, it's not a variable at all, it's a property. Instance (or, member) variables aren't exposed in Swift. A minor point, I know, but something that tends to confuse folks who are new to Swift.
Okay, I think I got the issue. I totally misunderstood the global variable thing. Your tip to move transferredData out of the class really helped me to understand everything. I think I'll just store my data in another object and then retry everything. Thanks a lot!!
0

Where is the problem:

$ swift
Welcome to Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1). Type :help for assistance.
  1> var data:[Double] = [0.0] 
  2.  
  3. class MuckWithData { 
  4.  
  5.   func showData () -> [Double] { 
  6.     print ("Data: \(data)")  
  7.     return data  
  8.   }  
  9.  
 10.   func modifyData () {  
 11.     data = [1.0]  
 12.   } 
 13. }
data: [Double] = 1 value {
  [0] = 0
}
 14>  
 15> var mucked = MuckWithData()
mucked: MuckWithData = {}
 16> mucked.showData()
Data: [0.0]
$R0: [Double] = 1 value {
  [0] = 0
}
 17> mucked.modifyData()
 18> mucked.showData() 
Data: [1.0]
$R1: [Double] = 1 value {
  [0] = 1
}

Probably your computation for dataDouble is actually [0.0] and thus it appears that transferredData didn't change, but it did.

1 Comment

I'm pretty sure the issue is that they effectively do var mucked2 = MuckWithData() and then mucked2.showData() which shows [0.0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.