0

I am new in Swift. I create a node swift file to store the node information. And the other group swift file is a group which store the all node.

The code of Node.swift is like the following:

class Node {

    var id:UInt8=0
    var type:Int=0
    var name:String=""
    var bdAddr:NSUUID!

    //The node private packet counter
    var nodePktNum:Int=0
}

The code of Group.swift is like the following:

class Group {

    var mLedDevice:[LedDevice]? = [LedDevice]()

    class LedDevice {
        var node            :Node?
        var rssi            :Int?
    }

    func allocateNode()
    {
        print("mLedDevice![0].node = \(mLedDevice![0].node))")
    }

}

When I try call function (allocateNode) and try to print mLedDevice![0].node) via print("mLedDevice![0].node = \(mLedDevice![0].node))")

It show the error fatal error: Array index out of range.

Did I missing something for initialize of var mLedDevice:[LedDevice]? = [LedDevice]() ?

Thanks in advance.

===================================EDIT=================================

I want to add the item into array , so I create a parameter like let let leddevice : LedDevice , and try to give it some value. And add the leddevice into array mLedDevice. But it show constant 'leddevice' used before being initialized.

How to give the init value for let leddevice : LedDevice ?

func allocateNode()
{
  let leddevice : LedDevice
  leddevice.node?.id = UInt8(0)
  leddevice.node!.bdAddr = NodeUUID
  mLedDevice?.append(leddevice)
}
3
  • Have you added any LedDevice object to the array. You created the array but it's empty one after then you are trying to access first element (mLedDevice![0]) which not exists and this produce the error Commented Jan 14, 2016 at 8:47
  • Your array is empty, allocateNode() should first add the node to the array, then print it Commented Jan 14, 2016 at 8:49
  • Please read the chapter about Initialization in the Swift Language Guide Commented Jan 14, 2016 at 9:11

2 Answers 2

1

The only thing I can think about that can cause this is that the array is empty i.e. you are attempting to access index 0 of that array but that doesn't exist.

Try the following and it may give you an insight on how to solve it after seeing the content of the array:

print("mLedDevice = \(mLedDevice))")

In other words you are instantiating an array with no elements in it.

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

3 Comments

Thanks! But I meet the new question when I try to add the item into array. it show constant 'leddevice' used before being initialized...how to give it a init vale ? There has many parameter in type LedDevice...
@Martin try using var instead of let. Does that help?
@Martin show us the code where you give it a value...it might help further
1

In your line of code

var mLedDevice:[LedDevice]? = [LedDevice]()

You are only initializing an empty array. What you are trying afterwards is to access the first element, of an empty array, which is out of bounds.

Before your print statement, you will need to add an item to your array

var ledDevice = LedDevice()
mLedDevice.append(ledDevice)

And then your print statement would not give you any errors.

UPDATED: Answer for the added question

let leddevice : LedDevice is defining a constant of type LedDevice but is not yet initialized, and then it is being used in the next lines of code. You should replace it with

let leddevice = LedDevice()

Which will also initialize the variable.

Note: If you have any further questions, you should write a new question for that.
Note2: Have you read any guides about initialization?

1 Comment

Thanks! But I meet the new question when I try to add the item into array. it show constant 'leddevice' used before being initialized...how to give it a init vale ? There has many parameter in type LedDevice...

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.