ios React Native - NSNumber 无法转换为 NSString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35640369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React Native - NSNumber cannot be converted to NSString
提问by Sohrab Hejazi
Below is part of my react component. I have a props named daysUntil coming into this component which contains a number. In this example it is being pass the number 0 which results in the fontWeight function returning 700
下面是我的反应组件的一部分。我有一个名为 daysUntil 的道具进入这个包含一个数字的组件。在此示例中,它正在传递数字 0,这导致 fontWeight 函数返回 700
render: function() {
return (
<Text style={this.style()}>
{this.props.day}
</Text>
)
},
style: function() {
return {
fontWeight: this.fontWeight()
}
},
fontWeight: function() {
var weight = 7 - this.props.daysUntil;
return weight * 100;
}
I get the following error:
我收到以下错误:
JSON value '700' of type NSNumber cannot be converted to NSSTring.
NSNumber 类型的 JSON 值“700”无法转换为 NSSTring。
I'm assuming this is because font-weight expects the value to be in string format. What's the proper fix for this?
我假设这是因为 font-weight 期望值是字符串格式。什么是正确的解决方法?
Thank you in advance!
先感谢您!
回答by Robert Moskal
In your fontWeight() function
在您的 fontWeight() 函数中
return weight * 100;
maybe becomes:
也许变成:
var val= weight * 100;
return val.toString();
回答by ehacinom
I had a similar problem, where I was passing in an icon instead of a uri to an Image. The code was written to accept icon = 'path/to/icon'
:
我有一个类似的问题,我将一个图标而不是一个 uri 传递给一个图像。编写代码以接受icon = 'path/to/icon'
:
<Image source={{ uri: icon }}>
but I was passing in icon = require('path/to/icon')
and I had to switch the jsx to
但我正在传递icon = require('path/to/icon')
,我不得不将 jsx 切换到
<Image source={icon}>
回答by Leon
fontWeight requires a string value and not an integer.
fontWeight 需要字符串值而不是整数。
Just make sure you return a string:
只要确保你返回一个字符串:
return (weight * 100).toString();
Make also sure that your "weight" variable is not equal to zero.
还要确保您的“权重”变量不等于零。
回答by ismnoiet
You can use StyleSheet
from react-native
module, something like this:
您可以使用StyleSheet
fromreact-native
模块,如下所示:
import StyleSheet from 'react-native'
// declare the styles using Stylesheet.create
const myStyles = StyleSheet.create({marginTop:30})
//... some code inside render method
<Text style={myStyles}>
This is an example
</Text>
回答by NuOne
in react font weight should be a string ,
在反应字体重量应该是一个字符串,
in react doc they have specifically mention that
fontWeight enum('normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900')
Specifies font weight. The values 'normal' and 'bold' are supported for most fonts. Not all fonts have a variant for each of the numeric values, in that case the closest one is chosen.
在反应文档中,他们特别提到
fontWeight enum('normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900')
Specifies font weight. The values 'normal' and 'bold' are supported for most fonts. Not all fonts have a variant for each of the numeric values, in that case the closest one is chosen.
so you can choose like following
所以你可以选择如下
const boldText = {
fontWeigth: '100'
}
or
或者
const boldText = {
fontWeight: 'bold'
}
in this code you can say
在这段代码中你可以说
style: function() {
return {
fontWeight: this.fontWeight()
}
},
fontWeight: function() {
var weight = 7 - this.props.daysUntil;
return (weight * 100).toString();
}