MMKV icon indicating copy to clipboard operation
MMKV copied to clipboard

鸿蒙版本的MMKV,double、float、int、bool现在不能返回undefined

Open HideNameUser opened this issue 1 year ago • 4 comments

let defaultValue:number | undefined = undefined MMKV.decodeDouble("KEY_NOT_FUND", defaultValue) 返回的结果为0,不是想要的默认值undefined 标题中提到的int、float也是同样的 boolean类型返回默认值为false,也无法返回undefined。 返回undefined能知道是没有存过这个值。

HideNameUser avatar May 24 '24 09:05 HideNameUser

Noted.

lingol avatar May 24 '24 11:05 lingol

I have taken a deeper look into this issue. It is more complicated than I thought.

The problem is that the Android interface, which many of you OHOS developers are familiar with, returns 0/false when a primitive type of value does not exist. If we declared the interface as

public decodeBool(key: string, defaultValue?: boolean): boolean | undefined;

By default, it will return undefined on non-exist values, even if you have not passed undefined as the defaultValue. This behavior doesn't agree with the Andoird/Java interface many of you have been familiar with.

Now, how about we declare it as:

public decodeBool(key: string, defaultValue?: boolean = false): boolean | undefined;

The problem is that it won't compile.

lingol avatar May 24 '24 12:05 lingol

Maybe we have to drop the undefined default value.

lingol avatar May 25 '24 02:05 lingol

I've decided that undefined will be returned on non-existent values unless a specific defaultValue is passed in. It's fixed in 1.3.6-beta2 on OHPM.

lingol avatar May 27 '24 08:05 lingol

It's fixed in v1.3.6.

lingol avatar Jul 05 '24 12:07 lingol