Better logging when Darwin.framework is asked to encode an incorrect type in a list. (#36599)

diff --git a/src/darwin/Framework/CHIP/templates/MTRBaseClusters-src.zapt b/src/darwin/Framework/CHIP/templates/MTRBaseClusters-src.zapt
index 0b8529b..f90acdd 100644
--- a/src/darwin/Framework/CHIP/templates/MTRBaseClusters-src.zapt
+++ b/src/darwin/Framework/CHIP/templates/MTRBaseClusters-src.zapt
@@ -9,6 +9,7 @@
 #import "MTRClusterStateCacheContainer_Internal.h"
 #import "MTRCommandPayloadsObjc.h"
 #import "MTRDevice_Internal.h"
+#import "MTRLogging_Internal.h"
 #import "MTRStructsObjc.h"
 #import "NSStringSpanConversion.h"
 #import "NSDataSpanConversion.h"
diff --git a/src/darwin/Framework/CHIP/templates/MTRCommandPayloadsObjc-src.zapt b/src/darwin/Framework/CHIP/templates/MTRCommandPayloadsObjc-src.zapt
index 4179d5a..41af262 100644
--- a/src/darwin/Framework/CHIP/templates/MTRCommandPayloadsObjc-src.zapt
+++ b/src/darwin/Framework/CHIP/templates/MTRCommandPayloadsObjc-src.zapt
@@ -4,6 +4,7 @@
 #import "MTRCommandPayloads_Internal.h"
 #import "MTRCommandPayloadExtensions_Internal.h"
 #import "MTRBaseDevice_Internal.h"
+#import "MTRDefines_Internal.h"
 #import "MTRError_Internal.h"
 #import "MTRLogging_Internal.h"
 #import "NSStringSpanConversion.h"
diff --git a/src/darwin/Framework/CHIP/templates/partials/encode_value.zapt b/src/darwin/Framework/CHIP/templates/partials/encode_value.zapt
index f2f4d07..16e2b3e 100644
--- a/src/darwin/Framework/CHIP/templates/partials/encode_value.zapt
+++ b/src/darwin/Framework/CHIP/templates/partials/encode_value.zapt
@@ -27,11 +27,12 @@
       }
       listFreer.add(listHolder_{{depth}});
       for (size_t i_{{depth}} = 0; i_{{depth}} < {{source}}.count; ++i_{{depth}}) {
-        if ( ! [{{source}}[i_{{depth}}] isKindOfClass: [{{asObjectiveCClass type cluster forceNotList=true}} class]] ) {
+        auto element_{{depth}} = MTR_SAFE_CAST({{source}}[i_{{depth}}], {{asObjectiveCClass type cluster forceNotList=true}});
+        if (!element_{{depth}}) {
           // Wrong kind of value.
+          MTR_LOG_ERROR("%@ incorrectly present in list of %@", {{source}}[i_{{depth}}], NSStringFromClass({{asObjectiveCClass type cluster forceNotList=true}}.class));
           {{errorCode}}
         }
-        auto element_{{depth}} = ({{asObjectiveCClass type cluster forceNotList=true}} *){{source}}[i_{{depth}}];
         {{>encode_value target=(concat "listHolder_" depth "->mList[i_" depth "]") source=(concat "element_" depth) cluster=cluster errorCode=errorCode depth=(incrementDepth depth) isArray=false}}
       }
       {{target}} = ListType_{{depth}}(listHolder_{{depth}}->mList, {{source}}.count);
diff --git a/src/darwin/Framework/CHIP/zap-generated/MTRBaseClusters.mm b/src/darwin/Framework/CHIP/zap-generated/MTRBaseClusters.mm
index ea02951..47d9cb1 100644
--- a/src/darwin/Framework/CHIP/zap-generated/MTRBaseClusters.mm
+++ b/src/darwin/Framework/CHIP/zap-generated/MTRBaseClusters.mm
@@ -25,6 +25,7 @@
 #import "MTRCommandPayloadsObjc.h"
 #import "MTRDefines_Internal.h"
 #import "MTRDevice_Internal.h"
+#import "MTRLogging_Internal.h"
 #import "MTRStructsObjc.h"
 #import "NSDataSpanConversion.h"
 #import "NSStringSpanConversion.h"
@@ -5173,11 +5174,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTRBindingClusterTargetStruct class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTRBindingClusterTargetStruct);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTRBindingClusterTargetStruct.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTRBindingClusterTargetStruct *)value[i_0];
                        if (element_0.node != nil) {
          auto & definedValue_2 = listHolder_0->mList[i_0].node.Emplace();
                  definedValue_2 = element_0.node.unsignedLongLongValue;
@@ -5712,11 +5714,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTRAccessControlClusterAccessControlEntryStruct class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTRAccessControlClusterAccessControlEntryStruct);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTRAccessControlClusterAccessControlEntryStruct.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTRAccessControlClusterAccessControlEntryStruct *)value[i_0];
                            listHolder_0->mList[i_0].privilege = static_cast<std::remove_reference_t<decltype(listHolder_0->mList[i_0].privilege)>>(element_0.privilege.unsignedCharValue);
            listHolder_0->mList[i_0].authMode = static_cast<std::remove_reference_t<decltype(listHolder_0->mList[i_0].authMode)>>(element_0.authMode.unsignedCharValue);
        if (element_0.subjects == nil) {
@@ -5733,11 +5736,12 @@
                }
                listFreer.add(listHolder_3);
                for (size_t i_3 = 0; i_3 < element_0.subjects.count; ++i_3) {
-                 if ( ! [element_0.subjects[i_3] isKindOfClass: [NSNumber class]] ) {
+                 auto element_3 = MTR_SAFE_CAST(element_0.subjects[i_3], NSNumber);
+                 if (!element_3) {
                    // Wrong kind of value.
+                   MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.subjects[i_3], NSStringFromClass(NSNumber.class));
                    return CHIP_ERROR_INVALID_ARGUMENT;
                  }
-                 auto element_3 = (NSNumber *)element_0.subjects[i_3];
                          listHolder_3->mList[i_3] = element_3.unsignedLongLongValue;
       }
                nonNullValue_2 = ListType_3(listHolder_3->mList, element_0.subjects.count);
@@ -5760,11 +5764,12 @@
                }
                listFreer.add(listHolder_3);
                for (size_t i_3 = 0; i_3 < element_0.targets.count; ++i_3) {
-                 if ( ! [element_0.targets[i_3] isKindOfClass: [MTRAccessControlClusterAccessControlTargetStruct class]] ) {
+                 auto element_3 = MTR_SAFE_CAST(element_0.targets[i_3], MTRAccessControlClusterAccessControlTargetStruct);
+                 if (!element_3) {
                    // Wrong kind of value.
+                   MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.targets[i_3], NSStringFromClass(MTRAccessControlClusterAccessControlTargetStruct.class));
                    return CHIP_ERROR_INVALID_ARGUMENT;
                  }
-                 auto element_3 = (MTRAccessControlClusterAccessControlTargetStruct *)element_0.targets[i_3];
                         if (element_3.cluster == nil) {
          listHolder_3->mList[i_3].cluster.SetNull();
        } else {
@@ -5870,11 +5875,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTRAccessControlClusterAccessControlExtensionStruct class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTRAccessControlClusterAccessControlExtensionStruct);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTRAccessControlClusterAccessControlExtensionStruct.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTRAccessControlClusterAccessControlExtensionStruct *)value[i_0];
                        listHolder_0->mList[i_0].data = AsByteSpan(element_0.data);
              listHolder_0->mList[i_0].fabricIndex = element_0.fabricIndex.unsignedCharValue;
       }
@@ -10171,11 +10177,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTROTASoftwareUpdateRequestorClusterProviderLocation class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTROTASoftwareUpdateRequestorClusterProviderLocation);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTROTASoftwareUpdateRequestorClusterProviderLocation.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTROTASoftwareUpdateRequestorClusterProviderLocation *)value[i_0];
                              listHolder_0->mList[i_0].providerNodeID = element_0.providerNodeID.unsignedLongLongValue;
              listHolder_0->mList[i_0].endpoint = element_0.endpoint.unsignedShortValue;
              listHolder_0->mList[i_0].fabricIndex = element_0.fabricIndex.unsignedCharValue;
@@ -32215,11 +32222,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTRGroupKeyManagementClusterGroupKeyMapStruct class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTRGroupKeyManagementClusterGroupKeyMapStruct);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTRGroupKeyManagementClusterGroupKeyMapStruct.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTRGroupKeyManagementClusterGroupKeyMapStruct *)value[i_0];
                              listHolder_0->mList[i_0].groupId = element_0.groupId.unsignedShortValue;
              listHolder_0->mList[i_0].groupKeySetID = element_0.groupKeySetID.unsignedShortValue;
              listHolder_0->mList[i_0].fabricIndex = element_0.fabricIndex.unsignedCharValue;
