3

Here i am using NSMutableArray to store date, then i tried to set key and assign ArrayValue in dictionary but the app crashed, please help me

Thanks in ADvance

 Here i tried the code for your reference:

  [DateArray addObject:dateString]; //NSMutablArray 
  NSMutableDictionary *myDictionary =[[NSMutableDictionary alloc]init];         
  [myDictionary setObject:DateArray forKey:@"Date"]; //put array value and set key in NSDictionary.
The iOSDev
  • 5,237
  • 7
  • 41
  • 78
SampathKumar
  • 2,525
  • 8
  • 47
  • 82

5 Answers5

10

NSDictionary Class is immutable. You must convert to NSMutableDictionary.

bitmapdata.com
  • 9,572
  • 5
  • 35
  • 43
  • Now i used NSMutableDictionary to store date fine, then How to sort this dictionary, is it possible? – SampathKumar Aug 09 '12 at 12:06
  • refer a following q&a: http://stackoverflow.com/questions/4558639/sort-an-nsmutabledictionary http://stackoverflow.com/questions/8118932/how-can-i-sort-nsmutabledictionary-with-keys-value – bitmapdata.com Aug 09 '12 at 12:09
  • There isn't actually any need to use a mutable dictionary here, instead just use the appropriate `init` method or new syntax. – Paul.s Aug 09 '12 at 12:14
2

You are using NSDictionary. You should use NSMutableDictionary. NSDictionary is immutable. If you want to use NSDictionary then use below method:

- (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
Apurv
  • 17,116
  • 8
  • 51
  • 67
  • Now i used NSMutableDictionary to store date fine, then How to sort this dictionary, is it possible? – SampathKumar Aug 09 '12 at 12:07
  • dictionary can not be sorted. You can sort an array. Also you should accept my answer because I have given it before. – Apurv Aug 09 '12 at 12:08
  • Your answer is pretty ambiguous. You have just given the method declaration without showing how to actually call it. This could very easily lead to any newbie using `[NSDictionary initWithObjects:....` as this is where the method is declared. – Paul.s Aug 09 '12 at 12:12
2

If you are using XCode version 4.4 or later you can jus do this:

[dateArray addObject:dateString]; //NSMutablArray 
NSDictionary *myDictionary = @{ @"Date", dateArray };
Cyprian
  • 9,423
  • 4
  • 39
  • 73
0

You're using an NSDictionary when you should be using NSMutableDictionary

Try this line :

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

Most of the collection classes in iOS have a mutable and nonmutable version (i.e.

NSArray -> NSMutableArray
NSSet -> NSMutableSet
NSDictionary -> NSMutableDictionary
(and others)

A mutable version will let you modify the contents. However, if you know you're not going to change it then a non mutable version will be slightly faster to use.

You can (usually) get a mutable class from a nonmutable one by using the mutableCopy method i.e.

// This array can't be changed
NSArray *myArray = @[ @"A", @"B", @"C" ];

// This array contains everything from the previous array but can now be modified
MSMutableArray myArray2 = [myArray mutableCopy];

NB : There are also other classes that have mutable subclasses i.e. NSURL -> NSMutableURL

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • check the question again before answering such a long answer. – Tripti Kumar Aug 09 '12 at 12:00
  • Hang on, did you edit it and change the class names from immutable to mutable? __You've changed the question that is being asked__; do you know the original questioner? Edit is designed to make the question more correctly asked, not to 'fix' it - if you want to fix the code, answer the question! – deanWombourne Aug 09 '12 at 12:02
  • Check out the name of Editor, don't blame me. – Tripti Kumar Aug 09 '12 at 12:03
-2

Do it like this:

for(int i=0;i<[DateArray count]; i++)
{
   [myDictionary addObject:[DateArray objectAtIndex:i] forKey:@"Date"];
}
Tripti Kumar
  • 1,559
  • 14
  • 28