NSMutableArray type is changed to NSArray type

Discussion in 'iOS Programming' started by tranvutuan, Feb 5, 2012.

  1. tranvutuan, Feb 5, 2012
    Last edited: Feb 5, 2012

    tranvutuan
    Expand Collapse
    macrumors member

    Joined:
    Dec 19, 2011
    #1
    This is a structure of my Objects

    Code:
    myList(NSObject): items(NSMutableArray)
                                    |
                                    |__item(NSObject)
                                    |         |
                                    |         |__ itemKey + itemDescription
                                    |
                                    |__item(NSOject)
                                              |
                                              |__ itemKey + itemDescription
    
    In myList.h

    Code:
    @interface myList : NSObject <XMLData,NSCoding>
    @property (nonatomic, copy) NSMutableArray *items;
    
    In AppDelegate.m
    Code:
    @implementation AppDelegate
    
    @synthesize window = _window;
    @synthesize navigationController = _navigationController;
    @synthesize myList, ;
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        NSLog(@"APPDELEGATE IS LOADED AT THE BEGINNING");
        myList    =   [[MyList alloc] init];
        NSLog(@"myList items is class of %@",NSStringFromClass([myList.items class]));
    
    - (void) check {
            NSLog(@"mylist items is class of %@",NSStringFromClass([wishlist.items class]));
    }
    
    in testClass.m
    Code:
    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
        [myList.items removeObjectAtIndex:indexPath.row];
        [myListTableView reloadData];
        NSLog(@"BEFORE check::::::::myList items is class of %@",NSStringFromClass([appDelegate.wishlist.items class]));
        [appDelegate check];
        
    }
    After running the program,this is the result from the debuger

    Code:
    APPDELEGATE IS LOADED AT THE BEGINNING
    class of items is __NSArrayM
    BEFORE check::::::::myList items is class of __NSArrayM
    mylist items is class of __NSArrayI
    I dont know why in check method at AppDelegate.m ,myList.items is a class of __NSArrayI.
    I think it have to be __NSArrayM (NSMutableArray).
    Have anyone had ideas about this. Please help me if you were experiencing before.
     
  2. KnightWRX
    Expand Collapse
    macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #2
    @property (nonatomic, copy) ... :confused:

    Try retain just for kicks.
     
  3. tranvutuan
    Expand Collapse
    thread starter macrumors member

    Joined:
    Dec 19, 2011
    #3
    Can you give me an explanation for that please..
     
  4. KnightWRX
    Expand Collapse
    macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #4
    Did it work ? From the documentation :

    https://developer.apple.com/library...ceptual/ObjectiveC/Chapters/ocProperties.html
    NSMutableArray inherits its NSCopying support from NSArray, and thus probably doesn't reimplement the copy method, which thus would obviously return an NSArray type instead of NSMutableArray :

    https://developer.apple.com/library...NSMutableArray_Class/Reference/Reference.html
    That's speculation on my part, haven't tried it myself.
     
  5. tranvutuan
    Expand Collapse
    thread starter macrumors member

    Joined:
    Dec 19, 2011
    #5
    I did made a change and it is working now. I am trying to convinced my self like below :
    Code:
    in [B][I]commitEdittingStyle[/I][/B] after doing an assignment
    [COLOR="Blue"]appDelegate.wishlist.items  =   wishList.items;[/COLOR]
    wishList.items on the left will be released. Moreover,"NSMutabeArray inherits from NSCopying from NSArray". Therefore, at the end appDelegate.wishlist.items is NSArray
    Please correct me if I have just made a mistake... Thanks
     
  6. KnightWRX
    Expand Collapse
    macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #6
    Like I said, speculation on my part, I have never had to do something of the sort. Try to experiment with it, changing the setter attributes and see what it does.
     
  7. dejo
    Expand Collapse
    Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    copy returns an immutable copy. If you want a mutable copy use, ...wait for it..., mutableCopy.
     
  8. KnightWRX
    Expand Collapse
    macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #8
    How do you do that through properties though ? In the documentation, setter attributes cannot be mutableCopy.
     
  9. dejo, Feb 6, 2012
    Last edited: Feb 6, 2012

    dejo
    Expand Collapse
    Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Hmm, good question. I'm not sure (currently).

    EDIT:

    Seems the answer is to either use retain or use your own setter, like suggested here.

    The real concern seems to be justifying the need for a copy in the first place.
     

Share This Page