简体   繁体   中英

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?

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]];

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

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.

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.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM