箭在科特林起什么作用?

nIM*_*aZx 2 android kotlin

我开始使用Kotlin为Android开发。我对某些概念有疑问。
我在条件语句中使用“->”,但在此示例中我不知道这意味着什么:

 XmlPullParser.START_TAG -> {...}
 XmlPullParser.TEXT -> textValue = xpp.text
 XmlPullParser.END_TAG -> {...}
Run Code Online (Sandbox Code Playgroud)

所有代码是:

        val factory = XmlPullParserFactory.newInstance()
        factory.isNamespaceAware = true
        val xpp = factory.newPullParser()
        xpp.setInput(xmlData.reader())
        var eventType = xpp.eventType
        var currentRecord = FeedEntry()
        while (eventType != XmlPullParser.END_DOCUMENT) {
            val tagName = xpp.name.toLowerCase()    
            when (eventType) {

              XmlPullParser.START_TAG -> {
                    Log.d(TAG, "parse: Starting tag for " + tagName)
                    if (tagName == "entry") {
                        inEntry = true
                    }
                }

                XmlPullParser.TEXT -> textValue = xpp.text

                   XmlPullParser.END_TAG -> {
                    Log.d(TAG, "parse: Ending tag for " + tagName)
                    if (inEntry) {
                        when (tagName) {
                            "entry" -> {
                                applications.add(currentRecord)
                                inEntry = false
                                currentRecord = FeedEntry()  
                            }

                            "name" -> currentRecord.name = textValue
                            "artist" -> currentRecord.artist = textValue
                            "releasedate" -> currentRecord.releaseDate = textValue
                            "summary" -> currentRecord.summary = textValue
                            "image" -> currentRecord.imageURL = textValue
                        }
                    }
                }
            }
Run Code Online (Sandbox Code Playgroud)

Ste*_*fMa 6

这只是Kotlin 在expression时语法

基本上,它会检查内的条件,when ()并在->匹配后执行该块。您可以“内联”,这必须要执行的代码或者其包装成花括号。

另请参见本示例(科特琳游乐场)并将其更改aString为另一个值。