protocols - How does Swift memory management work? -
specifically, how swift memory management work optionals using delegate pattern?
being accustomed writing delegate pattern in objective-c, instinct make delegate weak
. example, in objective-c:
@property (weak) id<foodelegate> delegate;
however, doing in swift isn't straight-forward.
if have normal looking protocol:
protocol foodelegate { func dostuff() }
we cannot declare variables of type weak:
weak var delegate: foodelegate?
produces error:
'weak' cannot applied non-class type 'foodelegate'
so either don't use keyword weak
, allows use structs
, enums
delegates, or change our protocol following:
protocol foodelegate: class { func dostuff() }
which allows use weak
, not allow use structs
or enums
.
if don't make protocol class protocol, , therefore not use weak
variable, i'm creating retain cycle, correct?
is there imaginable reason why protocol intended used delegate protocol shouldn't class protocol variables of type can weak
?
i ask, because in delegation section of the apple official documentation on swift protocols, provide example of non-class protocol , non-weak variable used delegate class:
protocol dicegamedelegate { func gamedidstart(game: dicegame) func game(game: dicegame, didstartnewturnwithdiceroll diceroll: int) func gamedidend(game: dicegame) }
class snakesandladders: dicegame { let finalsquare = 25 let dice = dice(sides: 6, generator: linearcongruentialgenerator()) var square = 0 var board: [int] init() { board = [int](count: finalsquare + 1, repeatedvalue: 0) board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02 board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08 } var delegate: dicegamedelegate? func play() { square = 0 delegate?.gamedidstart(self) gameloop: while square != finalsquare { let diceroll = dice.roll() delegate?.game(self, didstartnewturnwithdiceroll: diceroll) switch square + diceroll { case finalsquare: break gameloop case let newsquare newsquare > finalsquare: continue gameloop default: square += diceroll square += board[square] } } delegate?.gamedidend(self) } }
should take hint apple thinks should using structs delegates? or bad example, , realistically, delegate protocols should declared class-only protocols delegated object can hold weak reference delegate?
should take hint apple thinks should using structs delegates? or bad example, , realistically, delegate protocols should declared class-only protocols delegated object can hold weak reference delegate?
here's thing. in real life cocoa programming, delegate existing class. class because exists other purpose class can satisfy - because cocoa demands it.
for example, often, take ios example, 1 view controller needs act view controller's delegate purposes of arranging message , forth between them. ownership of view controllers dictated view controller hierarchy , nothing else. so, in swift, in objective-c, had better make delegate
property weak
, because terrible if 1 view controller took memory management ownership of view controller!
so, in real world of cocoa framework, there serious danger of incorrect ownership or of retain cycle. , problem weak
solves. works, rightly say, classes.
the example in book, however, objects living off in abstract made-up artificial swift-only world. in world, long aren't in danger of circularity (retain cycle), there's no reason not use structs , there's no reason worry memory management. world not world programming in! , world not framework cocoa world objective-c delegate pattern comes , belongs to.
Comments
Post a Comment