# \[Solved\]- Error message when trying to read variable from Photon in iOS Swift

**URL:** <https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205>\
**Category:** Developer Tools\
**Created:** [November 12, 2016, 3:57pm UTC](https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205 "2016-11-12T15:57:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustafssonerik](https://avatars.discourse-cdn.com/v4/letter/g/e19b73/32.png) [@gustafssonerik](https://community.particle.io/u/gustafssonerik)\
**Post date:** [November 12, 2016, 3:57pm UTC](https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205/1 "2016-11-12T15:57:49Z")

</div>

I’m getting error message in Xcode when i try to read a variable from my Photon. I’ve created an app that can log in and view my devices but I can’t read a variable.

The error message is:

```
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) 

```

Xcode does also print this in the console:

```
fatal error: unexpectedly found nil while unwrapping an Optional value 
(lldb)

```

This is the part of the code that the error occurs and the error points at the line with myPhoton!.getVariable…

```
@IBAction func readTapped(sender: AnyObject) {
    
   myPhoton!.getVariable("myVariable", completion: { (result:Any?, error:NSError?) -> Void in
        if let _ = error {
            print("Failed reading myVariable from device")
        }
        else {
            if let status = result as? Int {
                print("Photon status: \(status).")
            }
        }
    })   
}

```

Im in Xcode 7.3.1.

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [November 12, 2016, 5:58pm UTC](https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205/2 "2016-11-12T17:58:21Z")

</div>

Does the error point to a particular line in your code? I would guess from looking at that code, that myPhoton is nil. You can either use the debugger, or put a print statement at the beginning of the readTapped function to see if that’s true (try print(myPhoton) to see what it gives).

---

<div class="post-metadata">

**Author:** ![gustafssonerik](https://avatars.discourse-cdn.com/v4/letter/g/e19b73/32.png) [@gustafssonerik](https://community.particle.io/u/gustafssonerik)\
**Post date:** [November 12, 2016, 6:18pm UTC](https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205/3 "2016-11-12T18:18:14Z")

</div>

Yes you’re right! I commented out “myPhoton!.getVariable…” code and added “print(myPhoton)” and it is nil.

But how am I going to get my “getVariable” code to work? Do I need to replace “myPhoton” with the particular name of my device which I am trying to read a variable from or do I need to do something else?

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [November 12, 2016, 9:15pm UTC](https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205/4 "2016-11-12T21:15:15Z")

</div>

No, the name of the object is not the issue. You need to show the code for where you create myPhoton and how you declare it, so we can see what’s wrong.

---

<div class="post-metadata">

**Author:** ![gustafssonerik](https://avatars.discourse-cdn.com/v4/letter/g/e19b73/32.png) [@gustafssonerik](https://community.particle.io/u/gustafssonerik)\
**Post date:** [November 13, 2016, 11:11am UTC](https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205/5 "2016-11-13T11:11:43Z")

</div>

Alright! Here is my code:

```
import UIKit

class variableViewController: UIViewController {
    
    var myPhoton : SparkDevice?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

  
    
    
    @IBAction func myDevicesTapped(sender: AnyObject) {
        SparkCloud.sharedInstance().getDevices { (sparkDevices:[AnyObject]?, error:NSError?) -> Void in
            if error != nil {
                print("Check your internet connectivity")
            }
            else {
                if let devices = sparkDevices as? [SparkDevice] {
                    print(sparkDevices)
                    for device in devices {
                        if device.name == "myNewPhotonName" {
                            self.myPhoton = device
                        }
                    }
                }
            }
        }
    }

@IBAction func readTapped(sender: AnyObject) {
    
    // print(myPhoton)
    
    myPhoton!.getVariable("myVariable", completion: { (result:Any?, error:NSError?) -> Void in
        if let _ = error {
            print("Failed reading myVariable from device")
        }
        else {
            if let status = result as? Int {
                print("Photon status: \(status).")
            }
        }
    })
 }
}
```

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [November 13, 2016, 4:36pm UTC](https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205/6 "2016-11-13T16:36:04Z")

</div>

Two questions. Do you have a device whose name is “myNewPhotonName” (that string needs to be the actual name of the device you’re trying to access)? Are you invoking (by a button press, I presume) **myDevicesTapped** before you invoke **readTapped**? If the answer to both of those is yes, then I would put a print statement just above **self.myPhoton = device** to make sure your code reaches that point. Otherwise, I don’t see anything wrong with your code.

---

<div class="post-metadata">

**Author:** ![gustafssonerik](https://avatars.discourse-cdn.com/v4/letter/g/e19b73/32.png) [@gustafssonerik](https://community.particle.io/u/gustafssonerik)\
**Post date:** [November 13, 2016, 6:52pm UTC](https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205/7 "2016-11-13T18:52:41Z")

</div>

Alright, that solves my problem! It was the name of the device, it was not “myNewPhotonName”. Thanks!

But then I have another question. How can I read variables from different devices? Like how can I change the device that **myPhoton** is accessing between different buttons?

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [November 13, 2016, 8:40pm UTC](https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205/8 "2016-11-13T20:40:40Z")

</div>

In the function myDevicesTapped, you are getting the full list of all your devices, so you only need to extend the code a little to check for other names, and assign those to other variables. You don’t change which device myPhoton points to, you create more variables (myPhoton2, myPhoton3, etc.) that each point to a particular device.

```cpp
for device in devices {
    if device.name == "oneOfMyNames" {
        self.myPhoton = device
    }else if device.name == "anotherOfMyNames" {
        self.myPhoton2 = device
    }else if device.name == "stillAnotherName" {
        self.myPhoton3 = device
    }    
}

```

Be sure to declare myPhoton2, myPhoton3, etc. like you did for myPhoton.

---

<div class="post-metadata">

**Author:** ![gustafssonerik](https://avatars.discourse-cdn.com/v4/letter/g/e19b73/32.png) [@gustafssonerik](https://community.particle.io/u/gustafssonerik)\
**Post date:** [November 14, 2016, 9:19pm UTC](https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205/9 "2016-11-14T21:19:31Z")

</div>

Thanks a lot for the help, Ric!

---

<div class="post-metadata">

**Author:** ![KyleG](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/kyleg/32/11077_2.png) [@KyleG](https://community.particle.io/u/KyleG)\
**Post date:** [November 15, 2016, 12:06am UTC](https://community.particle.io/t/solved-error-message-when-trying-to-read-variable-from-photon-in-ios-swift/27205/10 "2016-11-15T00:06:04Z")

</div>

@Ric thanks for the help here!
