nsnumber类型的json值'2'无法转换为nsstring [英] json value '2' of type nsnumber cannot be converted to nsstring

查看:388
本文介绍了nsnumber类型的json值'2'无法转换为nsstring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用从库中导入的Carousel制作imageSlider: react-native-banner-carousel

I am working on a imageSlider using Carousel imported from library: "react-native-banner-carousel"

我正在尝试获取本地存储的图像里面有一个文件夹组件。我正在创建一个图像数组,然后尝试通过Carousel渲染它们。

I am trying to fetch the images which are stored locally inside a folder components. I am creating an array if images and then trying to render them through Carousel.

我当然会收到类似以下错误: nsnumber类型的json值'2'不能是转换为nsstring

I am certainly getting an error like: "json value '2' of type nsnumber cannot be converted to nsstring"

ImageSlider代码:

ImageSlider code:

const BannerWidth = Dimensions.get('window').width;
const BannerHeight = 260;

const images = [
    require("./abc.jpg"),
    require("./xyz.jpg")
];

export default class ImageSlider extends React.Component {
    renderPage(image, index) {
        return (
            <View key={index}>
                <Image style={{ width: BannerWidth, height: BannerHeight }} source={{uri:image}} /> 
            </View>
        ); 
    }

render() {
    return (
        <View style={styles.container}>
            <Carousel
                autoplay
                autoplayTimeout={5000}
                loop
                index={0}
                pageSize={BannerWidth}
            >
                {images.map((image, index) => this.renderPage(image, index))}
            </Carousel>
        </View>
    );
}
}

    const styles = StyleSheet.create({
        container: {
            flex: 1,
            backgroundColor: '#fff',
            justifyContent: 'center'
        },
    });

因此,我应该能够渲染图像数组中显示的所有图像。但我收到此错误。
我在哪里出问题了?

As a result I should be able to render all the images displayed inside the images array. But I am getting this error. Where am I getting wrong here?

推荐答案

您的问题在这里:

const images = [
    require("./abc.jpg"),
    require("./xyz.jpg")
];

require不返回图像的URL,而是返回媒体的内部索引。在您的情况下,我假设是2,并且图像的接口需要一个字符串。

The require does not return the URL of the image but the internal index of the media. Which in your case I assume is 2, and the interface for the image is expecting a string.

尝试一下:

<Image style={{ width: BannerWidth, height: BannerHeight }} source={image} />

这篇关于nsnumber类型的json值'2'无法转换为nsstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