我认为您缺少 ObjectMapper lib 的正确文档。
检查这个Github 对象映射器 https://github.com/Hearst-DD/ObjectMapper#the-basics.
这些是该库支持的类型:
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)
}
}
}
}