您的位置:首页 > 移动开发 > Objective-C

Decodes a String into an object of the specified type

2016-03-16 08:59 627 查看
 /**

     * Decodes a String into an object of the specified type. If the object

     * type is not supported, null will be returned.

     *

     * @param type the type of the property.

     * @param value the encode String value to decode.

     * @return the String value decoded into the specified type.

     * @throws Exception If decoding failed due to an error.

     */

    private static Object decode(Class<?> type, String value) throws Exception {

        if (type.getName().equals("java.lang.String")) {

            return value;

        }

        if (type.getName().equals("boolean")) {

            return Boolean.valueOf(value);

        }

        if (type.getName().equals("int")) {

            return Integer.valueOf(value);

        }

        if (type.getName().equals("long")) {

            return Long.valueOf(value);

        }

        if (type.getName().equals("float")) {

            return Float.valueOf(value);

        }

        if (type.getName().equals("double")) {

            return Double.valueOf(value);

        }

        if (type.getName().equals("java.lang.Class")) {

            return Class.forName(value);

        }

        return null;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: