I've got a strange problem with setter in swift. I've got class PlayingCard with code:
var rank: NSInteger {
get{
return self.rank
}
set(rank){
self.rank = rank
}
}
var suit: NSString {
get{
return self.suit
}
set(suit){
self.suit = suit
}
}
init(suit: NSString, rank: NSInteger) {
super.init()
self.suit = suit
self.rank = rank
}
I use this init() method in another class, and implementation looks like this:
init() {
super.init(cards: [])
for suit in PlayingCrad.validSuit() {
var rank: Int = 0
for rank; rank <= PlayingCrad.maxRank(); rank++ {
var card = PlayingCrad(suit: suit, rank: rank)
addCard(card)
}
}
}
And when the code looks like above I've got a error in line:
self.suit = suit
EXC_BAD_ACCESS(code=2, adress=0x7fff5c4fbff8)
But when I removed setter and getter from rank and suit attribute it's worked fine, no error has shown up.
Can you explain me why this EXC_BAD_ACCESS error has shown up?
Thank you for your help
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…