尝试将JSON数据发送到站点时,我一直收到错误.但是当我检查网站时,我发现json中的所有数据都已发送并且是正确的.
我得到的错误是:
Error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
Run Code Online (Sandbox Code Playgroud)
我的代码是:
let json = [
"name": "john",
"last name": "smith"
]
do{
let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: .PrettyPrinted)
let url = NSURL(string: website)
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPBody = jsonData
let task = …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我正在处理的本机应用程序中向某些图像添加填充。根据设备宽度调整图像大小,以便每行显示四张。在此props.images中,显示了9张图像,没有任何填充。
我试图将图像包装在填充为1的视图中,如下所示:
renderRow(rowData){
const {uri} = rowData;
return(
<View style={{padding: 1}}>
<Image style={styles.imageSize} source={{uri: uri}} />
</View>
)
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试仅前三个图像出现填充时。其余图像不会出现。
我的整个代码在这里:
class ImageList extends Component {
componentWillMount(){
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
this.dataSource = ds.cloneWithRows(this.props.images);
}
renderRow(rowData){
const {uri} = rowData;
return(
<Image style={styles.imageSize} source={{uri: uri}} />
)
}
render() {
return (
<View>
<ListView
contentContainerStyle={styles.list}
dataSource={this.dataSource}
renderRow={this.renderRow}
/>
</View>
);
}
}
var styles = StyleSheet.create({
imageSize: {
//newWidth is the …Run Code Online (Sandbox Code Playgroud) 我正在努力将 NSData 实例的内容写入文件。我目前正在使用 Xcode 游乐场。
这是我的代码:
let validDictionary = [
"numericalValue": 1,
"stringValue": "JSON",
"arrayValue": [0, 1, 2, 3, 4, 5]
]
let rawData: NSData!
if NSJSONSerialization.isValidJSONObject(validDictionary) {
do {
rawData = try NSJSONSerialization.dataWithJSONObject(validDictionary, options: .PrettyPrinted)
try rawData.writeToFile("newdata.json", options: .DataWritingAtomic)
} catch {
// Handle Error
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个名为 newdata.json 的文件位于资源中,但是当我检查它时,里面什么也没有。我也尝试删除并查看文件是否会被创建,但它仍然不起作用。