user559142
user559142

Reputation: 12517

Objective-C -> Remove Last Element In NSDictionary

EDIT:

The Code:

//stores dictionary of questions

- (void)requestFinished:(ASIHTTPRequest *)request
{   
    NSData *responseData = [request responseData];
    NSString *json = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSDictionary *qs = [json objectFromJSONString]; 
    self.questions = qs;
    NSLog(@"%@", questions);
    [json release]; 
    [self setQuestions];
    [load fadeOut:load.view withDuration:0.7 andWait:0];
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStylePlain target:self action:@selector(start:)];          
    self.navigationItem.rightBarButtonItem = anotherButton;
}

I have the following items in an NSDictionary:

(
   {
        max = 120;
        min = 30;
        question = "Morning Bodyweight (Kg)";
        questionId = 1;
        questionNumber = 1;
        sectionId = 1;
        type = TextInput;
    },
    {
        question = "Morning Urine Colour";
        questionId = 2;
        questionNumber = 2;
        sectionId = 1;
        type = ImagePicker;
    },
    {
        max = 120;
        min = 30;
        question = "Evening Bodyweight (Kg)";
        questionId = 3;
        questionNumber = 3;
        sectionId = 1;
        type = TextInput;
    },
    {
        question = "Evening Urine Colour";
        questionId = 4;
        questionNumber = 4;
        sectionId = 1;
        type = ImagePicker;
    },
    {
        max = 90;
        min = 40;
        question = "Morning Heart Rate (BPM)";
        questionId = 5;
        questionNumber = 5;
        sectionId = 1;
        type = TextInput;
    },
    {
        question = "Time of Month (TOM)";
        questionId = 6;
        questionNumber = 6;
        sectionId = 1;
        type = Option;
    }
)

I want to remove the last element:

    {
        question = "Time of Month (TOM)";
        questionId = 6;
        questionNumber = 6;
        sectionId = 1;
        type = Option;
    }

Is there a pop() equivalent for the NSDictionary? If not how is it possible to remove the last element?

Upvotes: 0

Views: 3906

Answers (5)

ACBurk
ACBurk

Reputation: 4428

There is no order to dictionaries so there is no 'last object'

However, this might solve your problem, though it might not always remove what you are thinking the 'last object' is:

[dictionaryName removeObjectForKey:[[dictionaryName allKeys] lastObject]];

Upvotes: 5

Alex Coplan
Alex Coplan

Reputation: 13371

I think that's an array of NSDictionaries you got yourself there. In which case it's very easy to do:

NSMutableArray *mArray = [NSMutableArray arrayWithArray:array]; // if not mutable
[mArray removeLastObject];

Upvotes: 0

ryanrhee
ryanrhee

Reputation: 2571

Can you somehow get the element by using the key value? NSDictionaries don't have an ordering, so there's no such thing as removing the "last" element.

Upvotes: 0

PengOne
PengOne

Reputation: 48398

This looks to be (or could be made to be) an array of dictionaries. If you have these dictionaries as the objects of an NSMutableArray, then you can use – removeLastObject. Otherwise, you're SOL since even NSMutableDictionary has no such method.

Upvotes: 2

Hollance
Hollance

Reputation: 2976

There is no last element in a dictionary, as elements in a dictionary are not ordered.

Upvotes: 2

Related Questions