jeudi 25 août 2016

Local scope in if-else statement in swift 2.3

I just found a small inconsistent behaviour in my ios app, about local scope.

A person can register itself for an event when there are seats left. In this case OnWaitingList and OnWaitingListAt is not defined.

When no seats are left the person can get on a waiting list. If the user accepts (clicks OK) OnWaitingList is set to true and OnWaitingListAt to unixtime. In either case saved to the same DynamoDB table.

A person can register itself to multiple events.

What I found out was when a person first gets on a waiting list (no seats left) and then registers itself to another event (seats left) OnWaitingList is false and OnWaitingListAt is 0, the initial values in the class-definition. These two values are then saved as well.

A participant variable is defined inside the brackets {} in the if and else statement.

I assumed scope was bound to the brackets and allow me to have two variables with the same name and possible different values as stated in this article. I may be wrong but it seems this cross-pollination should not happen under this circumstance.

Two variables with different names will solve this issue. It does not break the flow but it's annoying since I can't figure out why this happens.

if (still seats left) {
    let participant = self.createParticipant(self.items[indexPath.row])

    dynamoDBObjectMapper.save(participant).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task:AWSTask!) -> AnyObject! in
        Register person
        return nil
    })
} else { // Offer people to sign up on a waiting list.
    let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    let waitingListAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: { (action:UIAlertAction) -> Void in
        let participant = self.createParticipant(self.items[indexPath.row])
        participant.OnWaitingList = true
        participant.OnWaitingListAt = NSDate().timeIntervalSince1970

        dynamoDBObjectMapper.save(participant).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task:AWSTask!) -> AnyObject! in
            Register person with OnWaitingList and OnWaitingListAt set
            return nil
        })
    })
}

func createParticipant(item: AggregatedEvents) -> Participants {
    let participant = Participants()
    participant?.Timestamp = String(item.timestamp!)
    participant?.Id = self.fbId
    participant?.Date = item.date!
    participant?.Event = item.event!
    participant?.Location = item.location!
    participant?.Name = self.name
    participant?.Time = item.time!

    return participant!
}

class Participants: AWSDynamoDBObjectModel, AWSDynamoDBModeling {
    var Timestamp: NSString?  // Partition key.
    var Id: NSString?         // Sort key.

    var Date: NSString? = "28. okt. 1967"
    var Event: NSString? = "PT-AMOK Trening"
    var Location: NSString? = "Breim"
    var Name: NSString? = "Claus Guttesen"
    var Time: NSString? = "19:00"
    var OnWaitingList: NSNumber? = false
    var OnWaitingListAt: NSNumber? = 0.0
}

Aucun commentaire:

Enregistrer un commentaire