我正在处理深度嵌套的NSArray和NSDictionary,而且至少可以说它非常耗时.[data objectatindex:0] valueForKey:@"blah"]等等
有没有人知道一个漂亮的iOS类别,以递归方式记录结构,突出显示类型并显示值?
可能要问一点但你永远不知道:)
我有代码(剥离):
CFDictionaryRef *currentListingRef;
//declare currentListingRef here
NSDictionary *currentListing;
currentListing = (NSDictionary *) currentListingRef;
Run Code Online (Sandbox Code Playgroud)
然后我得到错误:
使用ARC禁止将非Objective-C指针类型'CFDictionaryRef*'(又名'const struct __CFDictionary**')转换为'NSDictionary*'
我究竟做错了什么?我如何从a转换CFDictionaryRef为NSDictionary?
我试图使用NSNotificationCenter将NSDictionary表单UIView传递给UIViewController.字典在发布通知时工作正常,但在接收方法中,我无法访问字典中的任何对象.
这是我如何创建字典并发布通知...
itemDetails = [[NSDictionary alloc] initWithObjectsAndKeys:@"Topic 1", @"HelpTopic", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"HotSpotTouched" object:itemDetails];
Run Code Online (Sandbox Code Playgroud)
在UIViewController中,我正在设置观察者......
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(hotSpotMore:)
name:@"HotSpotTouched"
object:nil];
Run Code Online (Sandbox Code Playgroud)
出于测试目的,hotSpotMore看起来像这样......
- (void)hotSpotMore:(NSDictionary *)itemDetails{
NSLog(@"%@", itemDetails);
NSLog(@"%@", [itemDetails objectForKey:@"HelpTopic"]);
}
Run Code Online (Sandbox Code Playgroud)
第一个NSLog工作正常,显示字典的内容.第二个日志抛出以下异常......
[NSConcreteNotification objectForKey:]: unrecognized selector sent to instance 0x712b130
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我无法访问传递的字典中的任何对象.
在此先感谢您的帮助.
约翰
我有一系列NSDictionaries.如何拉出字典中的第一个元素?
NSArray *messages = [[results objectForKey:@"messages"] valueForKey:@"message"];
for (NSDictionary *message in messages)
{
STObject *mySTObject = [[STObject alloc] init];
mySTObject.stID = [message valueForKey:@"id"];
stID = mySTObject.stID;
}
Run Code Online (Sandbox Code Playgroud) 我有一个plist文件,其中包含一系列字典.这是其中之一:
Fred字典
名称Fred
isMale [已选中框]
所以现在我用我从plist文件中读取的字典初始化我的Person对象:
-(id) initWithDictionary: (NSDictionary *) dictionary {
if (self = [super init])
self.name = [dictionary valueForKey: @"Name"];
self.isMale = ????
}
Run Code Online (Sandbox Code Playgroud)
如何完成上面的代码,以便在plist文件中选中复选框时将self.isMale设置为YES,如果不是则选择NO.如果字典中没有键isMale,则最好也设置为NO.
我有一个NSDictionary看起来像:
{
"Item1" = 2.4;
"Item2" = 5.5;
"Item3" = 15.6;
}
Run Code Online (Sandbox Code Playgroud)
要NSDictionary在表格视图中使用此项目,我必须将其转移到a NSArray,我是对的吗?
所以我尝试:
NSDictionary *dict = [myDict objectForKey:@"items"];
for (id item in dict) {
[_myArray addObject:[dict objectForKey:item]];
}
Run Code Online (Sandbox Code Playgroud)
但_myArray一直空着?我究竟做错了什么?
我需要将objective-c对象序列化和反序列化为JSON以存储在CouchDB中.人们是否有任何示例代码用于一般解决方案的最佳实践?我查看了一些JSON框架,它们在NSDictionary/NSArray级别停止.即很多框架将NSDictionary/NSArray序列化和反序列化为JSON.但我仍然需要将NSDictionary转换为Objective-C对象.
为了使事情变得更复杂,我的对象A可以引用对象B的NSArray/NSDictionary.
我的问题与这个问题非常相似,增加了收集要求.
我可以通过两种方式测试NSDictionary中是否存在密钥:
BOOL containsKey = [[dictionary allKeys] containsObject:foo];
BOOL containsKey = ([dictionary objectForKey:foo] != nil);
Run Code Online (Sandbox Code Playgroud)
哪种方法更快,为什么?
嗨,我有一个NSdictionary我在其中添加一个关键"国家"的数组.现在我把这个字典的值放到数组中并按照alpahbatical顺序对数组进行排序.现在我想将这个数组添加到我的字典中(我想用新的排序数组更新我的字典并从中删除旧数组). ....... 这该怎么做
我的代码如下
NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Iceland", @"Greenland", @"Switzerland", @"Norway", @"New Zealand", @"Greece", @"Italy", @"Ireland", nil];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];
NSArray *tmpary = [countriesToLiveInDict valueForKey:@"Countries"];
NSLog(@"ary value is %@",ary);
NSArray *sortedArray = [tmpary sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSLog(@"sortedArray is %@",sortedArray);
Run Code Online (Sandbox Code Playgroud)
在这里,我想删除countriesToLiveInArray并将其替换为具有相同键值的sortedArray,即国家/地区提前感谢..
JSON_CATEGORY_DATA_URL_STRING我的Feed网址在哪里,返回正常:
[
{
"group":"For Sale",
"code":"SSSS"
},
{
"group":"For Sale",
"category":"Wanted",
"code":"SWNT"
}
]
Run Code Online (Sandbox Code Playgroud)
我似乎无法从以下代码中获得一个好的NSDictionary(或NSArray):
+ (NSDictionary *)downloadJSON
{
NSDictionary *json_string;
NSString *dataURL = [NSString stringWithFormat:@"%@", JSON_CATEGORY_DATA_URL_STRING];
NSLog(@"%@",dataURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURL]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
json_string = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]autorelease];
NSDictionary *json_dict = (NSDictionary *)json_string;
NSLog(@"json_dict\n%@",json_dict);
NSLog(@"json_string\n%@",json_string);
return json_string;
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了很多这方面的帖子,但我没有得到它.
nsdictionary ×10
objective-c ×7
nsarray ×4
cocoa ×3
ios ×3
iphone ×3
xcode ×2
boolean ×1
casting ×1
cocoa-touch ×1
couchdb ×1
ios4 ×1
json ×1
performance ×1
sbjson ×1