kotlin中的类型推断失败

时间:2018-01-16 18:42:36

标签: java android json kotlin

我在iterator()类上创建了一个扩展函数JSONArray,在使用for for循环时,我收到了以下错误:

Type inferece failed: Not enough information to infer parameter T in 
operator fun <T> JSONArray.iterator () L Iterator<T>

我的代码是:

class GetFlickrJsonData(private val mCallBack: OnDataAvailable, private var mBaseUrl: String, private var mLanguage: String, private var mMatchAll: Boolean) : GetRawData.OnDownLoadComplete {

init {
    Log.d(TAG,"GetFlickrJsonData Called")
}
companion object {
    private val TAG = "GetFlickrJsonData"
}
private var mPhotosList: List<Photo>? = null

interface OnDataAvailable {
    fun onDataAvailable(data: List<Photo>,status: ClientInfoStatus)
}

fun executeOnSameThread(searchCriteria: String){
    Log.d(TAG,"executeOnSameThread starts...")
    val destinationUri: String = createUri(searchCriteria,mLanguage,mMatchAll)
    val getRawData = GetRawData(this)
    getRawData.execute(destinationUri)
    Log.d(TAG,"executeOnSameThread ends...")

}

fun createUri(searchCriteria: String,mLanguage: String,mMatchAll: Boolean): String{
    Log.d(TAG,"createUri starts...")

    return Uri.parse(mBaseUrl).buildUpon()
            .appendQueryParameter("tags",searchCriteria)
            .appendQueryParameter("tagmode",if(mMatchAll) "ALL" else "ANY")
            .appendQueryParameter("lang",mLanguage)
            .appendQueryParameter("format","json")
            .appendQueryParameter("nojsoncallback","1")
            .build().toString()

}

override fun onDownloadComplete(data: String?, status: DownloadStatus) {
    Log.d(TAG,"onDownLoadComplete  : $status")
    if (status == DownloadStatus.OK) mPhotosList = ArrayList()

    try {
        val jsonData = JSONObject(data)
        val jsonArray: JSONArray = jsonData.getJSONArray("items")
        for (item: JSONObject in jsonArray){

        }
    }

}

}

扩展功能是:

operator fun <T>JSONArray.iterator(): Iterator<T> = (0 until length()).asSequence().map { get(it) as T }.iterator()

我如何明确地推断它?

0 个答案:

没有答案