(pyobj)
Convert a Python object into an equivalent Foundation object. The returned object is autoreleased.

    This function is also available under the name :func:`at`, because its functionality is very similar to that of the
    Objective-C ``@`` operator and literals.

    Currently supported types:

    * ``None``, :class:`ObjCInstance`: Returned as-is
    * :class:`enum.Enum`: Replaced by their :attr:`~enum.Enum.value` and converted as below
    * :class:`str`: Converted to :class:`NSString`
    * :class:`bytes`: Converted to :class:`NSData`
    * :class:`decimal.Decimal`: Converted to :class:`NSDecimalNumber`
    * :class:`dict`: Converted to :class:`NSDictionary`, with all keys and values converted recursively
    * :class:`list`: Converted to :class:`NSArray`, with all elements converted recursively
    * :class:`bool`, :class:`int`, :class:`float`: Converted to :class:`NSNumber`

    Other types cause a :class:`TypeError`.
Convert a Python object into an equivalent Foundation object. The returned object is autoreleased.
Convert a Python object into an equivalent Foundation object . The returned object is autoreleased .
def ns_from_py(pyobj):
    """Convert a Python object into an equivalent Foundation object. The returned object is autoreleased.

    This function is also available under the name :func:`at`, because its functionality is very similar to that of the
    Objective-C ``@`` operator and literals.

    Currently supported types:

    * ``None``, :class:`ObjCInstance`: Returned as-is
    * :class:`enum.Enum`: Replaced by their :attr:`~enum.Enum.value` and converted as below
    * :class:`str`: Converted to :class:`NSString`
    * :class:`bytes`: Converted to :class:`NSData`
    * :class:`decimal.Decimal`: Converted to :class:`NSDecimalNumber`
    * :class:`dict`: Converted to :class:`NSDictionary`, with all keys and values converted recursively
    * :class:`list`: Converted to :class:`NSArray`, with all elements converted recursively
    * :class:`bool`, :class:`int`, :class:`float`: Converted to :class:`NSNumber`

    Other types cause a :class:`TypeError`.
    """

    if isinstance(pyobj, enum.Enum):
        pyobj = pyobj.value

    # Many Objective-C method calls here use the convert_result=False kwarg to disable automatic conversion of
    # return values, because otherwise most of the Objective-C objects would be converted back to Python objects.
    if pyobj is None or isinstance(pyobj, ObjCInstance):
        return pyobj
    elif isinstance(pyobj, str):
        return ObjCInstance(NSString.stringWithUTF8String_(pyobj.encode('utf-8'), convert_result=False))
    elif isinstance(pyobj, bytes):
        return ObjCInstance(NSData.dataWithBytes(pyobj, length=len(pyobj)))
    elif isinstance(pyobj, decimal.Decimal):
        return ObjCInstance(NSDecimalNumber.decimalNumberWithString_(pyobj.to_eng_string(), convert_result=False))
    elif isinstance(pyobj, dict):
        dikt = NSMutableDictionary.dictionaryWithCapacity(len(pyobj))
        for k, v in pyobj.items():
            dikt.setObject(v, forKey=k)
        return dikt
    elif isinstance(pyobj, list):
        array = NSMutableArray.arrayWithCapacity(len(pyobj))
        for v in pyobj:
            array.addObject(v)
        return array
    elif isinstance(pyobj, bool):
        return ObjCInstance(NSNumber.numberWithBool_(pyobj, convert_result=False))
    elif isinstance(pyobj, int):
        return ObjCInstance(NSNumber.numberWithLong_(pyobj, convert_result=False))
    elif isinstance(pyobj, float):
        return ObjCInstance(NSNumber.numberWithDouble_(pyobj, convert_result=False))
    else:
        raise TypeError(
            "Don't know how to convert a {cls.__module__}.{cls.__qualname__} to a Foundation object"
            .format(cls=type(pyobj))
        )
def ns_from_py ( pyobj ) : if isinstance ( pyobj , enum . Enum ) : pyobj = pyobj . value # Many Objective-C method calls here use the convert_result=False kwarg to disable automatic conversion of # return values, because otherwise most of the Objective-C objects would be converted back to Python objects. if pyobj is None or isinstance ( pyobj , ObjCInstance ) : return pyobj elif isinstance ( pyobj , str ) : return ObjCInstance ( NSString . stringWithUTF8String_ ( pyobj . encode ( 'utf-8' ) , convert_result = False ) ) elif isinstance ( pyobj , bytes ) : return ObjCInstance ( NSData . dataWithBytes ( pyobj , length = len ( pyobj ) ) ) elif isinstance ( pyobj , decimal . Decimal ) : return ObjCInstance ( NSDecimalNumber . decimalNumberWithString_ ( pyobj . to_eng_string ( ) , convert_result = False ) ) elif isinstance ( pyobj , dict ) : dikt = NSMutableDictionary . dictionaryWithCapacity ( len ( pyobj ) ) for k , v in pyobj . items ( ) : dikt . setObject ( v , forKey = k ) return dikt elif isinstance ( pyobj , list ) : array = NSMutableArray . arrayWithCapacity ( len ( pyobj ) ) for v in pyobj : array . addObject ( v ) return array elif isinstance ( pyobj , bool ) : return ObjCInstance ( NSNumber . numberWithBool_ ( pyobj , convert_result = False ) ) elif isinstance ( pyobj , int ) : return ObjCInstance ( NSNumber . numberWithLong_ ( pyobj , convert_result = False ) ) elif isinstance ( pyobj , float ) : return ObjCInstance ( NSNumber . numberWithDouble_ ( pyobj , convert_result = False ) ) else : raise TypeError ( "Don't know how to convert a {cls.__module__}.{cls.__qualname__} to a Foundation object" . format ( cls = type ( pyobj ) ) )