Swift 在应用内购买后启用阻止行。编码问题

Swift 在应用内购买后启用阻止行。编码问题,swift,function,uitableview,swift2,in-app-purchase,Swift,Function,Uitableview,Swift2,In App Purchase,我有一个应用程序,阻止用户访问视图控制器的几行。这是通过检查bool类型的变量是否设置为true或false来完成的 var unlocked: bool = false override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell

我有一个应用程序,阻止用户访问视图控制器的几行。这是通过检查bool类型的变量是否设置为true或false来完成的

var unlocked: bool = false

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!

    //blocking cells if they are not paid for.
    if unlocked == false {

        if ( indexPath.row  >= 2 ) {
            cell.userInteractionEnabled = false
            cell.contentView.alpha = 0.5
        }
        else{
            cell.userInteractionEnabled = true
            cell.contentView.alpha = 1
        }
    }
    return cell  
}
这很好用。然后,我有一个选项供用户购买对其余行的访问权限,从而获得应用程序的剩余内容。一旦购买了应用内购买,它将运行函数“updateSections()”。我知道这个函数是在购买时调用的,因为我已经测试过了

我现在想允许用户从“updatedSections()”函数访问表视图中的其余行,因为他们将为此付费

我尝试的是:

//function to unlock
func unlockSections() {

    //This is the code for what happens once the device has bought the IAP. going to have to save what happens here in using nsuserdefaults to make sure it will work when the app opens and closes.

    print("The IAP worked")
    let unlocked = true
    tableview.reloadData()
}

然而,这似乎不起作用。我看不出哪里出了问题。

问题是这行:

let unlocked = true
正在定义一个名为
unlocked
的新常量,该常量仅存在于
unlockSections
方法的范围内。它与在类开始时定义的名为
unlocked
的属性非常不同。要更新属性而不是创建新常量,只需删除“let”:

或者,如果你想清楚无误(或者你想两者兼有,但在特定情况下使用该物业),请使用“自我”。强调你打算使用该物业:

self.unlocked = true
self.unlocked = true