HarmonyOS 为什么List[index]能获取到数据,List.get(index)要报错?

1、有一个List里面有一百多条数据,为什么List[index]能获取到数据,List.get(index)要报错?get()方法api里面写的是Returns the element at the specified position in this list,那应该能返回才对。

2、这个地方报错为什么Log里面不打印错误日志,加了try…catch后才看到报错信息?

HarmonyOS 为什么List[index]能获取到数据,List.get(index)要报错?-鸿蒙开发者社区解决方案

JSON.parse(descedContent)返回的是BMathModel并不是所谓的线性List。强转了后实际还是BMathModel,所以调用失败了也没报错,以下是正常的代码:

let aryTes: [BMathModel] = JSON.parse(descedContent)
let allQaList: List<BMathModel> = new List();
for (let index = 0; index < aryTes.length; index++) {
  let a: BMathModel = aryTes[index];
  allQaList.add(a)
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

JSON.parse(descedContent)转为的数据不会携带方法只有数据,所以调用方法失败了,以下是正确的调用方法:

let model = new BMathModel();
model.content = "1"
BL.e('*********getQaByLevel1**********' + model.getContent())
  • 1.
  • 2.
  • 3.

JSON.parse出来是一个object 对像,不是class对像。HarmonyOS因为开启了严格的ARKTS语言检查,所以使用的data对象必须进行class类型的自定义,否则无法使用。

HarmonyOS 泛型回调函数的一般写法 <a name="section416358162012"></a>

问题描述

因为业务场景需要设计一个T泛型的回调函数,但是按以下格式写的时候会红线提示错误,现版本应该怎么设计泛型函数?

export interface func<T> {
  (T, extra): void
}
  • 1.
  • 2.
  • 3.

按以上写法提示错误:

Use "class" instead of a type with call signature (arkts-no-call-signatures) <ArkTSCheck>;
Use explicit types instead of "any", "unknown" (arkts-no-any-unknown) <ArkTSCheck>;
  • 1.
  • 2.
HarmonyOS
2024-12-23 16:10:10
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

参考示例如下:

type Handler<T, U> = (data: T) => U

interface Processor<T, U> {
  process: Handler<T, U>;
}

function createProcessor<T, U>(handler: Handler<T, U>): Processor<T, U> {
  return {
    process: handler,
  };
}

// 使用示例
const processor = createProcessor<number, string>((data) => `${data} is processed`);
console.log(processor.process(789)); // 输出: "789 is processed"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

官网文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-more-cases-V5#arkts-no-call-signature

分享
微博
QQ
微信
回复
2024-12-23 18:13:44
相关问题
HarmonyOS Tab指定默认Index
540浏览 • 1回复 待解决
HarmonyOS 关于struct index咨询
418浏览 • 1回复 待解决
HarmonyOS arkts-no-props-by-index错误疑问
479浏览 • 1回复 待解决
Redis数据类型列表list什么
4001浏览 • 1回复 待解决
HarmonyOS List怎么刷新数据
750浏览 • 1回复 待解决
HarmonyOS List数据全显示
623浏览 • 1回复 待解决
HarmonyOS list数据加载问题
512浏览 • 1回复 待解决
HarmonyOS List滑动速度是否控制?
1107浏览 • 1回复 待解决
HarmonyOS 如何禁止Swipe向某个index滑动
436浏览 • 1回复 待解决