【问题标题】:I have got AnyObject vehicle. need to access the values from vehicle object我有 AnyObject 车辆。需要访问车辆对象的值
【发布时间】:2015-07-22 08:38:59
【问题描述】:
{
        "id": 144241,
        "name": "Teltonika Test 8",
        "vrn": "ML57CEU3",
        "make": "Ford",
        "model": "Focus",
        "year": null,
        "icon": "red_car",
        "fuelType": "Petrol",
        "odometer": null,
        "useCostMile": 77,
        "idleCostHour": 77,
        "taxDate": "2015-12-05",
        "motDate": "2015-12-05",
        "nextServiceDate": "2015-12-05",
        "createdAt": 1433237926000,
        "updatedAt": 1433237926000,
        "privateCostPerMile": 77,
        "idleAlertTime": 77,
        "repeatPtoAlertTime": null,
        "payload": 0,
        "notifyMileage": 77,
        "siteVehicleType": null,
        "realRoadSpeed": false,
        "customer": {
            "id": 13492
        },
        "depot": {
            "id": 13492
        },
        "vehicleType": null,
        "deviceConfiguration": {
            "id": 139973
        },
        "liveView": {
            "id": 144061,
            "gpsPosition": {
                "id": 9209187941,
                "latitude": 3216.882,
                "longitude": -9.45553,
                "speed": 25,
                "heading": 0,
                "dateTime": 1351400858000
            },
            "currentStatus": "moving",
            "creationTime": 1433237926000,
            "createdAt": 1433237926000,
            "updatedAt": 1433237926000,
            "clockMeters": 77,
            "lastState": null,
            "batteryPercent": null
        },
        "businessHours": [
            {
                "id": 189251
            }
        ],
        "driver": null,
        "imei": null
    },

我想从 liveview 中显示 lastState。我没有在 Swift 编程中获得包装概念。

【问题讨论】:

    标签: swift uitableview uilabel viewcontroller


    【解决方案1】:

    试试这个。

    var dict = vehicleObject as! [String: AnyObject]
    var liveViewDict = dict["liveView"]! as! [String : AnyObject]
    var lastState = liveViewDict["lastState"] as! String
    

    此外,由于此值为 null 而不是 nil,因此您将让它检查其值是否为 NSNull

    【讨论】:

    • vehicleLastState.text = vehicle["liveView"]["lastState"] as! AnyObject 不工作
    • 需要一些关于快速编程的培训
    • @Raj 将带有类型转换的代码更新为 String。如果它仍然不起作用,请告诉我您遇到的错误是什么。并在控制台中打印 liveViewDict["lastState"] as! String 行中的值并告诉我它的值。
    • 谢谢它正在工作。我需要快速培训,你能教我吗
    • @Raj :D 你应该阅读The Swift Programming Language 的第一章Apple。它应该让你开始。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2018-10-13
    • 2021-11-05
    相关资源
    最近更新 更多