简体   繁体   English

Objective-C -> 删除 NSDictionary 中的最后一个元素

[英]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:我在 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? NSDictionary 是否有等效的 pop()? 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 .如果您将这些字典作为NSMutableArray的对象,那么您可以使用– removeLastObject Otherwise, you're SOL since even NSMutableDictionary has no such method.否则,您就是 SOL,因为即使NSMutableDictionary也没有这种方法。

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. NSDictionaries 没有排序,因此没有删除“最后一个”元素这样的事情。

I think that's an array of NSDictionaries you got yourself there.我认为这是您自己获得的一系列 NSDictionaries。 In which case it's very easy to do:在这种情况下,很容易做到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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