• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Programming Languages
2
3## What are the restrictions on using generator functions in TypeScript?
4
5Applicable to: OpenHarmony SDK 3.2.5.3, stage model of API version 9
6
7Below are the restrictions on using generator functions in TypeScript:
8
9- Expressions can be used only in character strings (in the ${expression} format), **if** conditions, **ForEach** parameters, and component parameters.
10
11- No expressions should cause any application state variables (including **@State**, **@Link**, and **@Prop**) to change. Otherwise, undefined and potentially unstable framework behavior may occur.
12
13- The generator function cannot contain local variables.
14
15None of the above restrictions apply to anonymous function implementations of event handlers (such as **onClick**).
16
17Negative example:
18
19```
20build() {
21  let a: number = 1 // invalid: variable declaration not allowed
22  Column() {
23    Text('Hello ${this.myName.toUpperCase()}') // ok.
24    ForEach(this.arr.reverse(), ..., ...) // invalid: Array.reverse modifies the @State array variable in place
25  }
26  buildSpecial()  // invalid: no function calls
27  Text(this.calcTextValue()) // this function call is ok.
28}
29```
30
31## How do I dynamically replace the %s placeholder in a resource file?
32
33Applicable to: OpenHarmony SDK 3.2.5.3, stage model of API version 9
34
35In an application, you can replace the %s placeholder by using the second parameter in **$r('app.string.xx')**, which is used to reference application resources.
36
37Example:
38
39```
40build() {
41  //do something
42  // The second parameter indicates the referenced string resource, which can be used to replace the %s placeholder.
43  Text($r('app.string.entry_desc','aaa'))
44    .fontSize(100)
45    .fontColor(Color.Black)
46  //do something
47}
48```
49
50## How do I read an XML file in Resource and convert data in it to the string type?
51
52Applicable to: OpenHarmony SDK 3.2.2.5, stage model of API version 9
53
541. Obtain Uint8Array data by calling the **RawFile** API of **resourceManager**.
55
562. Convert the Uint8Array data to strings by calling the **String.fromCharCode** API.
57
58Reference: [Resource Manager](../reference/apis/js-apis-resource-manager.md)
59
60Example:
61
62
63```
64resourceManager.getRawFile(path, (error, value) => {
65  if (error != null) {
66    console.log("error is " + error);
67  } else {
68    let rawFile = value;
69    let xml = String.fromCharCode.apply(null, rawFile)
70  }
71});
72```
73
74## How do I convert a Resource object to the string type?
75
76Applicable to: OpenHarmony SDK 3.2.3.5, stage model of API version 9
77
78Use the **resourceManager.getString()** API of the **@ohos.resourceManager** module.
79
80Reference: [Resource Manager](../reference/apis/js-apis-resource-manager.md#getstring)
81
82## What should I do if the global static variables of a class do not work?
83
84Applicable to: OpenHarmony SDK 3.2.3.5, stage model of API version 9
85
86Objects imported to abilities and pages are packaged into two different closures, that is, two global objects. In this case, a static variable referenced by the abilities is not the same object as that referenced by the pages. Therefore, global variables cannot be defined by defining static variables in the class. You are advised to use AppStorage to manage global variables.
87
88Reference: [State Management with Application-level Variables](../quick-start/arkts-state-mgmt-application-level.md)
89
90## How do I obtain resources in the stage model?
91
92Applicable to: OpenHarmony SDK 3.2.3.5, stage model of API version 9
93
94The stage model allows an application to obtain a **ResourceManager** object based on **context** and call its resource management APIs without first importing the required bundle. This method, however, is not applicable to the FA model.
95
96Example:
97
98
99```
100const context = getContext(this) as any
101context
102  .resourceManager
103  .getString($r('app.string.entry_desc').id)
104  .then(value => {
105    this.message = value.toString()
106})
107```
108
109## How do I obtain data through an API before page loading?
110
111Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
112
113In the **aboutToAppear** function, use an asynchronous API to obtain page data and **@State** to decorate related variables. After the data is obtained, the page is automatically refreshed based on the variables.
114
115
116```
117@Entry
118@Component
119struct Test6Page {
120  // After the data is obtained, the page is automatically refreshed.
121  @State message: string = 'loading.....'
122  aboutToAppear(){
123    // Simulate an asynchronous API to obtain data.
124    setTimeout(()=>{
125      this.message = 'new msg'
126    },3000)
127  }
128  build() {
129    Row() {
130      Column() {
131        Text(this.message)
132          .fontSize(50)
133          .fontWeight(FontWeight.Bold)
134      }
135      .width('100%')
136    }
137    .height('100%')
138  }
139}
140```
141
142## Do the worker thread and the main thread run in the same global context?
143
144Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
145
146No. The worker thread and the main thread are not in the same global context. They interact with each other in data communication mode.
147
148## Which API is used for URL encoding in OpenHarmony?
149
150Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
151
152The global function **encodeURI** is used for URI encoding, and **decodeURI** is used for URI decoding. For example, a space character is encoded as %20.
153
154## Does OpenHarmony provide any API for parsing XML files?
155
156Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
157
158Yes. The **convert** API of the **convertxml** module can be used to convert XML text into JavaScript objects.
159
160Reference: [@ohos.convertxml](../reference/apis/js-apis-convertxml.md)
161
162## How do I configure application icons to be used across devices?
163
164Applicable to: OpenHarmony SDK 3.0, stage model of API version 9
165
166Use resource qualifiers to configure application icons to be used across devices.
167
168## Can placeholders be configured in the string.json file of the stage model?
169
170Applicable to: OpenHarmony SDK 3.2.6.3, stage model of API version 9
171
172The **string.json** file does not support placeholders. As an alternative, you can define variables on the target page and combine these variables and **Resource** objects.
173
174## Is there any difference between the OpenHarmony API systemTime.getCurrentTime() and the JS API new Date().getTime() API?
175
176Applicable to: OpenHarmony SDK 3.2.6.3, stage model of API version 9
177
178Similar to **new Date().getTime()**, **systemTime.getCurrentTime(false)** returns the number of milliseconds that have elapsed since the Unix epoch. **systemTime.getCurrentTime(true)** returns the number of nanoseconds that have elapsed since the Unix epoch. The system time is used in both APIs.
179
180## How do I define @BuilderParam decorated attributes based on the value assigned to them?
181
182Applicable to: OpenHarmony SDK 3.2.6.5, stage model of API version 9
183
184If no parameter is passed when assigning a value to the **@BuilderParam** decorated attribute (for example, **content: this.specificParam**), define the type of the attribute as a function without a return value (for example, **@BuilderParam content: () => voi**). If any parameter is passed when assigning a value to the **@BuilderParam** decorated attribute (for example, **callContent: this.specificParam1("111")**), define the type of the attribute as **any** (for example, **@BuilderParam callContent: any**). For details, see [BuilderParam](../quick-start/arkts-dynamic-ui-elememt-building.md#builderparam8).
185
186## How does ArkTS convert a string into a byte array?
187
188Applicable to: all versions
189
190Refer to the following code:
191
192
193```
194function stringToByte(str) {
195  var bytes = new Array();
196  var len,c;
197  len = str.length;
198  for(var i = 0;i<len;i++) {
199    c = str.charCodeAt(i);
200    if( c >= 0x010000 && c<= 0x10FFFF) {
201      bytes.push(((c>>18) & 0x07) | 0xf0);
202      bytes.push(((c>>12) & 0x3F) | 0x80);
203      bytes.push(((c>>6) & 0x3f) | 0x80);
204      bytes.push((c & 0x3F) | 0x80);
205    } else if(c >= 0x000800 && c<= 0x00FFF){
206      bytes.push(((c>>12) & 0x07) | 0xf0);
207      bytes.push(((c>>6) & 0x3F) | 0x80);
208      bytes.push((c & 0x3F) | 0x80);
209    } else if(c >= 0x000800 && c<= 0x0007FF) {
210      bytes.push(((c>>6) & 0x3F) | 0x80);
211      bytes.push((c & 0x3F) | 0x80);
212    } else {
213      bytes.push(c & 0xFF)
214    }
215  }
216  return bytes;
217}
218```
219
220## What do I do if the "Too many wokers, the number of worker exceeds the maximum" message is displayed during worker creation?
221
222Applicable to: OpenHarmony SDK 3.2.6.5
223
224An application allows for a maximum of seven workers. Therefore, use the **termiate** API to release workers when they are not needed.
225
226Reference: [@ohos.worker](../reference/apis/js-apis-worker.md#terminate)
227
228## What is the recommended multithreading solution on OpenHarmony?
229
230Applicable to: OpenHarmony SDK 3.2.6.5, stage model of API version 9
231
232OpenHarmony recommends that worker threads be used for multithreading.
233
234Reference: [@ohos.worker](../reference/apis/js-apis-worker.md)
235
236## What is the difference between a @Builder decorated method and other methods?
237
238Applicable to: OpenHarmony SDK 3.2.6.5, stage model of API version 9
239
240If the **@Builder** decorated method uses a custom component, this component is re-created each time the method is called.
241
242## Why is the callback registered through @Watch not triggered when the object attributes of an array change?
243
244Applicable to: OpenHarmony SDK 3.2.6.5, stage model of API version 9
245
246As with **@State**, the callback registered through **@Watch** can be used to listen for only one layer of data changes. If the object attributes of the array changes at the inner layer, the callback will not be triggered.
247
248## How do I listen for in-depth changes of @State decorated variables?
249
250Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
251
252To listen for in-depth changes of **@State** decorated variables, you can use **@Observed** and **@ObjectLink** decorators.
253
254Reference: [@Observed and @ObjectLink](../quick-start/arkts-state-mgmt-page-level.md#observed-and-objectlink)
255
256## How do I implement character string encoding and decoding?
257
258Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
259
260You can use **TextEncoder** and **TextDecoder** provided by the **util** module.
261
262Reference: [TextEncoder](../reference/apis/js-apis-util.md#textencoder) and [TextDecoder](../reference/apis/js-apis-util.md#textdecoder)
263
264## How do i import and export namespaces?
265
266Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
267
268- Exporting namespaces
269
270  ```
271  namespace Util{
272      export function getTime(){
273          return Date.now()
274      }
275  }
276  export default Util
277  ```
278
279- Importing namespaces
280
281  ```
282  import Util from './util'
283  Util.getTime()
284  ```
285
286## Can relational database operations be performed in the worker thread?
287
288Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
289
290No. Relational database operations cannot be performed in the worker thread.
291
292