NSMutableDictionary: mutating method sent to immutable object

前端 未结 6 2036
栀梦
栀梦 2021-01-11 10:24

The following code is returning an exception with the following error message \"mutating method sent to immutable object\" when attempting to removeObjectForKey



        
相关标签:
6条回答
  • 2021-01-11 10:45

    if you have on error NSMutableDictionary: mutating method sent to immutable object in Swift, make this step:

    This is because you have assigned a NSUserDefault to NSMutableArray, when you take something NSUserDefault it returns you a NSArray not a NSMutableArray, so in this case you have to use a NSMutableArray Auxiliary .

    see for Swift :

    var Products:NSMutableArray = NSMutableArray()
    
    
    override func viewDidAppear(animated: Bool) {
    
        if let Produtos = NSUserDefaults.standardUserDefaults().valueForKey("Produtos") {
            Products = Produtos as! NSMutableArray
        }
    }
    
    func InsertProducts(productCode:String){
         //COPY Products Atual for auxMutable
         var auxMutable = Products.mutableCopy()
    
            //Add object in auxMutable
            auxMutable.addObjectsFromArray([productCode])
    
            //in line back data to Array Products and make cast to NSMutableArray
            Products = auxMutable as! NSMutableArray
    
            //Refresh Data of NSUserDefaults
            NSUserDefaults.standardUserDefaults().setObject(Products, forKey: "Produtos")
    }
    
    
    @IBAction func Bt_New_Product(sender: AnyObject) {
         var ProductName:String = TXT_NameProduct.text
    
         InsertProducts(ProductName)
    }
    

    This work for me!!!

    0 讨论(0)
  • 2021-01-11 10:58

    i found same issue and found solution hope it will help some one.

    arrayOfferId = defaults.objectForKey("offerId")?.mutableCopy() as! NSMutableArray
    

    NSUserDefaults returns immutable objects, even if you put in mutable ones. You must call -mutableCopy on the returned value to get a mutable collection. so when you get value from NSUserDefault use mutableCopy()

    0 讨论(0)
  • 2021-01-11 10:58

    [NSUserDefaults dictionaryForKey] returns an immutable dictionary (NSDictionary) and you cannot force it to be mutable by casting it to NSMutableDictionary.

    Instead you must create the mutable dictionary using mutableCopy, overwrite the element and then re-assigning the dictionary back into NSUserDefaults:

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *storedIpDictionary = [[userDefaults dictionaryForKey:@"dictDeviceIp"] mutableCopy];
    
    NSString *key = self.currentDeviceNameText.text;
    NSString *ipAddressTemp = [storedIpDictionary objectForKey:key];
    
    // Don't need this line
    //[storedIpDictionary removeObjectForKey:key];
    
    storedIpDictionary[key] = ipAddressTemp;
    [userDefaults setObject:storedIpDictionary
                     forKey:@"dictDeviceIp"];
    
    0 讨论(0)
  • 2021-01-11 10:59

    You cant just cast an NSDictionary to NSMutableDictinary thats not at all how casting works.

    to remove a key from NSUserDefualts call removeObjectForKey on the NSUserDefaults instance itself.

    if you really do want a dictionary for some other reason, then you must make a mutableCopy from the dictionary obtained by dictionaryForKey.

    0 讨论(0)
  • 2021-01-11 11:03

    NSUserDefaults returns immutable objects, even if you put in mutable ones. You must call -mutableCopy on the returned value to get a mutable collection.

    0 讨论(0)
  • 2021-01-11 11:09

    This is the code that eventually worked, I used some of the details provided from others above, but none had it completely explained.

    - (void)cleanDictionary
    {
        NSMutableDictionary * storedIpDictionary = [[[NSUserDefaults standardUserDefaults] objectForKey: @"dictDeviceIp"] mutableCopy];
    
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"dictDeviceIp"];
    
        NSString *oldKey = self.currentDeviceNameText.text;
        NSString *newKey = self.deviceNameChangeText.text;
        NSString *ipAddressTemp = [storedIpDictionary objectForKey:oldKey];
    
        // Make some change to the structure
        [storedIpDictionary removeObjectForKey:oldKey];  // Remove object
        storedIpDictionary[newKey] = ipAddressTemp;      // Add object with new key
    
        // Add it the whole thing back into NSUserDefaults
        [[NSUserDefaults standardUserDefaults] setObject:storedIpDictionary forKey:@"dictDeviceIp"];
    
        // Synchronize to ensure it's saved
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    0 讨论(0)
提交回复
热议问题