我在尝试将字符串的 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];
}
为什么我无法向 arrServicesUserChoice
添加元素。我卡住了,请帮忙!先感谢您!
最佳答案
您声明了一个可变数组,您用来添加的代码返回一个数组,但它不添加任何内容。您必须添加以下对象:
[arrAvaialbleServicesList addObject:cell.textlabel.text];
在此之后,您的可变数组将具有添加的数据
或者你可以声明另一个数组,然后按你的方式做"
NSArray *test=[arrAvaialbleServicesList arrayByAddingObject:cell.textLabel.text];
在此之后,新数组将包含添加数据的数组。
关于ios - 将 NSMutableArray 添加到项目并在另一个类中访问相同的 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32770649/