【问题标题】:Modeling JSON result in Core Data Editor在 Core Data Editor 中建模 JSON 结果
【发布时间】:2014-08-25 11:19:49
【问题描述】:

我正在学习 Core Data,我需要创建一个名为 Country 的实体。我从我的 API 收到此响应,但我不确定如何在我的国家实体中创建和存储 JSON 响应。

{
    "total_count":10,
    "results": [
        {
            "name": "Spain",
            "population" : 45.000,
            "location" : {
                "latitude" : 29.567423,
                "longitude" : -5.675326
            }
        },
        {
            "name": "France",
            "population" : 25.000,
            "location" : {
                "latitude" : 29.567423,
                "longitude" : -5.675326
            }
        },
        {
            "name": "Germany",
            "population" : 15.000,
            "location" : {
                "latitude" : 29.567423,
                "longitude" : -5.675326
            }
        }
    ]
}

父字段(name,population)没问题,但是实体图中怎么设置子字段(locationkey)呢?也许是NSDictionary

【问题讨论】:

    标签: ios objective-c json core-data


    【解决方案1】:

    有很多可能性。

    您可以创建两个单独的属性,例如具有类型 (double, double) 的 (latitude, longitude) 并在获取时使用两个值创建位置,或者您可以使用这两个值创建 CGPoint然后从CGPoint 创建字符串并存储它们

    CGPoint location = CGPointMake(latitude, longitude);
    NSString *stringLocation = NSStringFromCGPoint(point);
    

    存储该字符串并获取它,然后再次转换为CGpoint 并使用它

    CGPoint myPoint = CGPointFromString(stringLocation);
    

    【讨论】:

    • 创建从 location 属性到 Country 实体到名为 countryLocation 的新实体的关系,其中两个浮点数作为属性将是一个不错的选择?
    • 我认为在这种情况下不需要添加另一个实体。您可以简单地创建两列,一列用于纬度,另一列用于经度。
    • 我不会谈论列,而只会谈论属性。 Core Data 不是数据库。
    猜你喜欢
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    相关资源
    最近更新 更多