I have the following class (which is quite simple, and the only reason it exists by itself instead of its properties and methods being coded where they are needed is so that I can subclass it) and it is proving to be quite difficult.
class ParseSubclass{
let object: PFObject
init (objectPassed: PFObject){
object = objectPassed
}
init (queryPassed: PFQuery, toCallWhenLoadFinishes: () -> ()){
func toCallUponCompletion (response: PFObject){
object = response
toCallWhenLoadFinishes()
}
findFirstObjectsInBackgroundFromLocalDataStoreIfPossible(queryPassed, toCallUponCompletion: toCallUponCompletion)
}
}
The error,
Cannot assign to 'object' in 'self'.
is being thrown at the following line: object = response
.
I understand that it is impossible to assign to a let
value after initilization, but specifically inside the class's init
method, it is allowed as documented in the Swift documentation.
Since I am only giving the value to object
inside of the initializer, how would I change the code to explicitly tell the compiler that the function I am using to set it will only be used inside the initializer? Or do I have to trick the compiler in some way? Or is what I am trying to accomplish impossible?
(Note: I tried declaring object
as a var
but it also threw several more errors including:
- on line:
func toCallUponCompletion (response: PFObject){
"Variable 'self.object' used before being initialized - on line:
findFirstObjectsInBackgroundFromLocalDataStoreIfPossible(queryPassed, toCallUponCompletion: toCallUponCompletion)
- "Use of 'self' in method call 'findFirstObjectsInBackgroundFromLocalDataStoreIfPossible' before all stored properties are initialized" - On the following line: (only a
}
is on this line) - Return from initializer without initializing all stored properties
Thanks for any help in advance.