toDartObject function
Converts a Objective C object to the corresponding Dart object.
This supports basic types like NSNumber and NSString. It also works on
collections, and recursively converts their elements.
If objCObject is not one of the recognized types, convertOther is
called. If convertOther is not provided, objCObject is returned
directly.
Implementation
Object toDartObject(
ObjCObject objCObject, {
Object Function(ObjCObject) convertOther = _defaultDartConverter,
}) {
// A type-based switch, like in toObjCObject, won't work here because the
// object could have a Dart runtime type of eg NSObject, even if the
// underlying ObjC object that the Dart object is wrapping is a NSNumber.
if (NSNumber.isA(objCObject)) {
return NSNumber.as(objCObject).numValue;
}
if (NSString.isA(objCObject)) {
return NSString.as(objCObject).toDartString();
}
if (NSDate.isA(objCObject)) {
return NSDate.as(objCObject).toDateTime();
}
if (NSArray.isA(objCObject)) {
return NSArray.as(objCObject).toDartList(convertOther: convertOther);
}
if (NSSet.isA(objCObject)) {
return NSSet.as(objCObject).toDartSet(convertOther: convertOther);
}
if (NSDictionary.isA(objCObject)) {
return NSDictionary.as(objCObject).toDartMap(convertOther: convertOther);
}
return convertOther(objCObject);
}