1

when i am trying to run this code i got error "variable used within it's own initial value" . what cause this error ?

var pagetitle = ""
var alertActionButtons = [UIAlertAction]()
var okAction = UIAlertAction(title: pagetitle, style: .Default) { action in
    self.presentingViewController!.dismissViewControllerAnimated(false) {
        self.unitDetailProtocolVar!.closeUnitDetail()
    }
}
alertActionButtons.append(okAction)
self.alert = self.controllerUtil.customAlert(pagetitle, buttons: alertActionButtons, alertMessage: alertMessage)
1
  • I see you edited the question to include the missing curly brace. Is it still giving you the same error? If so, check for a recursive method call. Is this code written within dismissViewControllerAnimated or closeUnitDetail? Commented Jun 30, 2015 at 14:39

2 Answers 2

3

The error itself means that you are trying to use a variable to initialize itself.

In your case it's just a missing bracket, causing code to be misaligned. This is how your code looks like:

var okAction = UIAlertAction(title: pagetitle, style: .Default){ action in
    self.presentingViewController!.dismissViewControllerAnimated(false, completion: { ()->Void in
        self.unitDetailProtocolVar!.closeUnitDetail()
    })

    alertActionButtons.append(okAction)
    self.alert = self.controllerUtil.customAlert(pagetitle, buttons: alertActionButtons, alertMessage: alertMessage)
}

and you can see that okAction is used in this line:

alertActionButtons.append(okAction)

The missing bracket in your code is in the closure passed to the UIAlertAction:

var okAction = UIAlertAction(title: pagetitle, style: .Default){ action in
    self.presentingViewController!.dismissViewControllerAnimated(false, completion: { ()->Void in
        self.unitDetailProtocolVar!.closeUnitDetail()
    })
} // <-- this is missing

alertActionButtons.append(okAction)
self.alert = self.controllerUtil.customAlert(pagetitle, buttons: alertActionButtons, alertMessage: alertMessage)
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot to close the curly brace before calling alertActionButtons.append(okAction). Therefore, it thinks you're trying to use okAction within it's own assignment block.

Corrected Code:

var pagetitle = ""
var alertActionButtons:[UIAlertAction] = [UIAlertAction]()
var okAction = UIAlertAction(title: pagetitle, style: .Default){action in
    self.presentingViewController!.dismissViewControllerAnimated(false, completion: {()->Void in
        self.unitDetailProtocolVar!.closeUnitDetail()
    });
} // <- Added missing curly brace     
alertActionButtons.append(okAction)
self.alert = self.controllerUtil.customAlert(pagetitle, buttons: alertActionButtons, alertMessage: alertMessage)

Comments

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.