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 03-16-2011, 08:46 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 83
djp_phillips is on a distinguished road
Default Copying an Array

Hello all,

I've started a project and I have an indexed UITableView displaying a list of data in sections (alphabet), I also have 3 buttons in the view and touching them allows the user to filter the data.

I have one button called "Browse all", another one called "Active" and the last one called "Inactive".

What I've put together is having an original array of data. And when I want to filter that, I copy that array into a temporary array and loop through each letter (section), and each entry per section (array of my data objects).

If according to the filter I should not be displaying a particular object in the list, I use NSMutableArray's removeObjectAtIndex method to remove that object.

All works fine but when I select Browse all after filtering the data previously, I realise that in fact when I used removeObjectAtIndex I actually made some objects release from memory.

Could anyone point out why in fact the items of my NSMutableDictionary reference the original objects and not a copy of these items ?

Code:
filteredArray = nil;

NSMutableArray* tmpArray = [fullArray mutableCopy];
if(filterActive){
    for (int u = 0; u < [tmpArray count]; u++) {
         NSMutableDictionary* d = [tmpArray objectAtIndex:u];
         NSMutableArray* dataObjects = [d objectForKey:@"Rows"];
         for(int i = 0; i < [dataObjects count]; i++){
              myObjectType* f = [dataObjects objectAtIndex:i];
              if(f.badge == 1){
                  [dataObjects removeObjectAtIndex:i];
                  i--;
               }
         }

         if([dataObjects count] == 0){
               [tmpArray removeObjectAtIndex:u];
               u--;
         }
     }
}

filteredArray = [[NSArray alloc] initWithArray:tmpArray];

// I use filteredArray as the data source for the UITableView

Last edited by djp_phillips; 03-16-2011 at 08:47 AM. Reason: forgot to ask the question... !
djp_phillips is offline   Reply With Quote
Old 03-16-2011, 08:52 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

The concept you are looking for is called "deep copy". What you are getting is a "shallow" copy.

If that is your entire method, you have a couple of leaks.

It depends on the situation, but I prefer to loop through the primary source, and then add matches to the separate array.

Edit:

You might be interested in initWithArray:copyItems:
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.

Last edited by BrianSlick; 03-16-2011 at 08:55 AM.
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
array, copy, remove

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: 356
11 members and 345 guests
akphyo, apatsufas, BinHex, blueorb, fredidf, iAppDeveloper, iGamesDev, Kirkout, MarkC, mottdog, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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