object property nil despite assigning him value

jerem

I have the following class:

class Event: NSObject {


    var eventID :String!
    var name: String!
init(name: String,eventID: String) {

        self.eventID = eventID
        self.name = name
    }

Here in my View Im using an instance of my class

     var selectedEvent: Event?

     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

                let indexPath = tableView.indexPathForSelectedRow!

                print(listOfEvents[indexPath.row].eventID) // print my string
                selectedEvent.eventID = listOfEvents[indexPath.row].eventID
                print(selectedEvent.eventID) //print nil
     }

what am I doing wrong??? thanks for help

KeyMaker00

If you put your code into a Playground you will get:

An error at the line

selectedEvent.eventID = listOfEvents[indexPath.row].eventID

Because your variable selectedEvent is nill since you've declared it but not initialized it. To do it, replace the line

var selectedEvent: Event?

with

var selectedEvent = Event(name: "YourEventName", eventID: listOfEvents[indexPath.row].eventID)

as Oleg mentioned it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Found nil despite checking before assigning to variable

Angular ngOnInit the object property is always undefined despite the the object has value

method change value of object passed to him

Assigning a value to an object in Java

Parse.com - email from PFUser object fetches a nil despite having value

Assigning Retained Object to Unsafe Property

Assigning retained object to weak property

assigning undefined to JSON property value

Assigning a random enum value a property

Assigning `nil` value to a generically typed variable in Swift

Understanding TS error when assigning value to an object dynamic property (which key is from a Union Type)

Assigning a value to an object of type AnyObject

UIPasteBoard "string" property returning nil despite "hasStrings" being true

If params[:object].nil? :object = value

"Assigning retained object to weak property" with init?

Assigning one of many values to object's property

Function used for assigning something as an object's property?

Assigning new property to object using deconstruction

Assigning an executable function to a property of a Javascript object

Typescript "Object is possibly undefined" while assigning property

What are they doing in this piece of code (Assigning $({}) to an object property)?

C# assigning value to property of struct error

ios magicalRecord not assigning string value to property

Cypher query: Assigning an aggregate value as a property of a relationship

assigning new value to an object in object array in jquery

Assigning the key of the object as a value inside that object

Assigning value to arg with val object(arg) = object

Deserializing an XML into a object: XML element with xsi:nil="true" should have null value (and not empty value) for respective property in object

unexpectedly found nil while unwrapping an Optional value DESPITE checking