-2

I tried to get some data from NSMutableDictionary,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{   
    long time = (long)[[tempPosts allKeys] objectAtIndex:section];
    return [[tempPosts objectForKey:[[NSNumber numberWithLong:time] stringValue]] count];
}

But this is giving me a <nil>. enter image description here

codebot
  • 2,540
  • 3
  • 38
  • 89
  • NSMutableDictionary need both key and values an NSObject. What is the exact object type of your key? Is it really a string? If not, passing an NSString won't help you to retrieve the mapped value. – Ken Cheung May 04 '16 at 09:24
  • I think instead of doing so much conversion, you should simply use, `NSString *key=[[tempPosts allKeys] objectAtIndex:section];` and then `[[tempPosts objectForKey:key] count];` see if it works.. – iphonic May 04 '16 at 09:25
  • Since you're debugging why don't you print the complete dictionary to the log and see the result. "po tempPosts" – Ruchira Randana May 04 '16 at 09:27
  • why not log time first and move forward do a break point and check your tempPost if key is there. please do basic things first – Joshua May 04 '16 at 09:28
  • @codebot what is [tempPosts objectForKey:@"1461745812000"] returning? – J. Lopes May 04 '16 at 11:15

5 Answers5

2

NSDictionaries are not sorted. allKeys returns keys in a random order. There is no guarantee whatsoever that it will return the same order twice, so this approach is inherently flawed. And this line:

long time = (long)[[tempPosts allKeys] objectAtIndex:section];

is absolutely completely wrong. You are picking one of the keys of the dictionary, which is a pointer to an object, most like an NSString*. You cast this to long - but casting an address of an NSObject to long just gives you meaningless bits.

I suggest you describe what you actually want.

And NSDictionary is entirely unsuitable for your data model, as you may have figured out by now. Take that dictionary and convert it into an object containing what your application needs.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • I realized that. Then I changed my code, Setting the object `[tempPosts setObject:postsArr forKey:@"1461745812000"];` and then I tryed to get like `[[tempPosts objectForKey:@"1461745812000"] count];`. Still fails. – codebot May 04 '16 at 10:32
  • I was thinking about this answer and here it is! @gnasher. +1. – J. Lopes May 04 '16 at 11:15
0

The cast to long is probably an error. Your key is probably an NSNumber* or an NSString*. If you want to convert it into a long you have to get the longValue. Casting it to long like you do, won't work.

KIDdAe
  • 2,714
  • 2
  • 22
  • 29
0

Use this below code, and its working for me

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{   

    NSLog(@"tempPosts %@",NSStringFromClass([tempPosts class]));

    return [[[tempPosts objectForKey:[numberArray objectAtIndex:section]] allKeys] count];
}

numberArray is NSMutableArray Value,

I have also same issue, then change the above code, hope its helpful

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
0

simply use this. it will return number of element in your dictionary.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return yourDictionary.count;
}

hope it will help you.

0

The objects in NSMutableDictionary are stored and accessed using a key.

setting the NSMutableDictionary

NSMutableDictionary * dictionary = [[NSMutableDictionary alloc]init];

storing in NSMutableDictionary e.g.

dictionary[@"someKey"] = @"someString";

accessing the stored element NSString as follow

NSString * string  = dictionary[@"someKey"];
iOS Geek
  • 4,825
  • 1
  • 9
  • 30