React Native - nsnumber类型的json值'0'无法转换为nsstring

Posted

技术标签:

【中文标题】React Native - nsnumber类型的json值\'0\'无法转换为nsstring【英文标题】:React Native - json value '0' of type nsnumber cannot be converted to nsstringReact Native - nsnumber类型的json值'0'无法转换为nsstring 【发布时间】:2020-04-04 23:56:12 【问题描述】:

我正在尝试将在我的 React 应用程序中工作的组件转换为 React Native。但是,我在 ios 模拟器中收到此错误: nsnumber 类型的 json 值 '0' 无法转换为 nsstring' 以前有人遇到过吗?这似乎与其他所有人的造型有关。但是我还没有造型。 如果您需要更多信息,请告诉我,谢谢。

import React from 'react';
import  Text, View, Button, Picker  from 'react-native';


const baseMin = [];
for (var i=0; i <= 60; i++) 
    baseMin.push(i);


const baseSec = [];
for (var i=0; i <= 60; i++) 
    baseSec.push(i);


const displayMinutes = baseMin.map((minute) => 
    <Picker.Item key=minute.toString() value=minute label=minute />
)

const displaySeconds = baseSec.map((second) => 
    <Picker.Item key=second.toString() value=second label=second />
)


class RandomTimer extends React.Component 
    state =  
            baseMinutes: 0, 
            baseSeconds: 0, 
            varMinutes: 0, 
            varSeconds: 0,
            minutes: 0,
            seconds: 0,
            random: 0
        ;

    generate() 


        var min = (this.state.baseSeconds) - (this.state.varSeconds);

        var max = (parseInt(this.state.varSeconds)) + (parseInt(this.state.baseSeconds));

        const randomInt = Math.floor(Math.random() * (max - min + 1) + min);
        this.setState( random: randomInt );
        this.reset();

    

    go() 

        this.timer = setInterval(() => 

            if (this.state.seconds == this.state.random) 
                clearInterval(this.timer);

             else 
                this.setState( seconds: this.state.seconds + 1 )

            
        , 1000)
    

    stop = () => 
        clearInterval(this.timer);
     

     reset = () => 
         this.setState( minutes: 0, seconds: 0 )
         this.stop();
     

    render() 

        const varMin = [];
        for (var i=0; i <= this.state.baseMinutes; i++) 
            varMin.push(i);
        
        const displayBaseMin = varMin.map((minute) => 
            <Picker.Item key=minute.toString() value=minute label=minute />
        )

        const varSec = [];
        for (var i=0; i <= this.state.baseSeconds; i++) 
            varSec.push(i);
        
        const displayBaseSec = varSec.map((second) =>
            <Picker.Item key=second.toString() value=second label=second />
        )

        const  baseMinutes, baseSeconds, varMinutes, varSeconds, minutes, seconds  = this.state;

        return (
            <View>
                <Text>settings</Text>

                <Picker onValueChange=
                    (itemValue) => this.setState(
                        baseSeconds: itemValue
                    ) selectedValue=baseMinutes>
                    displayMinutes
                </Picker>

                <Text>:</Text>

                <Picker onValueChange=
                    (itemValue) => this.setState(
                        baseSeconds: itemValue
                    ) selectedValue=baseSeconds>
                    displaySeconds
                </Picker>

                <Text>VarTime +-</Text>
                <Picker onValueChange=
                    (itemValue) => this.setState(
                        baseSeconds: itemValue
                    ) selectedValue=varMinutes>
                    displayBaseMin
                </Picker>
                <Text>:</Text>
                <Picker onValueChange=
                    (itemValue) => this.setState(
                        baseSeconds: itemValue
                    ) selectedValue=varSeconds>
                    displayBaseSec
                </Picker>
               <Button onPress=this.generate title='new' />

               <Text>Timer</Text>

                <Text>minutes:seconds</Text>


                <View>
                    <Button onPress=this.go title='go' />
                    <Button onPress=this.stop title='stop' />
                    <Button onPress=this.reset title='reset' />
                </View>

            </View>
        )
    


export default RandomTimer;

【问题讨论】:

【参考方案1】:

您做错的唯一一件事是 Picker 标签应该是一个字符串,但您传递一个数字只需将您的 minute 替换为 minute.toString()secondsecond.toString(),您需要每次更改此标签你在哪里使用。

并将go() 更改为go = () =&gt;generate() 更改为generate = () =&gt;

希望这会有所帮助!

【讨论】:

以上是关于React Native - nsnumber类型的json值'0'无法转换为nsstring的主要内容,如果未能解决你的问题,请参考以下文章

nsnumber 类型的 json 值“2”无法转换为 nsstring

react native 怎么设置键盘类型

React-native (expo) - 无效类型

使用 Relay + React Native (create-react-native-app) 时出错:GraphQL 验证错误`未知类型“查看器”。`

react-native 动画库的流类型

React Native 相机条形码类型