0

I am having some trouble trying to share a NSMutableArray of Strings to another class. I have a tableView that is populated with Strings that I would like to add to a NSMutableArray. Then use that SAME NSMutableArray in another ViewController

I have created a class with the subclass of NSMutableArray

.h

#import <Foundation/Foundation.h>

@interface HYServicesMArray : NSMutableArray

@property (nonatomic, weak)NSMutableArray * arrServicesUserChoice;


@end

.m

#import "HYServicesMArray.h"

@implementation HYServicesMArray

@dynamic arrServicesUserChoice;


@end

I am trying to add elements to this NSMutableArray from a tableView didSelectRowAtIndexPath:

.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    NSLog(@"You have selected: %@",cell.textLabel.text);

    // add cell.textLabel.text to arrServicesUserChoice 
    // Tried the code below but causes my app to crash  
      arrServicesUserChoice = [[NSMutableArray alloc]init];
      [arrServicesUserChoice addObject:cell.textLabel.text];
}

enter image description here

How ever I am unable to add elements to the arrServicesUserChoice. I am stuck please help! Thank you in advance!

Jay Dave
  • 297
  • 4
  • 11

6 Answers6

1

YOu declared a mutable array, and the code you used to add returns an array, but it dont add anyting. you have to add the objects like:

     [arrAvaialbleServicesList addObject:cell.textlabel.text];

After this, your mutable array will have the added data

or you can declare another array, and do it your way"

 NSArray *test=[arrAvaialbleServicesList arrayByAddingObject:cell.textLabel.text];

after this, the new array will have the array with added data.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
1

I think you should change

@dynamic arrServicesUserChoice;

To

@synthesize arrServicesUserChoice;
nynohu
  • 1,628
  • 12
  • 12
0

The original initialization to MutableArray does nothing really and does not specify arrAvalabelServiceList as Mutable - arrayByAddingObject creates a brand new instance which is NSArray, NOT NSMutableArray

  arrAvaialbleServicesList = [[NSMutableArray alloc]init];
  [arrAvaialbleServicesList arrayByAddingObject:cell.textLabel.text];

You are however not assigning this new instance to anything, if you did:

arrAvaialbleServicesList = [arrAvaialbleServicesList arrayByAddingObject:cell.textLabel.text];

You would at least get a handle to the new NSArray instance.

What you should probably to is to create your list when you setup the table:

arrAvaialbleServicesList = [[NSMutableArray alloc]init];

The on your didSelect you would just add the text to the list:

NSString *selected = [NSString stringWithString:cell.textLabel.text];

[arrAvaialbleServicesList addObject:selected];

or something close to this then you would add something to your list whenever you select a row in the table.

  • Still running into the issue of `[arrAvaialbleServicesList addObject:selected];` does not have access to `addObject:` – Jay Dave Sep 24 '15 at 21:46
0

What you try to create is a kind of public static var correct? i think the better way to do that is creating an Class that can handle your MutableArray object. Doing her as a singleton class is a good way to guarantee that you will not create another instance of your object and lost your MutableArray. Here is a sample:

HYServicesMArray.h

#import <Foundation/Foundation.h>

@interface HYServicesMArray : NSObject

@property (nonatomic, strong) NSMutableArray *arrServicesUserChoice;

+ (HYServicesMArray *) instance;

@end

HYServicesMArray.m

#import "HYServicesMArray.h"

@implementation HYServicesMArray

+ (HYServicesMArray *)instance
{
    static HYServicesMArray *_ServicesMArray = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _ServicesMArray = [[self alloc] init];
    });
    return _ServicesMArray;
}


-(id)init {
    self.arrServicesUserChoice = [[NSMutableArray alloc] init];
    return self;
}

@end

And the application on your didSelectRowAtIndexPath method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    NSLog(@"You have selected: %@",cell.textLabel.text);

    [[HYServicesMArray instance].arrServicesUserChoice addObject:cell.textLabel.text];
}
Ostanik
  • 104
  • 1
  • 5
0

You should make it @synthesized and dereference the property as self.arrServicesUserChoice. (You can leave the self away only in Swift.)

This should compile:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    NSLog(@"You have selected: %@",cell.textLabel.text);

    self.arrServicesUserChoice = [[NSMutableArray alloc] init];
    [self.arrServicesUserChoice addObject:cell.textLabel.text];
}
ctietze
  • 2,805
  • 25
  • 46
0

You do not need to declare it @dynamic, You need to @synthesize it. If you don't want to @synthesize it then use self. You can check the difference between here between these two here.

@synthesize vs @dynamic, what are the differences? If you want to use this same array in another class then you should declare a property in that class and pass this class's array to another class using segue.

Community
  • 1
  • 1
shivam
  • 136
  • 13