我是 iOS 和 Swift 开发环境的新手。我试图使用 Alamofire 拉取 JSON,并使用 AlamofireObjectMapper 将检索到的 JSON 集合映射回我的 Swift 对象。
问题是我能够通过 Alamofire 请求获取 JSON 并显示计数,但映射部分似乎显示为零。是我错过了什么。感谢帮助。
模型类
import UIKit
import CoreLocation
import ObjectMapper
class BranchObjectMapper : Mappable {
// MARK: Properties
var id: Int?
var cityId: Int?
var areaId: Int?
var name: String?
var nameAr: String?
var branchAddr: String?
var branchAddr2: String?
var location: CLLocation?
required init?(_ map: Map) {
mapping(map)
}
func mapping(map: Map) {
id <- map["id"]
cityId <- map["cityId"]
areaId <- map["areaId"]
name <- map["name"]
nameAr <- map["nameAr"]
branchAddr <- map["branchAddr"]
branchAddr2 <- map["branchAddr2"]
location <- map["location"]
}
}
viewDidLoad()
中的请求部分
Alamofire.request(.GET, UrlEndpoints.branchUrl()).responseArray { (response: Response<[BranchObjectMapper], NSError>) in
self.branchList = response.result.value!
print(self.branchList.count) // count is correct
for branch in self.branchList {
print(branch.id) // prints nil
print(branch.id) // prints nil
}
}
提前致谢
完整的 JSON 响应如下所示。在模型中只构造了需要的。
[{"Id":"16","areaId":"17","name":"Al Aqiq”,”cityId”:4”,”Zip":"","nameAr":"\u0637\u0631\u064a\u0642 \u0627\u0644\u0645","branchAddr":"test","branchAddr2":"test"Latitude":"24.60425","Longitude":"46.629631","cityId":"1"}]
最佳答案
我认为您缺少 ObjectMapper 库的正确文档。 检查这个Github ObjectMapper .
这些是库支持的类型:
-
Int
-
Bool
-
Double
-
Float
-
String
-
RawRepresentable
(枚举) -
Array<AnyObject>
-
Dictionary<String, AnyObject>
-
Object<T: Mappable>
-
Array<T: Mappable>
-
Array<Array<T: Mappable>>
-
Set<T: Mappable>
-
Dictionary<String, T: Mappable>
-
Dictionary<String, Array<T:Mappable>>
- 以上所有选项
- 上面的隐式解包选项
因此,如果您尝试映射不在该列表中的对象,则结果为 nil
.
在你的例子中是 var location: CLLocation?
.
如果需要映射 CLLocation 对象,一种方法是映射一个具有所有属性的 CustomCLLocation,如下所示: JSON(我不知道你的 Json,这是一个例子)
"location":{
"long": 43.666,
"lat": 73.4
}
Swift:创建另一个文件“CustomCLLocation”,例如与第一个文件类似,但用于将 CLLocation 映射到您的 Json
var latitude: Double?
var longitude: Double?
required init?(_ map: Map) {
mapping(map)
}
func mapping(map: Map) {
longitude <- map["long"]
latitude <- map["lat"]
}
现在,您可以映射一个“假的”CLLocation 对象: var location: CustomCLLocation?
然后如果你想要一个真正的 CLLocation。只需创建一个像这样的简单扩展(将其添加到 CustomCLLocation 文件中):
extension CLLocation {
public class func createFromCustomCLLocation(custom: CustomCLLocation) -> CLLocation {
return self.init(custom.latitude,custom.longitude)
}
}
使用转换:
var locationCLL = CLLocation.createFromCustomCLLocation(location) // now is a CLLocation
已编辑:Alamofire 请求
我对我的应用程序有与适用于 ios 8.0 + 的最新版本的 AlamofireObjectMapper 相同的请求
Alamofire.request(.GET, UrlEndpoints.branchUrl()).responseArray { (response: [BranchObjectMapper]?, error : ErrorType?) in
if(error != nil) {
print(error)
}else{
print("data downloaded")
if let response = response {
branchList(response)
for branch in self.branchList {
print(branch.id)
print(branch.name)
}
}
}
}
关于ios - 使用 Alamofire ObjectMapper 映射到显示 nil 的 Swift 对象问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35197369/