使用Alamofire ObjectMapper映射到Swift对象的问题显示为零 [英] Mapping to Swift Object issue showing nil using Alamofire ObjectMapper
问题描述
我是iOS和Swift开发环境的新手.我试图使用Alamofire提取JSON,使用AlamofireObjectMapper将检索到的JSON集合映射回我的Swift对象.
I m new to iOS and Swift development environment. I was trying to use Alamofire for pulling JSON and AlamofireObjectMapper for Mapping the retrieved JSON collections back to my Swift Object.
问题是我可以通过Alamofire请求获取JSON,并显示了计数,但映射部分似乎显示为nil.是我一直错过的事情.感谢帮助.
The issue is I m able to fetch the JSON thru Alamofire request and the count is shown, but the mapping part seems to be showing nil. Is something I had been missed out . Appreciate help.
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响应如下所示.在模型中只构造了需要的.
The complete JSON response looks like below. Have constructed only needed ones in Model.
[{"Id":"16","areaId":"17","name":"Al Aqiq","cityId":4," Zip:"," nameAr:" \ u0637 \ u0631 \ u064a \ u0642 \ u0627 \ u0644 \ u0645," branchAddr:"测试," branchAddr2:"测试纬度":"24.60425",经度":"46.629631","cityId":"1}]
[{"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 lib的正确文档. 选中此 Github ObjectMapper .
i think you are missing the right documentation of ObjectMapper lib. Check this Github ObjectMapper.
这些是lib支持的类型:
These are the types supported by the lib:
-
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>>
- 以上所有选项
- 上述内容的未包装选项
Int
Bool
Double
Float
String
RawRepresentable
(Enums)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>>
- Optionals of all the above
- Implicitly Unwrapped Optionals of the above
因此,如果您尝试映射不在该列表中的对象,则结果为nil
.
So, if you are trying to map an Object not in that list, the result is nil
.
您的情况是var location: CLLocation?
.
如果需要映射CLLocation对象,一种方法是使用所有属性映射CustomCLLocation,如下所示: JSON(我不知道您的Json,这是一个示例)
If you need to map CLLocation Object, one way is to map a CustomCLLocation with all properties as follows: JSON(i don't know your Json, this is an example)
"location":{
"long": 43.666,
"lat": 73.4
}
快速:创建另一个文件"CustomCLLocation",例如第一个文件,但用于将CLLocation与您的Json映射
Swift: create another file "CustomCLLocation" for example like the first one but for mapping CLLocation with your 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?
and now, you can map an "fake" CLLocation Object: var location: CustomCLLocation?
然后,如果您想要一个真正的CLLocation.只需创建一个像这样的Simply Extension(将其添加到CustomCLLocation文件中)即可:
Then if you want a really CLLocation. just create an Simply Extension like that(add it in the CustomCLLocation file):
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的应用程序有相同的请求
Edited: Alamofire Request
I have the same request on my app with the last version of AlamofireObjectMapper for ios 8.0 +
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)
}
}
}
}
这篇关于使用Alamofire ObjectMapper映射到Swift对象的问题显示为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!