mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 18:12:09 +08:00
feat(Utils): add dataSerialize and dataDeserialize for iOS
This commit is contained in:
6
packages/core/utils/native-helper.d.ts
vendored
6
packages/core/utils/native-helper.d.ts
vendored
@ -121,6 +121,12 @@ export namespace iOSNativeHelper {
|
||||
export function nsArrayToJSArray<T>(a: NSArray<T>): T[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Data serialize and deserialize helpers
|
||||
*/
|
||||
export function dataSerialize(data?: any): any;
|
||||
export function dataDeserialize(nativeData?: any): any;
|
||||
|
||||
/**
|
||||
* @deprecated use application.orientation instead
|
||||
*
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { Trace } from '../trace';
|
||||
import { getClass, isNullOrUndefined } from './types';
|
||||
|
||||
declare let UIImagePickerControllerSourceType: any;
|
||||
|
||||
@ -53,6 +54,60 @@ export namespace iOSNativeHelper {
|
||||
}
|
||||
}
|
||||
|
||||
export function dataDeserialize(nativeData?: any) {
|
||||
if (isNullOrUndefined(nativeData)) {
|
||||
// some native values will already be js null values
|
||||
// calling types.getClass below on null/undefined will cause crash
|
||||
return null;
|
||||
} else {
|
||||
switch (getClass(nativeData)) {
|
||||
case 'NSNull':
|
||||
return null;
|
||||
case 'NSMutableDictionary':
|
||||
case 'NSDictionary':
|
||||
let obj = {};
|
||||
const length = nativeData.count;
|
||||
const keysArray = nativeData.allKeys as NSArray<any>;
|
||||
for (let i = 0; i < length; i++) {
|
||||
const nativeKey = keysArray.objectAtIndex(i);
|
||||
obj[nativeKey] = dataDeserialize(nativeData.objectForKey(nativeKey));
|
||||
}
|
||||
return obj;
|
||||
case 'NSMutableArray':
|
||||
case 'NSArray':
|
||||
let array = [];
|
||||
const len = nativeData.count;
|
||||
for (let i = 0; i < len; i++) {
|
||||
array[i] = dataDeserialize(nativeData.objectAtIndex(i));
|
||||
}
|
||||
return array;
|
||||
default:
|
||||
return nativeData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function dataSerialize(data?: any) {
|
||||
switch (typeof data) {
|
||||
case 'number':
|
||||
case 'string':
|
||||
case 'boolean':
|
||||
return data;
|
||||
case 'object':
|
||||
if (Array.isArray(data)) {
|
||||
return NSArray.arrayWithArray(<any>data.map(dataSerialize));
|
||||
}
|
||||
|
||||
let obj = {};
|
||||
for (let key of Object.keys(data)) {
|
||||
obj[key] = dataSerialize(data[key]);
|
||||
}
|
||||
return NSDictionary.dictionaryWithDictionary(<any>obj);
|
||||
default:
|
||||
return NSNull.new();
|
||||
}
|
||||
}
|
||||
|
||||
export function isLandscape(): boolean {
|
||||
console.log('utils.ios.isLandscape() is deprecated; use application.orientation instead');
|
||||
|
||||
|
Reference in New Issue
Block a user