首页 > 解决方案 > JSON 值NSNull 类型的不能在 react native 中转换为 NSString

问题描述

当我在我的应用程序的主屏幕上登陆时,我一直遇到这个问题,我不是 100% 为什么会发生问题或可能是什么原因。我试过用谷歌搜索,但没有发现任何可以引导我走上正确道路的有用信息。

错误截图

认为它发生在这段代码的某个地方:

const InstaagramEmpty = () => <InstagramUsersEmpty text="No matching users found" />

const InstagramUserItem = (props: { onSelect: () => void, user: InstagramUser }) => {

    //TOOD: user button
    return <AccountContainer onPress={props.onSelect}>
        <AccountImage source={{ uri: props.user.photoUrl }} />
        <Text text={props.user.username} />
    </AccountContainer>
}

// Brinds image/text together
const SelectionContainer = styled(Row)` 
`

const SelectionClear = styled(ButtonIcon)`
    width:45px;
    height:45px; 
`

const InstagramUserSelection = (props: { onSelect: () => void, user: InstagramUser }) => {

    return <Row drop>
        <SelectionContainer>
            <AccountImage source={{ uri: props.user.photoUrl }} />
            <Text text={props.user.username} />
        </SelectionContainer>
        <SelectionClear icon={IconClose} iconTint={Colors.GreyDark} onPress={props.onSelect} />
    </Row>
}

标签: iosreact-nativereact-native-ios

解决方案


当您的 JSON 文档包含值 null 时,它将被转换为 NSNull 类的 Objective-C 对象。NSNull 是一个单例类;类方法 [NSNull null] 总是返回唯一存在的 NSNull 对象。如果给你一个未知类型的对象,你可以通过与 [NSNull null] 比较来检查它是否为 JSON null 值。if (myJSONObject == [NSNull null]) ...

所以你的 JSON 数据包含一个空值,它变成了 [NSNull null],并且 [NSNull null] 不能转换成任何东西。和 nil 一样处理。例如如果 (myJSONObject == nil || myJSONObject == [NSNull null]) ...


推荐阅读