为什么我在React Native iOS中收到“发送给实例的无法识别的选择器”?

时间:2019-07-06 11:47:21

标签: ios react-native

我的代码在Android上运行完美,但在iOS上显示错误。

iOS中的错误

Error on iOS mobile

我无法理解此错误;与AsyncStorage有关系吗?

为什么在iOS设备上会发生这种情况?


第一个文件

我的进口

import React, {Component} from 'react';

import { Alert, Dimensions, Image, TouchableOpacity, AsyncStorage } from 'react-native';

import { Container, Body, Footer, Header, Input, Item, Left, Text, Title, Right, View, Button, Label, Form} from 'native-base';

import { SimpleLineIcons, Ionicons } from '@expo/vector-icons';

import { NavigationActions } from 'react-navigation';

import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';

import { LinearGradient } from 'expo';

import { StatusBar } from "react-native";

import { Grid, Row, Col } from 'react-native-easy-grid';

import Toast, {DURATION} from 'react-native-easy-toast';

import Strings from '../utils/Strings';
var width = Dimensions.get('window').width;

export default class Login extends Component {
    static navigationOptions = {
        header: null
    };

    constructor() {
        super();
        this.state = {
            MobileNo: '',
        };
    }

    login = () => {
        AsyncStorage.setItem('mobileno', MobileNo);

        const { MobileNo } = this.state;
        console.log("Expected login number " + MobileNo);

        fetch('http://demo.weybee.in/Backend/controller/User_Login.php', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                mobileno: MobileNo
            })
        }).then((response) => response.json())
        .then((responseJson) => {
            // If server response message same as Data Matched
            if(responseJson != 'Enter valid phone number') {   
                const { navigation } = this.props;
                // Then open Profile activity and send user email to profile activity.
                this.props.navigation.navigate('ForgetPass');
            } else {
                this.refs.toast.show('Invalid Number', DURATION.LENGTH_LONG);
            }
        }).catch((error) => {
            console.error(error);
        });
    }
}

第二个文件

我的进口

import React, {Component} from 'react';

import { Alert, Dimensions, Image, TouchableOpacity, AsyncStorage } from 'react-native';

import { Container, Body, Footer, Header, Input, Item, Left, Text, Title, Right, View, Button, Label, Form} from 'native-base';

import { SimpleLineIcons, Ionicons } from '@expo/vector-icons';

import { NavigationActions } from 'react-navigation';

import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';

import { LinearGradient } from 'expo';

import { StatusBar } from "react-native";

import { Grid, Row, Col } from 'react-native-easy-grid';

import Toast, {DURATION} from 'react-native-easy-toast'

import Strings from '../utils/Strings';

import OtpInputs from 'react-native-otp-inputs';
var width = Dimensions.get('window').width; 

export default class Login extends Component {
    static navigationOptions = {
        header: null
    };

    constructor() {
        super();
        this.state = {
            MobileNo: '',
            mobileNumber: '',
            code: '',
        }
    }

    componentDidMount() {
        AsyncStorage.getItem('mobileno').then((mobileNo) => {
            if(mobileNo){
                this.setState({ mobileNumber: mobileNo });
            }
        });
    }

    PTP = () => {
        let mobileNumber = JSON.parse(this.state.mobileNumber);
        console.log("login number " + mobileNumber);

        let {code} = this.state;
        console.log(code);

        fetch('http://demo.weybee.in/Backend/controller/Get_PTP.php', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                mobileno: mobileNumber,
                code: code,
            })
        }).then((response) => response.json())
        .then((responseJson) => {
            // If server response message same as Data Matched
            if(responseJson != 'Enter valid phone number') {     
                const { navigation } = this.props;
                // Then open Profile activity and send user email to profile activity.
                this.props.navigation.navigate('Home');
            } else {
                this.refs.toast.show('Invalid PTP', DURATION.LENGTH_LONG);
            }
        }).catch((error) => {
            console.error(error);
        });
    }
}

2 个答案:

答案 0 :(得分:1)

我认为问题可能在于您如何将MobileNo保存到AsyncStorage。 MobileNo是否不是州的一部分,不应该称为this.state.MobileNo

在FirstFile内部,这是问题所在,

AsyncStorage.setItem('mobileno', MobileNo);

应该是

AsyncStorage.setItem('mobileno', this.state.MobileNo);

答案 1 :(得分:0)

将空值传递给AsyncStorage.setItem时出现此错误:

AsyncStorage.setItem('user_id', null) // breaks!

要解决此问题,我刚刚传递了一个字符串作为setItem命令的值:

AsyncStorage.setItem('user_id', 'tupac_without_a_nosering') // good!