@@ -33396,11 +33404,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTRUserLabelClusterLabelStruct class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTRUserLabelClusterLabelStruct);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTRUserLabelClusterLabelStruct.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTRUserLabelClusterLabelStruct *)value[i_0];
                        listHolder_0->mList[i_0].label = AsCharSpan(element_0.label);
        listHolder_0->mList[i_0].value = AsCharSpan(element_0.value);
       }
@@ -64721,11 +64730,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTRThermostatClusterPresetStruct class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTRThermostatClusterPresetStruct);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTRThermostatClusterPresetStruct.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTRThermostatClusterPresetStruct *)value[i_0];
                        if (element_0.presetHandle == nil) {
          listHolder_0->mList[i_0].presetHandle.SetNull();
        } else {
@@ -64835,11 +64845,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTRThermostatClusterScheduleStruct class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTRThermostatClusterScheduleStruct);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTRThermostatClusterScheduleStruct.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTRThermostatClusterScheduleStruct *)value[i_0];
                        if (element_0.scheduleHandle == nil) {
          listHolder_0->mList[i_0].scheduleHandle.SetNull();
        } else {
@@ -64865,11 +64876,12 @@
            }
            listFreer.add(listHolder_2);
            for (size_t i_2 = 0; i_2 < element_0.transitions.count; ++i_2) {
-             if ( ! [element_0.transitions[i_2] isKindOfClass: [MTRThermostatClusterScheduleTransitionStruct class]] ) {
+             auto element_2 = MTR_SAFE_CAST(element_0.transitions[i_2], MTRThermostatClusterScheduleTransitionStruct);
+             if (!element_2) {
                // Wrong kind of value.
+               MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.transitions[i_2], NSStringFromClass(MTRThermostatClusterScheduleTransitionStruct.class));
                return CHIP_ERROR_INVALID_ARGUMENT;
              }
-             auto element_2 = (MTRThermostatClusterScheduleTransitionStruct *)element_0.transitions[i_2];
                           listHolder_2->mList[i_2].dayOfWeek = static_cast<std::remove_reference_t<decltype(listHolder_2->mList[i_2].dayOfWeek)>>(element_2.dayOfWeek.unsignedCharValue);
              listHolder_2->mList[i_2].transitionTime = element_2.transitionTime.unsignedShortValue;
        if (element_2.presetHandle != nil) {
@@ -97820,11 +97832,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTRZoneManagementClusterZoneTriggerControlStruct class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTRZoneManagementClusterZoneTriggerControlStruct);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTRZoneManagementClusterZoneTriggerControlStruct.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTRZoneManagementClusterZoneTriggerControlStruct *)value[i_0];
                              listHolder_0->mList[i_0].initialDuration = element_0.initialDuration.unsignedShortValue;
              listHolder_0->mList[i_0].augmentationDuration = element_0.augmentationDuration.unsignedShortValue;
              listHolder_0->mList[i_0].maxDuration = element_0.maxDuration.unsignedIntValue;
@@ -104448,11 +104461,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [NSNumber class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], NSNumber);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(NSNumber.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (NSNumber *)value[i_0];
                         listHolder_0->mList[i_0] = element_0.unsignedCharValue;
       }
               cppValue = ListType_0(listHolder_0->mList, value.count);
@@ -104533,11 +104547,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [NSData class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], NSData);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(NSData.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (NSData *)value[i_0];
                   listHolder_0->mList[i_0] = AsByteSpan(element_0);
               }
               cppValue = ListType_0(listHolder_0->mList, value.count);
@@ -104618,11 +104633,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTRUnitTestingClusterTestListStructOctet class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTRUnitTestingClusterTestListStructOctet);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTRUnitTestingClusterTestListStructOctet.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTRUnitTestingClusterTestListStructOctet *)value[i_0];
                              listHolder_0->mList[i_0].member1 = element_0.member1.unsignedLongLongValue;
        listHolder_0->mList[i_0].member2 = AsByteSpan(element_0.member2);
       }
@@ -105088,11 +105104,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTRUnitTestingClusterNullablesAndOptionalsStruct class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTRUnitTestingClusterNullablesAndOptionalsStruct);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTRUnitTestingClusterNullablesAndOptionalsStruct.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTRUnitTestingClusterNullablesAndOptionalsStruct *)value[i_0];
                        if (element_0.nullableInt == nil) {
          listHolder_0->mList[i_0].nullableInt.SetNull();
        } else {
@@ -105197,11 +105214,12 @@
                }
                listFreer.add(listHolder_3);
                for (size_t i_3 = 0; i_3 < element_0.nullableList.count; ++i_3) {
-                 if ( ! [element_0.nullableList[i_3] isKindOfClass: [NSNumber class]] ) {
+                 auto element_3 = MTR_SAFE_CAST(element_0.nullableList[i_3], NSNumber);
+                 if (!element_3) {
                    // Wrong kind of value.
+                   MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.nullableList[i_3], NSStringFromClass(NSNumber.class));
                    return CHIP_ERROR_INVALID_ARGUMENT;
                  }
-                 auto element_3 = (NSNumber *)element_0.nullableList[i_3];
                        listHolder_3->mList[i_3] = static_cast<std::remove_reference_t<decltype(listHolder_3->mList[i_3])>>(element_3.unsignedCharValue);
       }
                nonNullValue_2 = ListType_3(listHolder_3->mList, element_0.nullableList.count);
@@ -105222,11 +105240,12 @@
                }
                listFreer.add(listHolder_3);
                for (size_t i_3 = 0; i_3 < element_0.optionalList.count; ++i_3) {
-                 if ( ! [element_0.optionalList[i_3] isKindOfClass: [NSNumber class]] ) {
+                 auto element_3 = MTR_SAFE_CAST(element_0.optionalList[i_3], NSNumber);
+                 if (!element_3) {
                    // Wrong kind of value.
+                   MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.optionalList[i_3], NSStringFromClass(NSNumber.class));
                    return CHIP_ERROR_INVALID_ARGUMENT;
                  }
-                 auto element_3 = (NSNumber *)element_0.optionalList[i_3];
                        listHolder_3->mList[i_3] = static_cast<std::remove_reference_t<decltype(listHolder_3->mList[i_3])>>(element_3.unsignedCharValue);
       }
                definedValue_2 = ListType_3(listHolder_3->mList, element_0.optionalList.count);
@@ -105251,11 +105270,12 @@
                    }
                    listFreer.add(listHolder_4);
                    for (size_t i_4 = 0; i_4 < element_0.nullableOptionalList.count; ++i_4) {
-                     if ( ! [element_0.nullableOptionalList[i_4] isKindOfClass: [NSNumber class]] ) {
+                     auto element_4 = MTR_SAFE_CAST(element_0.nullableOptionalList[i_4], NSNumber);
+                     if (!element_4) {
                        // Wrong kind of value.
+                       MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.nullableOptionalList[i_4], NSStringFromClass(NSNumber.class));
                        return CHIP_ERROR_INVALID_ARGUMENT;
                      }
-                     auto element_4 = (NSNumber *)element_0.nullableOptionalList[i_4];
                            listHolder_4->mList[i_4] = static_cast<std::remove_reference_t<decltype(listHolder_4->mList[i_4])>>(element_4.unsignedCharValue);
       }
                    nonNullValue_3 = ListType_4(listHolder_4->mList, element_0.nullableOptionalList.count);
@@ -105739,11 +105759,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [NSData class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], NSData);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(NSData.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (NSData *)value[i_0];
                   listHolder_0->mList[i_0] = AsByteSpan(element_0);
               }
               cppValue = ListType_0(listHolder_0->mList, value.count);
