Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
908 views
in Technique[技术] by (71.8m points)

syntax - What does the underscore mean in this case?

my code before the migation to Swift 2.0:

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "RhymeFavoriten") {
        // pass data to next view
        let dest = segue.destinationViewController as! FavoritenViewController
        let source = segue.sourceViewController as! RhymeViewController // !!!!!!
        dest.favoritenType = 1
        dest.delegate = self
    }
}

the migration told me to change it to

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "RhymeFavoriten") {
        // pass data to next view
        let dest = segue.destinationViewController as! FavoritenViewController
        _ = segue.sourceViewController as! RhymeViewController // !!!!!!!!!!
        dest.favoritenType = 1
        dest.delegate = self
    }

or

    func textSelected(selectedText:String, selectedType:Int) {
        var fullTextArr = text.componentsSeparatedByString("
")
        var myArray = [String]()  // !!!!!!

to

    func textSelected(selectedText:String, selectedType:Int) {
        var fullTextArr = text.componentsSeparatedByString("
")
        _ = [String]()    // !!!!!!!!!

I can′t see, what is _ = standing for :-(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

_ is a placeholder. It means that the values assigned to _ are ignored.

Xcode's migration tool made this changes because it has detected that you didn't use source or myArray anywhere, thus replaced these variables by the placeholder.

Now instead of being assigned to a variable, the returning result of segue.sourceViewController as! RhymeViewController and the returning result of [String]() are ignored.

The returning result is ignored but the expression is still evaluated at runtime: if it has side effects, these effects will occur.

So if you actually don't need these instructions you should get rid of them entirely.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...