@warn_unused_result
Suppose you have an array representing a deck of cards:
var deck: [Card] = standardDeck.shuffled()
You want to write a function to transfer the card to the player. You want to remove the βtopβ card from the deck, add it to the playerβs hand and remove it from the deck:
func dealCard(to player: Player) { guard let card = deck.last else { fatalError("Ran out of cards") } player.hand.append(card) deck.dropLast() }
When you are testing your application, you are perplexed. All hands of your players are filled with copies of the same card.
Being new to Swift, you think dropLast modifies the deck by removing its last element. Unfortunately, you are mistaken. It returns a new array containing everything except the last deck element. (Technically, it returns ArraySlice .)
The compiler and the standard library conspired to help you sort out the problem. The dropLast function dropLast annotated with @warn_unused_result , so Xcode shows you a warning when you call dropLast :
.../Cards.swift:85:10: Result of call to 'dropLast()' is unused
When you see the warning, you decide to click on the dropLast button and read the documentation that tells you what dropLast doing (returns a new array), and you understand that you need to change the line to this:
deck.removeLast()
Many, many functions in the Swift standard library and in other libraries are mainly useful for what they return. Ignoring the return value of one of these functions is usually a mistake, so Swift makes it easier for the author of the library to warn the user about this behavior. In fact, Swift is likely to be changed soon to use @warn_unused_result by default and use the new @discardableResult attribute for the function to suppress the warning.
@warn_unqualified_access
You were so successful at your amazing iOS card game that you decided to port it to Mac OS X. On Mac OS X, you use NSView instead of UIView to show everything on screen. You are trying to understand why your own CardView not draw correctly, so you want to call the standard print Swift function in drawRect:
class CardView: NSView { var card: Card override func drawRect(dirtyRect: NSRect) { print("Drawing \(card)")
When you launch the application, you will be surprised to find that it pops up a print dialog! What's happening? The compiler and NSView conspired to help you sort out the problem. The NSView.print function NSView.print annotated with @warn_unqualified_access , so Xcode shows you a warning when calling print :
.../CardView.swift:95:9: Use of 'print' treated as a reference to instance method in class 'NSView'
When you see the warning, you can click print and read the documentation. You will learn that NSView has its own print method, which allows the user to print the contents of the view on paper. How funny! Now you understand that you need to change the call to explicitly use the Swift print function as follows:
Swift.print("Drawing \(card)")
(It is almost impossible to develop for Mac OS X in Swift without being involved in this particular case.)
Such a problem is much less common than another problem of ignoring the result of a function. NSView.print is the only case I can recall.