ios - 将 NSMutableArray 添加到项目并在另一个类中访问相同的 NSMutableArray

标签 ios objective-c iphone xcode nsmutablearray

我在尝试将字符串的 NSMutableArray 共享给另一个类时遇到了一些问题。我有一个 tableView,其中填充了 Strings,我想将其添加到 NSMutableArray。然后在另一个 ViewController

中使用相同的 NSMutableArray

我用 NSMutableArray 的子类创建了一个类

.h

#import <Foundation/Foundation.h>

@interface HYServicesMArray : NSMutableArray

@property (nonatomic, weak)NSMutableArray * arrServicesUserChoice;


@end

.m

#import "HYServicesMArray.h"

@implementation HYServicesMArray

@dynamic arrServicesUserChoice;


@end

我正在尝试从 tableView didSelectRowAtIndexPath:

向此 NSMutableArray 添加元素

.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

为什么我无法向 arrServicesUserChoice 添加元素。我卡住了,请帮忙!先感谢您!

最佳答案

您声明了一个可变数组,您用来添加的代码返回一个数组,但它不添加任何内容。您必须添加以下对象:

     [arrAvaialbleServicesList addObject:cell.textlabel.text];

在此之后,您的可变数组将具有添加的数据

或者你可以声明另一个数组,然后按你的方式做"

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

在此之后,新数组将包含添加数据的数组。

关于ios - 将 NSMutableArray 添加到项目并在另一个类中访问相同的 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32770649/

相关文章:

iphone从AppDelegate设置RightBarButtonItem

iphone - 设置 UIWebView scrollView delegate 导致错误

objective-c - 内存泄漏和僵尸有什么区别?

objective-c - 重新初始化对象

ios - iOS 9.1 启动屏幕上的黑色方 block

ios - facebook 登录错误,react-native ios expo

ios - AVSpeech合成器: AVSpeechSynthesisIPANotationAttribute seems like does not support Chinese

ios - ionic Xcode 无法打开文件(目标应用程序)

iphone - 在 ARC 上使用@property

iphone - 以编程方式对 UILabel touchUpInside 执行操作?