Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-22-2012, 08:44 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 44
Karl6669 is on a distinguished road
Default How to remove a key from an NSMutableDictionary

Hi, I am trying to create a filtering effect for a tableview but am struggling with one thing. I have a plist file containing multiple NSDictionary's (Manufacturer Names) with multiple sub-NSDictionary's (Model #'s). I am trying to remove specific keys (Model #'s) when the select a certain filter. My plist looks like this:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>2400</key>
	<dict>
		<key>Model</key>
		<string>2400</string>
		<key>Gauge</key>
		<string>24</string>
		<key>Thickness</key>
		<string>2&quot;</string>
		<key>Insulated</key>
		<string>Non-Insulated</string>
		<key>Material</key>
		<string>Steel</string>
		<key>Panel Design</key>
		<string>Ribbed</string>
		<key>Image</key>
		<string>Steel-Craft 2400.png</string>
		<key>Discontinued</key>
		<true/>
	</dict>
	<key>TD-138</key>
	<dict>
		<key>Model</key>
		<string>TD-138</string>
		<key>Gauge</key>
		<string>26</string>
		<key>Thickness</key>
		<string>1-3/8&quot;</string>
		<key>Insulated</key>
		<string>Insulated</string>
		<key>Material</key>
		<string>Steel</string>
		<key>Panel Design</key>
		<string>Ribbed</string>
		<key>Image</key>
		<string>Steel-Craft TD-138</string>
	</dict>
	<key>TD-134</key>
	<dict>
		<key>Model</key>
		<string>TD-134</string>
		<key>Gauge</key>
		<string>26</string>
		<key>Thickness</key>
		<string>1-3/4&quot;</string>
		<key>Insulated</key>
		<string>Insulated</string>
		<key>Material</key>
		<string>Steel</string>
		<key>Panel Design</key>
		<string>Ribbed</string>
		<key>Image</key>
		<string>Steel-Craft TD-134</string>
	</dict>
	<key>SA-5000</key>
	<dict>
		<key>Model</key>
		<string>SA-5000</string>
		<key>Gauge</key>
		<string>.071&quot;</string>
		<key>Thickness</key>
		<string>1-3/4&quot;</string>
		<key>Insulated</key>
		<string>Non-Insulated</string>
		<key>Material</key>
		<string>Aluminum</string>
		<key>Panel Design</key>
		<string>Full View</string>
		<key>Image</key>
		<string>No Image Available.jpg</string>
		<key>Discontinued</key>
		<true/>
	</dict>
	<key>SA-6000</key>
	<dict>
		<key>Model</key>
		<string>SA-6000</string>
		<key>Gauge</key>
		<string>.071&quot;</string>
		<key>Thickness</key>
		<string>1-3/4&quot;</string>
		<key>Insulated</key>
		<string>Non-Insulated</string>
		<key>Material</key>
		<string>Aluminum</string>
		<key>Panel Design</key>
		<string>Polycarbonate</string>
		<key>Image</key>
		<string>Steel-Craft SA-6000.png</string>
	</dict>
	<key>SA-7000</key>
	<dict>
		<key>Model</key>
		<string>SA-7000</string>
		<key>Gauge</key>
		<string>.071&quot;</string>
		<key>Thickness</key>
		<string>1-3/4&quot;</string>
		<key>Insulated</key>
		<string>Non-Insulated</string>
		<key>Material</key>
		<string>Aluminum</string>
		<key>Panel Design</key>
		<string>Full View</string>
		<key>Image</key>
		<string>Steel-Craft SA-7000.png</string>
	</dict>
</dict>
</plist>
I have tried removing a model # by using:
Code:
[self.displayDoorDictionary removeObjectForKey:@"2400"];
I can however remove an entire manufacturer which is one of the primary keys, but I can't figure out how to remove a sub key (model #). Can anyone please help me.
Karl6669 is offline   Reply With Quote
Old 01-22-2012, 10:05 PM   #2 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 44
Karl6669 is on a distinguished road
Default

OK, I managed to figure this one out. Here was the answer for me:
Code:
                        NSString *deleteKey = [model valueForKey:@"Model"];
                        [[[displayDoorDictionary allValues] objectAtIndex:x] removeObjectForKey:deleteKey];
Karl6669 is offline   Reply With Quote
Old 01-22-2012, 10:09 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Karl6669 View Post
Hi, I am trying to create a filtering effect for a tableview but am struggling with one thing. I have a plist file containing multiple NSDictionary's (Manufacturer Names) with multiple sub-NSDictionary's (Model #'s). I am trying to remove specific keys (Model #'s) when the select a certain filter. My plist looks like this:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>2400</key>
	<dict>
		<key>Model</key>
		<string>2400</string>
		<key>Gauge</key>
		<string>24</string>
		<key>Thickness</key>
		<string>2&quot;</string>
		<key>Insulated</key>
		<string>Non-Insulated</string>
		<key>Material</key>
		<string>Steel</string>
		<key>Panel Design</key>
		<string>Ribbed</string>
		<key>Image</key>
		<string>Steel-Craft 2400.png</string>
		<key>Discontinued</key>
		<true/>
	</dict>
	<key>TD-138</key>
	<dict>
		<key>Model</key>
		<string>TD-138</string>
		<key>Gauge</key>
		<string>26</string>
		<key>Thickness</key>
		<string>1-3/8&quot;</string>
		<key>Insulated</key>
		<string>Insulated</string>
		<key>Material</key>
		<string>Steel</string>
		<key>Panel Design</key>
		<string>Ribbed</string>
		<key>Image</key>
		<string>Steel-Craft TD-138</string>
	</dict>
	<key>TD-134</key>
	<dict>
		<key>Model</key>
		<string>TD-134</string>
		<key>Gauge</key>
		<string>26</string>
		<key>Thickness</key>
		<string>1-3/4&quot;</string>
		<key>Insulated</key>
		<string>Insulated</string>
		<key>Material</key>
		<string>Steel</string>
		<key>Panel Design</key>
		<string>Ribbed</string>
		<key>Image</key>
		<string>Steel-Craft TD-134</string>
	</dict>
	<key>SA-5000</key>
	<dict>
		<key>Model</key>
		<string>SA-5000</string>
		<key>Gauge</key>
		<string>.071&quot;</string>
		<key>Thickness</key>
		<string>1-3/4&quot;</string>
		<key>Insulated</key>
		<string>Non-Insulated</string>
		<key>Material</key>
		<string>Aluminum</string>
		<key>Panel Design</key>
		<string>Full View</string>
		<key>Image</key>
		<string>No Image Available.jpg</string>
		<key>Discontinued</key>
		<true/>
	</dict>
	<key>SA-6000</key>
	<dict>
		<key>Model</key>
		<string>SA-6000</string>
		<key>Gauge</key>
		<string>.071&quot;</string>
		<key>Thickness</key>
		<string>1-3/4&quot;</string>
		<key>Insulated</key>
		<string>Non-Insulated</string>
		<key>Material</key>
		<string>Aluminum</string>
		<key>Panel Design</key>
		<string>Polycarbonate</string>
		<key>Image</key>
		<string>Steel-Craft SA-6000.png</string>
	</dict>
	<key>SA-7000</key>
	<dict>
		<key>Model</key>
		<string>SA-7000</string>
		<key>Gauge</key>
		<string>.071&quot;</string>
		<key>Thickness</key>
		<string>1-3/4&quot;</string>
		<key>Insulated</key>
		<string>Non-Insulated</string>
		<key>Material</key>
		<string>Aluminum</string>
		<key>Panel Design</key>
		<string>Full View</string>
		<key>Image</key>
		<string>Steel-Craft SA-7000.png</string>
	</dict>
</dict>
</plist>
I have tried removing a model # by using:
Code:
[self.displayDoorDictionary removeObjectForKey:@"2400"];
I can however remove an entire manufacturer which is one of the primary keys, but I can't figure out how to remove a sub key (model #). Can anyone please help me.

If you read this structure from a plist, the container objects inside will not be mutable, even if they were when you saved the plist.

I've seen people write a mutableDeepCopy method that takes an immutable container object and recursively replaces all the objects inside that have mutable equivalents into mutable versions using mutableCopy.

Once you have a mutable dictionary of mutable dictionaries, you need to write code that fetches the dictionary from which you want to delete a key, and then delete the key from the child dictionary:

the2400Dict = [self.displayDoorDictionary objectForKey: @"2400"];
[the2400Dict removeObjectForKey: @"Model"];
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-23-2012, 09:39 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 44
Karl6669 is on a distinguished road
Default

Thanks Duncan, I don't FULLY understand everything you wrote there, but, the method I wrote and posted just prior to your response is currently working for me. Am I asking for trouble by leaving it that way?
Karl6669 is offline   Reply With Quote
Old 01-23-2012, 09:45 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 44
Karl6669 is on a distinguished road
Default

PS: I have checked out a few of your apps, tutorials, etc... You have helped me on numerous occasions with just the right amount of info to get me back on track without doing it for me. I have found your advice always helpful, THANK YOU.
Karl6669 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 387
10 members and 377 guests
7twenty7, Atatator, FrankWeller, glenn_sayers, iphonedevshani, MAMN84, mraalex, QuantumDoja, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,122
Posts: 402,907
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:47 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0