无法使用 Wordpress 中的高级自定义字段值
Can't use advanced custom fields value from Wordpress
我正在尝试在我的 Swift 项目中使用来自我的 Wordpress 服务器的值。我得到 JSON 格式的信息。我想要的值是由 "advanced custom fields" 在 wordpress 服务器上创建的。
我正在从另一个视图控制器加载它并使用此代码加载它:
let nummer = NSUserDefaults.standardUserDefaults().valueForKey("nummer")
print(nummer)
然后我得到这个结果:
{
attachments = (
);
author = {
description = "";
"first_name" = "";
id = 1;
"last_name" = "";
name = admin;
nickname = admin;
slug = admin;
url = "";
};
categories = (
);
"comment_count" = 0;
"comment_status" = open;
comments = (
);
content = "<p>text</p>\n";
"custom_fields" = {
latitude = (
"22.324432"
);
longitude = (
"40.233221"
);
};
date = "2015-12-11 08:12:31";
excerpt = "<p>text</p>\n";
id = 37;
modified = "2015-12-11 08:12:31";
slug = "test-2";
status = publish;
tags = (
);
title = test;
"title_plain" = test;
type = post;
url = "http://localhost:8080/wordpress/2015/12/11/test-2/";
}
要获得标题,我使用此代码:
let dict = nummer!["title"] as? String
print(dict)
得到这个结果:
可选("test")
我使用此代码获取经度值:
let dict = nummer!["custom_fields"]
let longitude = dict!!["longitude"]
print(longitude)
结果:
可选((
“40.233221”
))
现在我想将其转换为双精度。但它 returns nil:
let longitude = dict!!["longitude"] as? Double
print(longitude)
结果:无
为什么它 returns nil 而不是 double 值?
当您使用 as?
时,您试图将展开的值向下转换为 Double
,但它不是 Double
,而是 Array
。
if let longitudeArray = dict!!["longitude"] as? Array<String> {
if let longitude = Double(longitudeArray[0]) {
print(longitude)
}
}
因为这个变量是 String
而不是 Double
。你必须转换它
if let longitudeString = dict!!["longitude"] as? String {
let longitude = Double(longitudeString)
}
或
if let longitudeString = dict!!["longitude"] as? String {
let longitude = longitudeString.doubleValue
}
我正在尝试在我的 Swift 项目中使用来自我的 Wordpress 服务器的值。我得到 JSON 格式的信息。我想要的值是由 "advanced custom fields" 在 wordpress 服务器上创建的。
我正在从另一个视图控制器加载它并使用此代码加载它:
let nummer = NSUserDefaults.standardUserDefaults().valueForKey("nummer")
print(nummer)
然后我得到这个结果:
{
attachments = (
);
author = {
description = "";
"first_name" = "";
id = 1;
"last_name" = "";
name = admin;
nickname = admin;
slug = admin;
url = "";
};
categories = (
);
"comment_count" = 0;
"comment_status" = open;
comments = (
);
content = "<p>text</p>\n";
"custom_fields" = {
latitude = (
"22.324432"
);
longitude = (
"40.233221"
);
};
date = "2015-12-11 08:12:31";
excerpt = "<p>text</p>\n";
id = 37;
modified = "2015-12-11 08:12:31";
slug = "test-2";
status = publish;
tags = (
);
title = test;
"title_plain" = test;
type = post;
url = "http://localhost:8080/wordpress/2015/12/11/test-2/";
}
要获得标题,我使用此代码:
let dict = nummer!["title"] as? String
print(dict)
得到这个结果: 可选("test")
我使用此代码获取经度值:
let dict = nummer!["custom_fields"]
let longitude = dict!!["longitude"]
print(longitude)
结果: 可选(( “40.233221” ))
现在我想将其转换为双精度。但它 returns nil:
let longitude = dict!!["longitude"] as? Double
print(longitude)
结果:无
为什么它 returns nil 而不是 double 值?
当您使用 as?
时,您试图将展开的值向下转换为 Double
,但它不是 Double
,而是 Array
。
if let longitudeArray = dict!!["longitude"] as? Array<String> {
if let longitude = Double(longitudeArray[0]) {
print(longitude)
}
}
因为这个变量是 String
而不是 Double
。你必须转换它
if let longitudeString = dict!!["longitude"] as? String {
let longitude = Double(longitudeString)
}
或
if let longitudeString = dict!!["longitude"] as? String {
let longitude = longitudeString.doubleValue
}