You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从C原生代码访问与操作Kotlin的Map、List及Array?

Hey there! I’ve worked with Kotlin/Native’s C interop quite a bit, so let’s walk through how to handle Kotlin Map, List, and Array from your C code. Here’s a clear, step-by-step breakdown tailored to your scenario:

Working with Kotlin Map from C

Since you already have your enum keys accessible in C, here’s how to use them to index the Kotlin Map:

  1. Find the generated Map types and helpers in your header
    Your auto-generated header will include a struct for your specific Map type (e.g., typedef struct KotlinMap_YourEnum_YourValueType KotlinMap_YourEnum_YourValueType; — the exact name depends on your Kotlin type declarations). Look for helper functions prefixed with kotlin_map_; you’ll need at least:

    • kotlin_object* kotlin_map_get(KotlinMap_YourEnum_YourValueType* map, YourEnum key);
    • If you need to iterate through the Map, you’ll also see kotlin_map_iterator*, kotlin_map_iterator_has_next, kotlin_map_iterator_next_key, and kotlin_map_iterator_next_value.
  2. Access your Map instance
    Assume you’ve already obtained a pointer to your Map’s kref (either from a Kotlin function return value, a global exported variable, etc.):

    KotlinMap_YourEnum_YourValueType* my_kotlin_map = ...;
    
  3. Retrieve values using enum keys
    Call the kotlin_map_get function with your Map pointer and target enum key. Cast the result to your value type’s struct pointer (since the return is a generic kotlin_object*):

    YourValueType* target_value = (YourValueType*)kotlin_map_get(my_kotlin_map, YOUR_TARGET_ENUM_KEY);
    
    • Always check for NULL here — if the key doesn’t exist in the Map, the function will return a null pointer.
    • For iteration, initialize an iterator and loop through entries like this:
      kotlin_map_iterator* iter = kotlin_map_iterator(my_kotlin_map);
      while (kotlin_map_iterator_has_next(iter)) {
          YourEnum key = (YourEnum)kotlin_map_iterator_next_key(iter);
          YourValueType* value = (YourValueType*)kotlin_map_iterator_next_value(iter);
          // Do something with key/value
      }
      kotlin_release(iter); // Don't forget to clean up the iterator!
      
Working with Kotlin List & Array from C

The pattern for List and Array is similar, with slight differences based on whether the collection is fixed-size (Array) or mutable (List/MutableList):

For Kotlin Arrays

  • Your header will have a struct like typedef struct KotlinArray_YourElementType KotlinArray_YourElementType;
  • Key helper functions:
    • int kotlin_array_size(KotlinArray_YourElementType* array); — Get the array length
    • YourElementType* kotlin_array_get(KotlinArray_YourElementType* array, int index); — Access elements by 0-based index
  • Example usage:
    KotlinArray_YourElementType* my_array = ...;
    int array_len = kotlin_array_size(my_array);
    for (int i = 0; i < array_len; i++) {
        YourElementType* element = kotlin_array_get(my_array, i);
        // Work with the element
    }
    

For Kotlin Lists

  • Mutable or immutable Lists will have a struct like typedef struct KotlinList_YourElementType KotlinList_YourElementType;
  • Core helper functions match Array, plus extra functions for mutable Lists (if exposed in Kotlin):
    • int kotlin_list_size(KotlinList_YourElementType* list);
    • YourElementType* kotlin_list_get(KotlinList_YourElementType* list, int index);
    • Mutable List extras: void kotlin_list_add(KotlinList_YourElementType* list, YourElementType* element);, void kotlin_list_remove_at(KotlinList_YourElementType* list, int index);
  • Same index-based access rules apply — always check the size before accessing to avoid out-of-bounds errors.
Critical Best Practices
  • Handle NULL pointers rigorously: Kotlin nullable types translate to nullable pointers in C. Never dereference a pointer without checking if it’s NULL first.
  • Manage memory properly: Most returned objects from collection getters are "borrowed" (you don’t own them), but if you retain an object (using kotlin_retain), you must release it with kotlin_release to avoid leaks. The generated header will note ownership rules for each function.
  • Lean on your generated header: All the functions you need are auto-generated based on your Kotlin code. If you can’t find a helper, double-check the header — it’s the single source of truth for what’s available.
  • Keep enums in sync: The integer values of your C enum exactly match the order of entries in your Kotlin enum. Don’t reorder or modify the C enum without updating the Kotlin side, or you’ll get mismatched keys.

内容的提问来源于stack exchange,提问作者philtherobot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 16:52:37