Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift : Cannot assign to "" in self

Tags:

swift

In Swift, I have a computed property called managedObjectContext: NSManagedObjectContext. when I try to initialise it like:

var managedObjectContext: NSManagedObjectContext? {
    get {
        createManagedObjectContext()
        return self.managedObjectContext
    }
    set(newManagedObjectContext) {
        self.manageObjectContext = newManagedObjectContext
    }
}

func createManagedObjectContext() -> NSManagedObjectContext {

    if let coordinator = persistentStorageCoordinator() as NSPersistentStoreCoordinator!
    {
        self.managedObjectContext = NSManagedObjectContext()
        self.managedObjectContext!.persistentStoreCoordinator = coordinator

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleBackgroundMOCDidSaveNotification:", name: NSManagedObjectContextDidSaveNotification, object: self.managedObjectContext)
    }

    return self.managedObjectContext!;
}

I am getting an error saying : Cannot assign to "managedObjectContext" in self.

Can anybody help to fix this?

Thanks in advance.

like image 533
Keshav Avatar asked Jul 07 '14 04:07

Keshav


1 Answers

You also receive this compile error when you're trying to assign a value to a let constant. Changing the variable's type to var should solve it, in this case.

like image 115
Sebyddd Avatar answered Sep 19 '22 18:09

Sebyddd