Как я могу обнаружить, если селектор возвращает пустой указатель?

Вот решение для нового проекта Dagger.

Эти две строки отвечают за создание структуры времени компиляции в Dagger 2.

compile 'com.google.dagger:dagger:2.14.1' // создает структуру во время компиляции annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' // создает структуру во время компиляции на основе аннотации, которые вы предоставили.

Полная настройка Dagger

        //dagger 2
        compile 'com.google.dagger:dagger:2.14.1'
        annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'

        //to enable DaggerActivity, DaggerBroadcastReceiver, DaggerFragment etc classes
        compile 'com.google.dagger:dagger-android:2.14.1'
        annotationProcessor 'com.google.dagger:dagger-android-processor:2.14.1'

        //support libraries with dagger 2
        compile 'com.google.dagger:dagger-android-support:2.14.1'

Примечание : необходимо настроить процесс аннотации, как показано на скриншоте ниже. Вы можете сделать это File>Other Settings>Default Settings> поиск "Annotation processor" enter image description here

265
задан Ata Iravani 21 January 2013 в 19:48
поделиться

3 ответа

My favourite is to extend jQuery with this tiny convenience:

$.fn.exists = function () {
    return this.length !== 0;
}

Used like:

$("#notAnElement").exists();

More explicit than using length.

486
ответ дан 23 November 2019 в 02:26
поделиться
if ( $("#anid").length ) {
  alert("element(s) found")
} 
else {
  alert("nothing found")
}
193
ответ дан 23 November 2019 в 02:26
поделиться

The selector returns an array of jQuery objects. If no matching elements are found, it returns an empty array. You can check the .length of the collection returned by the selector or check whether the first array element is 'undefined'.

You can use any the following examples inside an IF statement and they all produce the same result. True, if the selector found a matching element, false otherwise.

$('#notAnElement').length > 0
$('#notAnElement').get(0) !== undefined
$('#notAnElement')[0] !== undefined
66
ответ дан 23 November 2019 в 02:26
поделиться
Другие вопросы по тегам:

Похожие вопросы: