如何在类属性的Objective C中创建类似json的结构?
Posted
技术标签:
【中文标题】如何在类属性的Objective C中创建类似json的结构?【英文标题】:How to create json like structure in Objective C in class property? 【发布时间】:2016-11-29 13:13:42 【问题描述】:我有这样的视图控制器类
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) NSDictionary *dictionary;
@end
ViewController.m
#import "ViewController.h"
#import "GoogleMaps.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//HOW TO ACCESS THE PROPERTY VALUE HERE
self.dictionary = @;
/ DUMP ALL FOUND ITEMS
for(DummyContainer* geoItem in geoItems)
NSDictionary *item = @
@"latitude":geoItem.latitude,
@"longtitude":geoItem.longtitude
;
self.dictionary[geoItem.geoPoint.name] = item;
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@end
预期的格式是
var container =
'location':
'latitude':1233,
'longtitude':124
这可以通过
let obj = container['location'];
像这样的纬度访问
obj.latitude;
我是 ios 新手,请帮助我提前谢谢。
【问题讨论】:
appcoda.com/fetch-parse-json-ios-programming-tutorial 这可以帮助 你把 Objective-C 和 Swift 混为一谈了。我建议只从 Swift 和 Swift 开始。 请帮我找出目标c代码。我只在寻找目标 c 【参考方案1】:用于创建不可扩展/不可变的字典对象
@property (strong, nonatomic) NSDictionary *myClassDictionary;
用于创建可扩展/可变字典对象
@property (strong, nonatomic) NSMutableDictionary *myClassMutableDictionary;
像这样在字典中插入所有值 你的例子数据
'location':
'latitude':1233,
'longtitude':124
NSDictionary *dict = @@"lattitude":@"1233" , @"longitude":@"124";
self.myClassDictionary = @@"location":dict;//Convert this dictionary into JSON.
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject: self.myClassDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString jsonString;
if (! jsonData)
NSLog(@"Got an error: %@", error);
else
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
【讨论】:
以上是关于如何在类属性的Objective C中创建类似json的结构?的主要内容,如果未能解决你的问题,请参考以下文章