Inference Everywhere!
Swift uses inference to figure out what types you are attempting to use. Because of this, you do not need to declare a type when creating variables and constants. However, if you want to declare a type you may do so, and in certain situations, it is absolutely necessary. When declaring a variable, the rule of thumb is that Swift needs to know what type it is. If Swift cannot figure out the type, you need to be more explicit. The following is a valid statement:
var currentRetries = 0
Notice that Swift has to figure out what type of number this is. currentRetries
may be one of the many types of numbers that Swift offers (Swift will infer this as an Int
in case you are wondering, but more on that later). You could also use ...
Get Learning Swift™ Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.