@@ -105824,11 +105845,12 @@
               }
               listFreer.add(listHolder_0);
               for (size_t i_0 = 0; i_0 < value.count; ++i_0) {
-                if ( ! [value[i_0] isKindOfClass: [MTRUnitTestingClusterTestFabricScoped class]] ) {
+                auto element_0 = MTR_SAFE_CAST(value[i_0], MTRUnitTestingClusterTestFabricScoped);
+                if (!element_0) {
                   // Wrong kind of value.
+                  MTR_LOG_ERROR("%@ incorrectly present in list of %@", value[i_0], NSStringFromClass(MTRUnitTestingClusterTestFabricScoped.class));
                   return CHIP_ERROR_INVALID_ARGUMENT;
                 }
-                auto element_0 = (MTRUnitTestingClusterTestFabricScoped *)value[i_0];
                              listHolder_0->mList[i_0].fabricSensitiveInt8u = element_0.fabricSensitiveInt8u.unsignedCharValue;
        if (element_0.optionalFabricSensitiveInt8u != nil) {
          auto & definedValue_2 = listHolder_0->mList[i_0].optionalFabricSensitiveInt8u.Emplace();
@@ -105872,11 +105894,12 @@
            }
            listFreer.add(listHolder_2);
            for (size_t i_2 = 0; i_2 < element_0.fabricSensitiveInt8uList.count; ++i_2) {
-             if ( ! [element_0.fabricSensitiveInt8uList[i_2] isKindOfClass: [NSNumber class]] ) {
+             auto element_2 = MTR_SAFE_CAST(element_0.fabricSensitiveInt8uList[i_2], NSNumber);
+             if (!element_2) {
                // Wrong kind of value.
+               MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.fabricSensitiveInt8uList[i_2], NSStringFromClass(NSNumber.class));
                return CHIP_ERROR_INVALID_ARGUMENT;
              }
-             auto element_2 = (NSNumber *)element_0.fabricSensitiveInt8uList[i_2];
                      listHolder_2->mList[i_2] = element_2.unsignedCharValue;
       }
            listHolder_0->mList[i_0].fabricSensitiveInt8uList = ListType_2(listHolder_2->mList, element_0.fabricSensitiveInt8uList.count);
diff --git a/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloadsObjc.mm b/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloadsObjc.mm
index 6d8ddd6..318455a 100644
--- a/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloadsObjc.mm
+++ b/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloadsObjc.mm
@@ -20,6 +20,7 @@
 #import "MTRBaseDevice_Internal.h"
 #import "MTRCommandPayloadExtensions_Internal.h"
 #import "MTRCommandPayloads_Internal.h"
+#import "MTRDefines_Internal.h"
 #import "MTRError_Internal.h"
 #import "MTRLogging_Internal.h"
 #import "NSDataSpanConversion.h"
@@ -641,11 +642,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.groupList.count; ++i_0) {
-                    if (![self.groupList[i_0] isKindOfClass:[NSNumber class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.groupList[i_0], NSNumber);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.groupList[i_0], NSStringFromClass(NSNumber.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (NSNumber *) self.groupList[i_0];
                     listHolder_0->mList[i_0] = element_0.unsignedShortValue;
                 }
                 encodableStruct.groupList = ListType_0(listHolder_0->mList, self.groupList.count);
@@ -2559,11 +2561,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.arl.count; ++i_0) {
-                    if (![self.arl[i_0] isKindOfClass:[MTRAccessControlClusterCommissioningAccessRestrictionEntryStruct class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.arl[i_0], MTRAccessControlClusterCommissioningAccessRestrictionEntryStruct);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arl[i_0], NSStringFromClass(MTRAccessControlClusterCommissioningAccessRestrictionEntryStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRAccessControlClusterCommissioningAccessRestrictionEntryStruct *) self.arl[i_0];
                     listHolder_0->mList[i_0].endpoint = element_0.endpoint.unsignedShortValue;
                     listHolder_0->mList[i_0].cluster = element_0.cluster.unsignedIntValue;
                     {
@@ -2576,11 +2579,12 @@
                             }
                             listFreer.add(listHolder_2);
                             for (size_t i_2 = 0; i_2 < element_0.restrictions.count; ++i_2) {
-                                if (![element_0.restrictions[i_2] isKindOfClass:[MTRAccessControlClusterAccessRestrictionStruct class]]) {
+                                auto element_2 = MTR_SAFE_CAST(element_0.restrictions[i_2], MTRAccessControlClusterAccessRestrictionStruct);
+                                if (!element_2) {
                                     // Wrong kind of value.
+                                    MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.restrictions[i_2], NSStringFromClass(MTRAccessControlClusterAccessRestrictionStruct.class));
                                     return CHIP_ERROR_INVALID_ARGUMENT;
                                 }
-                                auto element_2 = (MTRAccessControlClusterAccessRestrictionStruct *) element_0.restrictions[i_2];
                                 listHolder_2->mList[i_2].type = static_cast<std::remove_reference_t<decltype(listHolder_2->mList[i_2].type)>>(element_2.type.unsignedCharValue);
                                 if (element_2.id == nil) {
                                     listHolder_2->mList[i_2].id.SetNull();
@@ -3910,11 +3914,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.protocolsSupported.count; ++i_0) {
-                    if (![self.protocolsSupported[i_0] isKindOfClass:[NSNumber class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.protocolsSupported[i_0], NSNumber);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.protocolsSupported[i_0], NSStringFromClass(NSNumber.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (NSNumber *) self.protocolsSupported[i_0];
                     listHolder_0->mList[i_0] = static_cast<std::remove_reference_t<decltype(listHolder_0->mList[i_0])>>(element_0.unsignedCharValue);
                 }
                 encodableStruct.protocolsSupported = ListType_0(listHolder_0->mList, self.protocolsSupported.count);
@@ -7601,11 +7606,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.timeZone.count; ++i_0) {
-                    if (![self.timeZone[i_0] isKindOfClass:[MTRTimeSynchronizationClusterTimeZoneStruct class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.timeZone[i_0], MTRTimeSynchronizationClusterTimeZoneStruct);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.timeZone[i_0], NSStringFromClass(MTRTimeSynchronizationClusterTimeZoneStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRTimeSynchronizationClusterTimeZoneStruct *) self.timeZone[i_0];
                     listHolder_0->mList[i_0].offset = element_0.offset.intValue;
                     listHolder_0->mList[i_0].validAt = element_0.validAt.unsignedLongLongValue;
                     if (element_0.name != nil) {
@@ -7785,11 +7791,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.dstOffset.count; ++i_0) {
-                    if (![self.dstOffset[i_0] isKindOfClass:[MTRTimeSynchronizationClusterDSTOffsetStruct class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.dstOffset[i_0], MTRTimeSynchronizationClusterDSTOffsetStruct);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.dstOffset[i_0], NSStringFromClass(MTRTimeSynchronizationClusterDSTOffsetStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRTimeSynchronizationClusterDSTOffsetStruct *) self.dstOffset[i_0];
                     listHolder_0->mList[i_0].offset = element_0.offset.intValue;
                     listHolder_0->mList[i_0].validStarting = element_0.validStarting.unsignedLongLongValue;
                     if (element_0.validUntil == nil) {
@@ -13509,11 +13516,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.extensionFieldSets.count; ++i_0) {
-                    if (![self.extensionFieldSets[i_0] isKindOfClass:[MTRScenesManagementClusterExtensionFieldSet class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.extensionFieldSets[i_0], MTRScenesManagementClusterExtensionFieldSet);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.extensionFieldSets[i_0], NSStringFromClass(MTRScenesManagementClusterExtensionFieldSet.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRScenesManagementClusterExtensionFieldSet *) self.extensionFieldSets[i_0];
                     listHolder_0->mList[i_0].clusterID = element_0.clusterID.unsignedIntValue;
                     {
                         using ListType_2 = std::remove_reference_t<decltype(listHolder_0->mList[i_0].attributeValueList)>;
@@ -13525,11 +13533,12 @@
                             }
                             listFreer.add(listHolder_2);
                             for (size_t i_2 = 0; i_2 < element_0.attributeValueList.count; ++i_2) {
-                                if (![element_0.attributeValueList[i_2] isKindOfClass:[MTRScenesManagementClusterAttributeValuePairStruct class]]) {
+                                auto element_2 = MTR_SAFE_CAST(element_0.attributeValueList[i_2], MTRScenesManagementClusterAttributeValuePairStruct);
+                                if (!element_2) {
                                     // Wrong kind of value.
+                                    MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.attributeValueList[i_2], NSStringFromClass(MTRScenesManagementClusterAttributeValuePairStruct.class));
                                     return CHIP_ERROR_INVALID_ARGUMENT;
                                 }
-                                auto element_2 = (MTRScenesManagementClusterAttributeValuePairStruct *) element_0.attributeValueList[i_2];
                                 listHolder_2->mList[i_2].attributeID = element_2.attributeID.unsignedIntValue;
                                 if (element_2.valueUnsigned8 != nil) {
                                     auto & definedValue_4 = listHolder_2->mList[i_2].valueUnsigned8.Emplace();
@@ -15884,11 +15893,12 @@
                 }
                 listFreer.add(listHolder_1);
                 for (size_t i_1 = 0; i_1 < self.event.transitions.count; ++i_1) {
-                    if (![self.event.transitions[i_1] isKindOfClass:[MTRDemandResponseLoadControlClusterLoadControlEventTransitionStruct class]]) {
+                    auto element_1 = MTR_SAFE_CAST(self.event.transitions[i_1], MTRDemandResponseLoadControlClusterLoadControlEventTransitionStruct);
+                    if (!element_1) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.event.transitions[i_1], NSStringFromClass(MTRDemandResponseLoadControlClusterLoadControlEventTransitionStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_1 = (MTRDemandResponseLoadControlClusterLoadControlEventTransitionStruct *) self.event.transitions[i_1];
                     listHolder_1->mList[i_1].duration = element_1.duration.unsignedShortValue;
                     listHolder_1->mList[i_1].control = static_cast<std::remove_reference_t<decltype(listHolder_1->mList[i_1].control)>>(element_1.control.unsignedShortValue);
                     if (element_1.temperatureControl != nil) {
@@ -16246,11 +16256,12 @@
                     }
                     listFreer.add(listHolder_1);
                     for (size_t i_1 = 0; i_1 < self.responses.count; ++i_1) {
-                        if (![self.responses[i_1] isKindOfClass:[MTRMessagesClusterMessageResponseOptionStruct class]]) {
+                        auto element_1 = MTR_SAFE_CAST(self.responses[i_1], MTRMessagesClusterMessageResponseOptionStruct);
+                        if (!element_1) {
                             // Wrong kind of value.
+                            MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.responses[i_1], NSStringFromClass(MTRMessagesClusterMessageResponseOptionStruct.class));
                             return CHIP_ERROR_INVALID_ARGUMENT;
                         }
-                        auto element_1 = (MTRMessagesClusterMessageResponseOptionStruct *) self.responses[i_1];
                         if (element_1.messageResponseID != nil) {
                             auto & definedValue_3 = listHolder_1->mList[i_1].messageResponseID.Emplace();
                             definedValue_3 = element_1.messageResponseID.unsignedIntValue;
@@ -16354,11 +16365,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.messageIDs.count; ++i_0) {
-                    if (![self.messageIDs[i_0] isKindOfClass:[NSData class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.messageIDs[i_0], NSData);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.messageIDs[i_0], NSStringFromClass(NSData.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (NSData *) self.messageIDs[i_0];
                     listHolder_0->mList[i_0] = AsByteSpan(element_0);
                 }
                 encodableStruct.messageIDs = ListType_0(listHolder_0->mList, self.messageIDs.count);
@@ -16870,11 +16882,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.slotAdjustments.count; ++i_0) {
-                    if (![self.slotAdjustments[i_0] isKindOfClass:[MTRDeviceEnergyManagementClusterSlotAdjustmentStruct class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.slotAdjustments[i_0], MTRDeviceEnergyManagementClusterSlotAdjustmentStruct);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.slotAdjustments[i_0], NSStringFromClass(MTRDeviceEnergyManagementClusterSlotAdjustmentStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRDeviceEnergyManagementClusterSlotAdjustmentStruct *) self.slotAdjustments[i_0];
                     listHolder_0->mList[i_0].slotIndex = element_0.slotIndex.unsignedCharValue;
                     if (element_0.nominalPower != nil) {
                         auto & definedValue_2 = listHolder_0->mList[i_0].nominalPower.Emplace();
@@ -16981,11 +16994,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.constraints.count; ++i_0) {
-                    if (![self.constraints[i_0] isKindOfClass:[MTRDeviceEnergyManagementClusterConstraintsStruct class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.constraints[i_0], MTRDeviceEnergyManagementClusterConstraintsStruct);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.constraints[i_0], NSStringFromClass(MTRDeviceEnergyManagementClusterConstraintsStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRDeviceEnergyManagementClusterConstraintsStruct *) self.constraints[i_0];
                     listHolder_0->mList[i_0].startTime = element_0.startTime.unsignedIntValue;
                     listHolder_0->mList[i_0].duration = element_0.duration.unsignedIntValue;
                     if (element_0.nominalPower != nil) {
@@ -17622,11 +17636,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.chargingTargetSchedules.count; ++i_0) {
-                    if (![self.chargingTargetSchedules[i_0] isKindOfClass:[MTREnergyEVSEClusterChargingTargetScheduleStruct class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.chargingTargetSchedules[i_0], MTREnergyEVSEClusterChargingTargetScheduleStruct);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.chargingTargetSchedules[i_0], NSStringFromClass(MTREnergyEVSEClusterChargingTargetScheduleStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTREnergyEVSEClusterChargingTargetScheduleStruct *) self.chargingTargetSchedules[i_0];
                     listHolder_0->mList[i_0].dayOfWeekForSequence = static_cast<std::remove_reference_t<decltype(listHolder_0->mList[i_0].dayOfWeekForSequence)>>(element_0.dayOfWeekForSequence.unsignedCharValue);
                     {
                         using ListType_2 = std::remove_reference_t<decltype(listHolder_0->mList[i_0].chargingTargets)>;
@@ -17638,11 +17653,12 @@
                             }
                             listFreer.add(listHolder_2);
                             for (size_t i_2 = 0; i_2 < element_0.chargingTargets.count; ++i_2) {
-                                if (![element_0.chargingTargets[i_2] isKindOfClass:[MTREnergyEVSEClusterChargingTargetStruct class]]) {
+                                auto element_2 = MTR_SAFE_CAST(element_0.chargingTargets[i_2], MTREnergyEVSEClusterChargingTargetStruct);
+                                if (!element_2) {
                                     // Wrong kind of value.
+                                    MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.chargingTargets[i_2], NSStringFromClass(MTREnergyEVSEClusterChargingTargetStruct.class));
                                     return CHIP_ERROR_INVALID_ARGUMENT;
                                 }
-                                auto element_2 = (MTREnergyEVSEClusterChargingTargetStruct *) element_0.chargingTargets[i_2];
                                 listHolder_2->mList[i_2].targetTimeMinutesPastMidnight = element_2.targetTimeMinutesPastMidnight.unsignedShortValue;
                                 if (element_2.targetSoC != nil) {
                                     auto & definedValue_4 = listHolder_2->mList[i_2].targetSoC.Emplace();
@@ -21675,11 +21691,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.newAreas.count; ++i_0) {
-                    if (![self.newAreas[i_0] isKindOfClass:[NSNumber class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.newAreas[i_0], NSNumber);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.newAreas[i_0], NSStringFromClass(NSNumber.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (NSNumber *) self.newAreas[i_0];
                     listHolder_0->mList[i_0] = element_0.unsignedIntValue;
                 }
                 encodableStruct.newAreas = ListType_0(listHolder_0->mList, self.newAreas.count);
@@ -22259,11 +22276,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.transitions.count; ++i_0) {
-                    if (![self.transitions[i_0] isKindOfClass:[MTRThermostatClusterWeeklyScheduleTransitionStruct class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.transitions[i_0], MTRThermostatClusterWeeklyScheduleTransitionStruct);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.transitions[i_0], NSStringFromClass(MTRThermostatClusterWeeklyScheduleTransitionStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRThermostatClusterWeeklyScheduleTransitionStruct *) self.transitions[i_0];
                     listHolder_0->mList[i_0].transitionTime = element_0.transitionTime.unsignedShortValue;
                     if (element_0.heatSetpoint == nil) {
                         listHolder_0->mList[i_0].heatSetpoint.SetNull();
@@ -22812,11 +22830,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.attributeRequests.count; ++i_0) {
-                    if (![self.attributeRequests[i_0] isKindOfClass:[NSNumber class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.attributeRequests[i_0], NSNumber);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.attributeRequests[i_0], NSStringFromClass(NSNumber.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (NSNumber *) self.attributeRequests[i_0];
                     listHolder_0->mList[i_0] = element_0.unsignedIntValue;
                 }
                 encodableStruct.attributeRequests = ListType_0(listHolder_0->mList, self.attributeRequests.count);
@@ -26190,11 +26209,12 @@
                     }
                     listFreer.add(listHolder_1);
                     for (size_t i_1 = 0; i_1 < self.channelList.count; ++i_1) {
-                        if (![self.channelList[i_1] isKindOfClass:[MTRChannelClusterChannelInfoStruct class]]) {
+                        auto element_1 = MTR_SAFE_CAST(self.channelList[i_1], MTRChannelClusterChannelInfoStruct);
+                        if (!element_1) {
                             // Wrong kind of value.
+                            MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.channelList[i_1], NSStringFromClass(MTRChannelClusterChannelInfoStruct.class));
                             return CHIP_ERROR_INVALID_ARGUMENT;
                         }
-                        auto element_1 = (MTRChannelClusterChannelInfoStruct *) self.channelList[i_1];
                         listHolder_1->mList[i_1].majorNumber = element_1.majorNumber.unsignedShortValue;
                         listHolder_1->mList[i_1].minorNumber = element_1.minorNumber.unsignedShortValue;
                         if (element_1.name != nil) {
@@ -26261,11 +26281,12 @@
                     }
                     listFreer.add(listHolder_1);
                     for (size_t i_1 = 0; i_1 < self.externalIDList.count; ++i_1) {
-                        if (![self.externalIDList[i_1] isKindOfClass:[MTRChannelClusterAdditionalInfoStruct class]]) {
+                        auto element_1 = MTR_SAFE_CAST(self.externalIDList[i_1], MTRChannelClusterAdditionalInfoStruct);
+                        if (!element_1) {
                             // Wrong kind of value.
+                            MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.externalIDList[i_1], NSStringFromClass(MTRChannelClusterAdditionalInfoStruct.class));
                             return CHIP_ERROR_INVALID_ARGUMENT;
                         }
-                        auto element_1 = (MTRChannelClusterAdditionalInfoStruct *) self.externalIDList[i_1];
                         listHolder_1->mList[i_1].name = AsCharSpan(element_1.name);
                         listHolder_1->mList[i_1].value = AsCharSpan(element_1.value);
                     }
@@ -26827,11 +26848,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.externalIDList.count; ++i_0) {
-                    if (![self.externalIDList[i_0] isKindOfClass:[MTRChannelClusterAdditionalInfoStruct class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.externalIDList[i_0], MTRChannelClusterAdditionalInfoStruct);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.externalIDList[i_0], NSStringFromClass(MTRChannelClusterAdditionalInfoStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRChannelClusterAdditionalInfoStruct *) self.externalIDList[i_0];
                     listHolder_0->mList[i_0].name = AsCharSpan(element_0.name);
                     listHolder_0->mList[i_0].value = AsCharSpan(element_0.value);
                 }
@@ -26946,11 +26968,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.externalIDList.count; ++i_0) {
-                    if (![self.externalIDList[i_0] isKindOfClass:[MTRChannelClusterAdditionalInfoStruct class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.externalIDList[i_0], MTRChannelClusterAdditionalInfoStruct);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.externalIDList[i_0], NSStringFromClass(MTRChannelClusterAdditionalInfoStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRChannelClusterAdditionalInfoStruct *) self.externalIDList[i_0];
                     listHolder_0->mList[i_0].name = AsCharSpan(element_0.name);
                     listHolder_0->mList[i_0].value = AsCharSpan(element_0.value);
                 }
@@ -28964,11 +28987,12 @@
                 }
                 listFreer.add(listHolder_1);
                 for (size_t i_1 = 0; i_1 < self.search.parameterList.count; ++i_1) {
-                    if (![self.search.parameterList[i_1] isKindOfClass:[MTRContentLauncherClusterParameterStruct class]]) {
+                    auto element_1 = MTR_SAFE_CAST(self.search.parameterList[i_1], MTRContentLauncherClusterParameterStruct);
+                    if (!element_1) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.search.parameterList[i_1], NSStringFromClass(MTRContentLauncherClusterParameterStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_1 = (MTRContentLauncherClusterParameterStruct *) self.search.parameterList[i_1];
                     listHolder_1->mList[i_1].type = static_cast<std::remove_reference_t<decltype(listHolder_1->mList[i_1].type)>>(element_1.type.unsignedCharValue);
                     listHolder_1->mList[i_1].value = AsCharSpan(element_1.value);
                     if (element_1.externalIDList != nil) {
@@ -28983,11 +29007,12 @@
                                 }
                                 listFreer.add(listHolder_4);
                                 for (size_t i_4 = 0; i_4 < element_1.externalIDList.count; ++i_4) {
-                                    if (![element_1.externalIDList[i_4] isKindOfClass:[MTRContentLauncherClusterAdditionalInfoStruct class]]) {
+                                    auto element_4 = MTR_SAFE_CAST(element_1.externalIDList[i_4], MTRContentLauncherClusterAdditionalInfoStruct);
+                                    if (!element_4) {
                                         // Wrong kind of value.
+                                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_1.externalIDList[i_4], NSStringFromClass(MTRContentLauncherClusterAdditionalInfoStruct.class));
                                         return CHIP_ERROR_INVALID_ARGUMENT;
                                     }
-                                    auto element_4 = (MTRContentLauncherClusterAdditionalInfoStruct *) element_1.externalIDList[i_4];
                                     listHolder_4->mList[i_4].name = AsCharSpan(element_4.name);
                                     listHolder_4->mList[i_4].value = AsCharSpan(element_4.value);
                                 }
@@ -29030,11 +29055,12 @@
                         }
                         listFreer.add(listHolder_4);
                         for (size_t i_4 = 0; i_4 < self.playbackPreferences.textTrack.characteristics.count; ++i_4) {
-                            if (![self.playbackPreferences.textTrack.characteristics[i_4] isKindOfClass:[NSNumber class]]) {
+                            auto element_4 = MTR_SAFE_CAST(self.playbackPreferences.textTrack.characteristics[i_4], NSNumber);
+                            if (!element_4) {
                                 // Wrong kind of value.
+                                MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.playbackPreferences.textTrack.characteristics[i_4], NSStringFromClass(NSNumber.class));
                                 return CHIP_ERROR_INVALID_ARGUMENT;
                             }
-                            auto element_4 = (NSNumber *) self.playbackPreferences.textTrack.characteristics[i_4];
                             listHolder_4->mList[i_4] = static_cast<std::remove_reference_t<decltype(listHolder_4->mList[i_4])>>(element_4.unsignedCharValue);
                         }
                         definedValue_3 = ListType_4(listHolder_4->mList, self.playbackPreferences.textTrack.characteristics.count);
@@ -29056,11 +29082,12 @@
                         }
                         listFreer.add(listHolder_3);
                         for (size_t i_3 = 0; i_3 < self.playbackPreferences.audioTracks.count; ++i_3) {
-                            if (![self.playbackPreferences.audioTracks[i_3] isKindOfClass:[MTRContentLauncherClusterTrackPreferenceStruct class]]) {
+                            auto element_3 = MTR_SAFE_CAST(self.playbackPreferences.audioTracks[i_3], MTRContentLauncherClusterTrackPreferenceStruct);
+                            if (!element_3) {
                                 // Wrong kind of value.
+                                MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.playbackPreferences.audioTracks[i_3], NSStringFromClass(MTRContentLauncherClusterTrackPreferenceStruct.class));
                                 return CHIP_ERROR_INVALID_ARGUMENT;
                             }
-                            auto element_3 = (MTRContentLauncherClusterTrackPreferenceStruct *) self.playbackPreferences.audioTracks[i_3];
                             listHolder_3->mList[i_3].languageCode = AsCharSpan(element_3.languageCode);
                             if (element_3.characteristics != nil) {
                                 auto & definedValue_5 = listHolder_3->mList[i_3].characteristics.Emplace();
@@ -29074,11 +29101,12 @@
                                         }
                                         listFreer.add(listHolder_6);
                                         for (size_t i_6 = 0; i_6 < element_3.characteristics.count; ++i_6) {
-                                            if (![element_3.characteristics[i_6] isKindOfClass:[NSNumber class]]) {
+                                            auto element_6 = MTR_SAFE_CAST(element_3.characteristics[i_6], NSNumber);
+                                            if (!element_6) {
                                                 // Wrong kind of value.
+                                                MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_3.characteristics[i_6], NSStringFromClass(NSNumber.class));
                                                 return CHIP_ERROR_INVALID_ARGUMENT;
                                             }
-                                            auto element_6 = (NSNumber *) element_3.characteristics[i_6];
                                             listHolder_6->mList[i_6] = static_cast<std::remove_reference_t<decltype(listHolder_6->mList[i_6])>>(element_6.unsignedCharValue);
                                         }
                                         definedValue_5 = ListType_6(listHolder_6->mList, element_3.characteristics.count);
@@ -31387,11 +31415,12 @@
                 }
                 listFreer.add(listHolder_1);
                 for (size_t i_1 = 0; i_1 < self.zone.vertices.count; ++i_1) {
-                    if (![self.zone.vertices[i_1] isKindOfClass:[MTRZoneManagementClusterTwoDCartesianVertexStruct class]]) {
+                    auto element_1 = MTR_SAFE_CAST(self.zone.vertices[i_1], MTRZoneManagementClusterTwoDCartesianVertexStruct);
+                    if (!element_1) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.zone.vertices[i_1], NSStringFromClass(MTRZoneManagementClusterTwoDCartesianVertexStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_1 = (MTRZoneManagementClusterTwoDCartesianVertexStruct *) self.zone.vertices[i_1];
                     listHolder_1->mList[i_1].x = element_1.x.unsignedShortValue;
                     listHolder_1->mList[i_1].y = element_1.y.unsignedShortValue;
                 }
@@ -31579,11 +31608,12 @@
                 }
                 listFreer.add(listHolder_1);
                 for (size_t i_1 = 0; i_1 < self.zone.vertices.count; ++i_1) {
-                    if (![self.zone.vertices[i_1] isKindOfClass:[MTRZoneManagementClusterTwoDCartesianVertexStruct class]]) {
+                    auto element_1 = MTR_SAFE_CAST(self.zone.vertices[i_1], MTRZoneManagementClusterTwoDCartesianVertexStruct);
+                    if (!element_1) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.zone.vertices[i_1], NSStringFromClass(MTRZoneManagementClusterTwoDCartesianVertexStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_1 = (MTRZoneManagementClusterTwoDCartesianVertexStruct *) self.zone.vertices[i_1];
                     listHolder_1->mList[i_1].x = element_1.x.unsignedShortValue;
                     listHolder_1->mList[i_1].y = element_1.y.unsignedShortValue;
                 }
@@ -32929,11 +32959,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.streamPriorities.count; ++i_0) {
-                    if (![self.streamPriorities[i_0] isKindOfClass:[NSNumber class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.streamPriorities[i_0], NSNumber);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.streamPriorities[i_0], NSStringFromClass(NSNumber.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (NSNumber *) self.streamPriorities[i_0];
                     listHolder_0->mList[i_0] = static_cast<std::remove_reference_t<decltype(listHolder_0->mList[i_0])>>(element_0.unsignedCharValue);
                 }
                 encodableStruct.streamPriorities = ListType_0(listHolder_0->mList, self.streamPriorities.count);
@@ -33250,11 +33281,12 @@
                     }
                     listFreer.add(listHolder_1);
                     for (size_t i_1 = 0; i_1 < self.iceServers.count; ++i_1) {
-                        if (![self.iceServers[i_1] isKindOfClass:[MTRWebRTCTransportProviderClusterICEServerStruct class]]) {
+                        auto element_1 = MTR_SAFE_CAST(self.iceServers[i_1], MTRWebRTCTransportProviderClusterICEServerStruct);
+                        if (!element_1) {
                             // Wrong kind of value.
+                            MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.iceServers[i_1], NSStringFromClass(MTRWebRTCTransportProviderClusterICEServerStruct.class));
                             return CHIP_ERROR_INVALID_ARGUMENT;
                         }
-                        auto element_1 = (MTRWebRTCTransportProviderClusterICEServerStruct *) self.iceServers[i_1];
                         {
                             using ListType_3 = std::remove_reference_t<decltype(listHolder_1->mList[i_1].urls)>;
                             using ListMemberType_3 = ListMemberTypeGetter<ListType_3>::Type;
@@ -33265,11 +33297,12 @@
                                 }
                                 listFreer.add(listHolder_3);
                                 for (size_t i_3 = 0; i_3 < element_1.urls.count; ++i_3) {
-                                    if (![element_1.urls[i_3] isKindOfClass:[NSString class]]) {
+                                    auto element_3 = MTR_SAFE_CAST(element_1.urls[i_3], NSString);
+                                    if (!element_3) {
                                         // Wrong kind of value.
+                                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_1.urls[i_3], NSStringFromClass(NSString.class));
                                         return CHIP_ERROR_INVALID_ARGUMENT;
                                     }
-                                    auto element_3 = (NSString *) element_1.urls[i_3];
                                     listHolder_3->mList[i_3] = AsCharSpan(element_3);
                                 }
                                 listHolder_1->mList[i_1].urls = ListType_3(listHolder_3->mList, element_1.urls.count);
@@ -33568,11 +33601,12 @@
                     }
                     listFreer.add(listHolder_1);
                     for (size_t i_1 = 0; i_1 < self.iceServers.count; ++i_1) {
-                        if (![self.iceServers[i_1] isKindOfClass:[MTRWebRTCTransportProviderClusterICEServerStruct class]]) {
+                        auto element_1 = MTR_SAFE_CAST(self.iceServers[i_1], MTRWebRTCTransportProviderClusterICEServerStruct);
+                        if (!element_1) {
                             // Wrong kind of value.
+                            MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.iceServers[i_1], NSStringFromClass(MTRWebRTCTransportProviderClusterICEServerStruct.class));
                             return CHIP_ERROR_INVALID_ARGUMENT;
                         }
-                        auto element_1 = (MTRWebRTCTransportProviderClusterICEServerStruct *) self.iceServers[i_1];
                         {
                             using ListType_3 = std::remove_reference_t<decltype(listHolder_1->mList[i_1].urls)>;
                             using ListMemberType_3 = ListMemberTypeGetter<ListType_3>::Type;
@@ -33583,11 +33617,12 @@
                                 }
                                 listFreer.add(listHolder_3);
                                 for (size_t i_3 = 0; i_3 < element_1.urls.count; ++i_3) {
-                                    if (![element_1.urls[i_3] isKindOfClass:[NSString class]]) {
+                                    auto element_3 = MTR_SAFE_CAST(element_1.urls[i_3], NSString);
+                                    if (!element_3) {
                                         // Wrong kind of value.
+                                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_1.urls[i_3], NSStringFromClass(NSString.class));
                                         return CHIP_ERROR_INVALID_ARGUMENT;
                                     }
-                                    auto element_3 = (NSString *) element_1.urls[i_3];
                                     listHolder_3->mList[i_3] = AsCharSpan(element_3);
                                 }
                                 listHolder_1->mList[i_1].urls = ListType_3(listHolder_3->mList, element_1.urls.count);
@@ -34077,11 +34112,12 @@
                     }
                     listFreer.add(listHolder_1);
                     for (size_t i_1 = 0; i_1 < self.iceServers.count; ++i_1) {
-                        if (![self.iceServers[i_1] isKindOfClass:[MTRWebRTCTransportRequestorClusterICEServerStruct class]]) {
+                        auto element_1 = MTR_SAFE_CAST(self.iceServers[i_1], MTRWebRTCTransportRequestorClusterICEServerStruct);
+                        if (!element_1) {
                             // Wrong kind of value.
+                            MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.iceServers[i_1], NSStringFromClass(MTRWebRTCTransportRequestorClusterICEServerStruct.class));
                             return CHIP_ERROR_INVALID_ARGUMENT;
                         }
-                        auto element_1 = (MTRWebRTCTransportRequestorClusterICEServerStruct *) self.iceServers[i_1];
                         {
                             using ListType_3 = std::remove_reference_t<decltype(listHolder_1->mList[i_1].urls)>;
                             using ListMemberType_3 = ListMemberTypeGetter<ListType_3>::Type;
@@ -34092,11 +34128,12 @@
                                 }
                                 listFreer.add(listHolder_3);
                                 for (size_t i_3 = 0; i_3 < element_1.urls.count; ++i_3) {
-                                    if (![element_1.urls[i_3] isKindOfClass:[NSString class]]) {
+                                    auto element_3 = MTR_SAFE_CAST(element_1.urls[i_3], NSString);
+                                    if (!element_3) {
                                         // Wrong kind of value.
+                                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_1.urls[i_3], NSStringFromClass(NSString.class));
                                         return CHIP_ERROR_INVALID_ARGUMENT;
                                     }
-                                    auto element_3 = (NSString *) element_1.urls[i_3];
                                     listHolder_3->mList[i_3] = AsCharSpan(element_3);
                                 }
                                 listHolder_1->mList[i_1].urls = ListType_3(listHolder_3->mList, element_1.urls.count);
@@ -36079,11 +36116,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.arg1.count; ++i_0) {
-                    if (![self.arg1[i_0] isKindOfClass:[MTRUnitTestingClusterNestedStructList class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.arg1[i_0], MTRUnitTestingClusterNestedStructList);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg1[i_0], NSStringFromClass(MTRUnitTestingClusterNestedStructList.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRUnitTestingClusterNestedStructList *) self.arg1[i_0];
                     listHolder_0->mList[i_0].a = element_0.a.unsignedCharValue;
                     listHolder_0->mList[i_0].b = element_0.b.boolValue;
                     listHolder_0->mList[i_0].c.a = element_0.c.a.unsignedCharValue;
@@ -36108,11 +36146,12 @@
                             }
                             listFreer.add(listHolder_2);
                             for (size_t i_2 = 0; i_2 < element_0.d.count; ++i_2) {
-                                if (![element_0.d[i_2] isKindOfClass:[MTRUnitTestingClusterSimpleStruct class]]) {
+                                auto element_2 = MTR_SAFE_CAST(element_0.d[i_2], MTRUnitTestingClusterSimpleStruct);
+                                if (!element_2) {
                                     // Wrong kind of value.
+                                    MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.d[i_2], NSStringFromClass(MTRUnitTestingClusterSimpleStruct.class));
                                     return CHIP_ERROR_INVALID_ARGUMENT;
                                 }
-                                auto element_2 = (MTRUnitTestingClusterSimpleStruct *) element_0.d[i_2];
                                 listHolder_2->mList[i_2].a = element_2.a.unsignedCharValue;
                                 listHolder_2->mList[i_2].b = element_2.b.boolValue;
                                 listHolder_2->mList[i_2].c = static_cast<std::remove_reference_t<decltype(listHolder_2->mList[i_2].c)>>(element_2.c.unsignedCharValue);
@@ -36141,11 +36180,12 @@
                             }
                             listFreer.add(listHolder_2);
                             for (size_t i_2 = 0; i_2 < element_0.e.count; ++i_2) {
-                                if (![element_0.e[i_2] isKindOfClass:[NSNumber class]]) {
+                                auto element_2 = MTR_SAFE_CAST(element_0.e[i_2], NSNumber);
+                                if (!element_2) {
                                     // Wrong kind of value.
+                                    MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.e[i_2], NSStringFromClass(NSNumber.class));
                                     return CHIP_ERROR_INVALID_ARGUMENT;
                                 }
-                                auto element_2 = (NSNumber *) element_0.e[i_2];
                                 listHolder_2->mList[i_2] = element_2.unsignedIntValue;
                             }
                             listHolder_0->mList[i_0].e = ListType_2(listHolder_2->mList, element_0.e.count);
@@ -36163,11 +36203,12 @@
                             }
                             listFreer.add(listHolder_2);
                             for (size_t i_2 = 0; i_2 < element_0.f.count; ++i_2) {
-                                if (![element_0.f[i_2] isKindOfClass:[NSData class]]) {
+                                auto element_2 = MTR_SAFE_CAST(element_0.f[i_2], NSData);
+                                if (!element_2) {
                                     // Wrong kind of value.
+                                    MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.f[i_2], NSStringFromClass(NSData.class));
                                     return CHIP_ERROR_INVALID_ARGUMENT;
                                 }
-                                auto element_2 = (NSData *) element_0.f[i_2];
                                 listHolder_2->mList[i_2] = AsByteSpan(element_2);
                             }
                             listHolder_0->mList[i_0].f = ListType_2(listHolder_2->mList, element_0.f.count);
@@ -36185,11 +36226,12 @@
                             }
                             listFreer.add(listHolder_2);
                             for (size_t i_2 = 0; i_2 < element_0.g.count; ++i_2) {
-                                if (![element_0.g[i_2] isKindOfClass:[NSNumber class]]) {
+                                auto element_2 = MTR_SAFE_CAST(element_0.g[i_2], NSNumber);
+                                if (!element_2) {
                                     // Wrong kind of value.
+                                    MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.g[i_2], NSStringFromClass(NSNumber.class));
                                     return CHIP_ERROR_INVALID_ARGUMENT;
                                 }
-                                auto element_2 = (NSNumber *) element_0.g[i_2];
                                 listHolder_2->mList[i_2] = element_2.unsignedCharValue;
                             }
                             listHolder_0->mList[i_0].g = ListType_2(listHolder_2->mList, element_0.g.count);
@@ -36215,11 +36257,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.arg2.count; ++i_0) {
-                    if (![self.arg2[i_0] isKindOfClass:[MTRUnitTestingClusterSimpleStruct class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.arg2[i_0], MTRUnitTestingClusterSimpleStruct);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg2[i_0], NSStringFromClass(MTRUnitTestingClusterSimpleStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRUnitTestingClusterSimpleStruct *) self.arg2[i_0];
                     listHolder_0->mList[i_0].a = element_0.a.unsignedCharValue;
                     listHolder_0->mList[i_0].b = element_0.b.boolValue;
                     listHolder_0->mList[i_0].c = static_cast<std::remove_reference_t<decltype(listHolder_0->mList[i_0].c)>>(element_0.c.unsignedCharValue);
@@ -36250,11 +36293,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.arg3.count; ++i_0) {
-                    if (![self.arg3[i_0] isKindOfClass:[NSNumber class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.arg3[i_0], NSNumber);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg3[i_0], NSStringFromClass(NSNumber.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (NSNumber *) self.arg3[i_0];
                     listHolder_0->mList[i_0] = static_cast<std::remove_reference_t<decltype(listHolder_0->mList[i_0])>>(element_0.unsignedCharValue);
                 }
                 encodableStruct.arg3 = ListType_0(listHolder_0->mList, self.arg3.count);
@@ -36274,11 +36318,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.arg4.count; ++i_0) {
-                    if (![self.arg4[i_0] isKindOfClass:[NSNumber class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.arg4[i_0], NSNumber);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg4[i_0], NSStringFromClass(NSNumber.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (NSNumber *) self.arg4[i_0];
                     listHolder_0->mList[i_0] = element_0.boolValue;
                 }
                 encodableStruct.arg4 = ListType_0(listHolder_0->mList, self.arg4.count);
@@ -37257,11 +37302,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.arg1.count; ++i_0) {
-                    if (![self.arg1[i_0] isKindOfClass:[MTRUnitTestingClusterSimpleStruct class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.arg1[i_0], MTRUnitTestingClusterSimpleStruct);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg1[i_0], NSStringFromClass(MTRUnitTestingClusterSimpleStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRUnitTestingClusterSimpleStruct *) self.arg1[i_0];
                     listHolder_0->mList[i_0].a = element_0.a.unsignedCharValue;
                     listHolder_0->mList[i_0].b = element_0.b.boolValue;
                     listHolder_0->mList[i_0].c = static_cast<std::remove_reference_t<decltype(listHolder_0->mList[i_0].c)>>(element_0.c.unsignedCharValue);
@@ -37477,11 +37523,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.arg1.count; ++i_0) {
-                    if (![self.arg1[i_0] isKindOfClass:[NSNumber class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.arg1[i_0], NSNumber);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg1[i_0], NSStringFromClass(NSNumber.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (NSNumber *) self.arg1[i_0];
                     listHolder_0->mList[i_0] = element_0.unsignedCharValue;
                 }
                 encodableStruct.arg1 = ListType_0(listHolder_0->mList, self.arg1.count);
@@ -37683,11 +37730,12 @@
                 }
                 listFreer.add(listHolder_1);
                 for (size_t i_1 = 0; i_1 < self.arg1.d.count; ++i_1) {
-                    if (![self.arg1.d[i_1] isKindOfClass:[MTRUnitTestingClusterSimpleStruct class]]) {
+                    auto element_1 = MTR_SAFE_CAST(self.arg1.d[i_1], MTRUnitTestingClusterSimpleStruct);
+                    if (!element_1) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg1.d[i_1], NSStringFromClass(MTRUnitTestingClusterSimpleStruct.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_1 = (MTRUnitTestingClusterSimpleStruct *) self.arg1.d[i_1];
                     listHolder_1->mList[i_1].a = element_1.a.unsignedCharValue;
                     listHolder_1->mList[i_1].b = element_1.b.boolValue;
                     listHolder_1->mList[i_1].c = static_cast<std::remove_reference_t<decltype(listHolder_1->mList[i_1].c)>>(element_1.c.unsignedCharValue);
@@ -37716,11 +37764,12 @@
                 }
                 listFreer.add(listHolder_1);
                 for (size_t i_1 = 0; i_1 < self.arg1.e.count; ++i_1) {
-                    if (![self.arg1.e[i_1] isKindOfClass:[NSNumber class]]) {
+                    auto element_1 = MTR_SAFE_CAST(self.arg1.e[i_1], NSNumber);
+                    if (!element_1) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg1.e[i_1], NSStringFromClass(NSNumber.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_1 = (NSNumber *) self.arg1.e[i_1];
                     listHolder_1->mList[i_1] = element_1.unsignedIntValue;
                 }
                 encodableStruct.arg1.e = ListType_1(listHolder_1->mList, self.arg1.e.count);
@@ -37738,11 +37787,12 @@
                 }
                 listFreer.add(listHolder_1);
                 for (size_t i_1 = 0; i_1 < self.arg1.f.count; ++i_1) {
-                    if (![self.arg1.f[i_1] isKindOfClass:[NSData class]]) {
+                    auto element_1 = MTR_SAFE_CAST(self.arg1.f[i_1], NSData);
+                    if (!element_1) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg1.f[i_1], NSStringFromClass(NSData.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_1 = (NSData *) self.arg1.f[i_1];
                     listHolder_1->mList[i_1] = AsByteSpan(element_1);
                 }
                 encodableStruct.arg1.f = ListType_1(listHolder_1->mList, self.arg1.f.count);
@@ -37760,11 +37810,12 @@
                 }
                 listFreer.add(listHolder_1);
                 for (size_t i_1 = 0; i_1 < self.arg1.g.count; ++i_1) {
-                    if (![self.arg1.g[i_1] isKindOfClass:[NSNumber class]]) {
+                    auto element_1 = MTR_SAFE_CAST(self.arg1.g[i_1], NSNumber);
+                    if (!element_1) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg1.g[i_1], NSStringFromClass(NSNumber.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_1 = (NSNumber *) self.arg1.g[i_1];
                     listHolder_1->mList[i_1] = element_1.unsignedCharValue;
                 }
                 encodableStruct.arg1.g = ListType_1(listHolder_1->mList, self.arg1.g.count);
@@ -37952,11 +38003,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.arg1.count; ++i_0) {
-                    if (![self.arg1[i_0] isKindOfClass:[MTRUnitTestingClusterNestedStructList class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.arg1[i_0], MTRUnitTestingClusterNestedStructList);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg1[i_0], NSStringFromClass(MTRUnitTestingClusterNestedStructList.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (MTRUnitTestingClusterNestedStructList *) self.arg1[i_0];
                     listHolder_0->mList[i_0].a = element_0.a.unsignedCharValue;
                     listHolder_0->mList[i_0].b = element_0.b.boolValue;
                     listHolder_0->mList[i_0].c.a = element_0.c.a.unsignedCharValue;
@@ -37981,11 +38033,12 @@
                             }
                             listFreer.add(listHolder_2);
                             for (size_t i_2 = 0; i_2 < element_0.d.count; ++i_2) {
-                                if (![element_0.d[i_2] isKindOfClass:[MTRUnitTestingClusterSimpleStruct class]]) {
+                                auto element_2 = MTR_SAFE_CAST(element_0.d[i_2], MTRUnitTestingClusterSimpleStruct);
+                                if (!element_2) {
                                     // Wrong kind of value.
+                                    MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.d[i_2], NSStringFromClass(MTRUnitTestingClusterSimpleStruct.class));
                                     return CHIP_ERROR_INVALID_ARGUMENT;
                                 }
-                                auto element_2 = (MTRUnitTestingClusterSimpleStruct *) element_0.d[i_2];
                                 listHolder_2->mList[i_2].a = element_2.a.unsignedCharValue;
                                 listHolder_2->mList[i_2].b = element_2.b.boolValue;
                                 listHolder_2->mList[i_2].c = static_cast<std::remove_reference_t<decltype(listHolder_2->mList[i_2].c)>>(element_2.c.unsignedCharValue);
@@ -38014,11 +38067,12 @@
                             }
                             listFreer.add(listHolder_2);
                             for (size_t i_2 = 0; i_2 < element_0.e.count; ++i_2) {
-                                if (![element_0.e[i_2] isKindOfClass:[NSNumber class]]) {
+                                auto element_2 = MTR_SAFE_CAST(element_0.e[i_2], NSNumber);
+                                if (!element_2) {
                                     // Wrong kind of value.
+                                    MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.e[i_2], NSStringFromClass(NSNumber.class));
                                     return CHIP_ERROR_INVALID_ARGUMENT;
                                 }
-                                auto element_2 = (NSNumber *) element_0.e[i_2];
                                 listHolder_2->mList[i_2] = element_2.unsignedIntValue;
                             }
                             listHolder_0->mList[i_0].e = ListType_2(listHolder_2->mList, element_0.e.count);
@@ -38036,11 +38090,12 @@
                             }
                             listFreer.add(listHolder_2);
                             for (size_t i_2 = 0; i_2 < element_0.f.count; ++i_2) {
-                                if (![element_0.f[i_2] isKindOfClass:[NSData class]]) {
+                                auto element_2 = MTR_SAFE_CAST(element_0.f[i_2], NSData);
+                                if (!element_2) {
                                     // Wrong kind of value.
+                                    MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.f[i_2], NSStringFromClass(NSData.class));
                                     return CHIP_ERROR_INVALID_ARGUMENT;
                                 }
-                                auto element_2 = (NSData *) element_0.f[i_2];
                                 listHolder_2->mList[i_2] = AsByteSpan(element_2);
                             }
                             listHolder_0->mList[i_0].f = ListType_2(listHolder_2->mList, element_0.f.count);
@@ -38058,11 +38113,12 @@
                             }
                             listFreer.add(listHolder_2);
                             for (size_t i_2 = 0; i_2 < element_0.g.count; ++i_2) {
-                                if (![element_0.g[i_2] isKindOfClass:[NSNumber class]]) {
+                                auto element_2 = MTR_SAFE_CAST(element_0.g[i_2], NSNumber);
+                                if (!element_2) {
                                     // Wrong kind of value.
+                                    MTR_LOG_ERROR("%@ incorrectly present in list of %@", element_0.g[i_2], NSStringFromClass(NSNumber.class));
                                     return CHIP_ERROR_INVALID_ARGUMENT;
                                 }
-                                auto element_2 = (NSNumber *) element_0.g[i_2];
                                 listHolder_2->mList[i_2] = element_2.unsignedCharValue;
                             }
                             listHolder_0->mList[i_0].g = ListType_2(listHolder_2->mList, element_0.g.count);
@@ -38249,11 +38305,12 @@
                 }
                 listFreer.add(listHolder_0);
                 for (size_t i_0 = 0; i_0 < self.arg1.count; ++i_0) {
-                    if (![self.arg1[i_0] isKindOfClass:[NSNumber class]]) {
+                    auto element_0 = MTR_SAFE_CAST(self.arg1[i_0], NSNumber);
+                    if (!element_0) {
                         // Wrong kind of value.
+                        MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.arg1[i_0], NSStringFromClass(NSNumber.class));
                         return CHIP_ERROR_INVALID_ARGUMENT;
                     }
-                    auto element_0 = (NSNumber *) self.arg1[i_0];
                     listHolder_0->mList[i_0] = element_0.unsignedCharValue;
                 }
                 encodableStruct.arg1 = ListType_0(listHolder_0->mList, self.arg1.count);
@@ -38868,11 +38925,12 @@
                     }
                     listFreer.add(listHolder_1);
                     for (size_t i_1 = 0; i_1 < self.nullableList.count; ++i_1) {
-                        if (![self.nullableList[i_1] isKindOfClass:[NSNumber class]]) {
+                        auto element_1 = MTR_SAFE_CAST(self.nullableList[i_1], NSNumber);
+                        if (!element_1) {
                             // Wrong kind of value.
+                            MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.nullableList[i_1], NSStringFromClass(NSNumber.class));
                             return CHIP_ERROR_INVALID_ARGUMENT;
                         }
-                        auto element_1 = (NSNumber *) self.nullableList[i_1];
                         listHolder_1->mList[i_1] = static_cast<std::remove_reference_t<decltype(listHolder_1->mList[i_1])>>(element_1.unsignedCharValue);
                     }
                     nonNullValue_0 = ListType_1(listHolder_1->mList, self.nullableList.count);
@@ -38895,11 +38953,12 @@
                     }
                     listFreer.add(listHolder_1);
                     for (size_t i_1 = 0; i_1 < self.optionalList.count; ++i_1) {
-                        if (![self.optionalList[i_1] isKindOfClass:[NSNumber class]]) {
+                        auto element_1 = MTR_SAFE_CAST(self.optionalList[i_1], NSNumber);
+                        if (!element_1) {
                             // Wrong kind of value.
+                            MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.optionalList[i_1], NSStringFromClass(NSNumber.class));
                             return CHIP_ERROR_INVALID_ARGUMENT;
                         }
-                        auto element_1 = (NSNumber *) self.optionalList[i_1];
                         listHolder_1->mList[i_1] = static_cast<std::remove_reference_t<decltype(listHolder_1->mList[i_1])>>(element_1.unsignedCharValue);
                     }
                     definedValue_0 = ListType_1(listHolder_1->mList, self.optionalList.count);
@@ -38926,11 +38985,12 @@
                         }
                         listFreer.add(listHolder_2);
                         for (size_t i_2 = 0; i_2 < self.nullableOptionalList.count; ++i_2) {
-                            if (![self.nullableOptionalList[i_2] isKindOfClass:[NSNumber class]]) {
+                            auto element_2 = MTR_SAFE_CAST(self.nullableOptionalList[i_2], NSNumber);
+                            if (!element_2) {
                                 // Wrong kind of value.
+                                MTR_LOG_ERROR("%@ incorrectly present in list of %@", self.nullableOptionalList[i_2], NSStringFromClass(NSNumber.class));
                                 return CHIP_ERROR_INVALID_ARGUMENT;
                             }
-                            auto element_2 = (NSNumber *) self.nullableOptionalList[i_2];
                             listHolder_2->mList[i_2] = static_cast<std::remove_reference_t<decltype(listHolder_2->mList[i_2])>>(element_2.unsignedCharValue);
                         }
                         nonNullValue_1 = ListType_2(listHolder_2->mList, self.nullableOptionalList.count);