Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

NSmutableArray Error Help

lmpeejaylmpeejay Posts: 28Registered Users
Hi there..
I am attempting to modif code from Jeff Lamarche's book for iphone development, Specifically the code to search and remove objects from an array, basically I am building a system to reduce the NSMutableArray by typing in a search term, however I keep getting the following error.

[__NSArrayI removeObjectsInArray:]: unrecognized selector sent to instance 0x6a30cd0

Below is the code. Can you please let me know what I am doing wrong? Thanks

P.J.
-----
- (void)handleSearchForTerm:(NSString *)searchTerm{

NSMutableArray *sectionsToRemove;
NSMutableArray *lclArray;
NSMutableArray *toRemove;
NSString *key;
NSString *altSearchTerm;
NSString *name;

altSearchTerm = @\"the \";
altSearchTerm = [altSearchTerm stringByAppendingString:searchTerm];

sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];

for (key in self.listEditData) {

lclArray = (NSMutableArray *)listData;
toRemove = [[NSMutableArray alloc] init];

for (name in lclArray) {

if ([name rangeOfString:searchTerm
options:NSCaseInsensitiveSearch].location != 0
&& [name rangeOfString:altSearchTerm
options:NSCaseInsensitiveSearch].location != 0)
[toRemove addObject:name];
}

if ([lclArray count] == [toRemove count]){
[sectionsToRemove addObject:key];
}

[lclArray removeObjectsInArray:toRemove];
}
[self.listEditData removeObjectsInArray:sectionsToRemove];
[myTableView reloadData];
}
Post edited by lmpeejay on
PJ Dahill

pdahill_at_gmail.com



Follow my romp through the world of finding a contract job in this economy on Twitter at "HiTechJobSeeker"

Replies

  • baja_yubaja_yu Posts: 6,166Super Moderators, Registered Users
    The error means that you are calling a method which does not exist for that object. More specifically, you're trying to remove objects from an immutable array.

    What is self.listEditData declared as and where is it created or set?
  • lmpeejaylmpeejay Posts: 28Registered Users
    It is defined in the .h file as an NSMutableArray. The thins is, I commented that line, and even the locally declared nsmutable array had the problem.

    Any ideas?

    PJ
    PJ Dahill

    pdahill_at_gmail.com



    Follow my romp through the world of finding a contract job in this economy on Twitter at "HiTechJobSeeker"
  • baja_yubaja_yu Posts: 6,166Super Moderators, Registered Users
    Where are you setting the value of that variable?
  • lmpeejaylmpeejay Posts: 28Registered Users
    baja_yu;434571 said:
    Where are you setting the value of that variable?
    It is set in the viewDidLoad with an array, that is being displayed in the table successfully.

    I used the list of dwarves lamarche added for testing.
    PJ Dahill

    pdahill_at_gmail.com



    Follow my romp through the world of finding a contract job in this economy on Twitter at "HiTechJobSeeker"
  • baja_yubaja_yu Posts: 6,166Super Moderators, Registered Users
    Post that code, or at least look at it. Your array is an NSArray for some reason.
  • lmpeejaylmpeejay Posts: 28Registered Users
    baja_yu;434583 said:
    Post that code, or at least look at it. Your array is an NSArray for some reason.
    I think you might be right, I am going to rewrite the code, I have had some limited success. I will be back with my solution.

    P.J.
    PJ Dahill

    pdahill_at_gmail.com



    Follow my romp through the world of finding a contract job in this economy on Twitter at "HiTechJobSeeker"
  • baja_yubaja_yu Posts: 6,166Super Moderators, Registered Users
    Just a quick note, if you're ever copying arrays keep in mind you have a 'copy' and a 'mutableCopy' method which work like this:

    copy of an NSArray is an NSArray
    mutableCopy of NSArray is an NSMutableArray

    copy of an NSMutableArray is NSArray (!)
    mutableCopy of an NSMutableArray is an NSMutableArray

    One of those can trick you, so keep it that mind.
  • lmpeejaylmpeejay Posts: 28Registered Users
    lmpeejay;434593 said:
    I think you might be right, I am going to rewrite the code, I have had some limited success. I will be back with my solution.

    P.J.
    Here is the new code...it works with no fuss

    - (void)handleSearchForTerm:(NSString *)searchTerm{

    // change toRemove to reducedArray

    NSMutableArray *lclArray;
    NSMutableArray *reducedArray;
    NSString *altSearchTerm;
    NSString *name;

    altSearchTerm = @\"the \";
    altSearchTerm = [altSearchTerm stringByAppendingString:searchTerm];

    [self resetSearch];

    lclArray = (NSMutableArray *)listData;
    reducedArray = [[NSMutableArray alloc] init];

    for (name in lclArray) {

    if ([name rangeOfString:searchTerm
    options:NSCaseInsensitiveSearch].location == 0
    || [name rangeOfString:altSearchTerm
    options:NSCaseInsensitiveSearch].location == 0)
    [reducedArray addObject:name];
    }


    self.listEditData = reducedArray;
    [myTableView reloadData];
    }
    PJ Dahill

    pdahill_at_gmail.com



    Follow my romp through the world of finding a contract job in this economy on Twitter at "HiTechJobSeeker"
Sign In or Register to comment.