路过的NSMutableArray其他类返回空数组 [英] Passing NSMutableArray to other class returns empty array

查看:93
本文介绍了路过的NSMutableArray其他类返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过有关这其他的答案,并在第一次我有一个(空)的错误,但现在是固定的。现在,每当我登录我的NSMutableArray,我在SelectTeacherTableViewController.h创造并传递给SendMessageViewController类,它创建了一个数组,但它是空的。

I've read other answers about this and at first I had a (null) error, but that is now fixed. Now, whenever I log my NSMutableArray, which I created in "SelectTeacherTableViewController.h" and passed to the SendMessageViewController class, it creates an array, but it is empty.

下面是从我的SelectTeacherTableViewController.h code

Here is the code from my SelectTeacherTableViewController.h

#import <UIKit/UIKit.h>

@interface SelectTeacherTableViewController :
UITableViewController
{
    NSMutableArray *recipients;
}

@property (strong, nonatomic) NSArray *teachers;
@property (strong, nonatomic) NSMutableArray *recipients;

@end

下面是从我SelectTeacherTableViewController,在那里我综合收件人,并通过一个方法将对象添加到它们code。 (code缩短)

Here is the code from my SelectTeacherTableViewController, where I synthesize the recipients and add objects to them through a method. (Code shortened)

#import "ParseStarterProjectAppDelegate.h"
#import "SelectTeacherTableViewController.h"
#import <Parse/Parse.h>

@interface SelectTeacherTableViewController ()

@end

@implementation SelectTeacherTableViewController
@synthesize recipients;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Message";
    self.recipients = [[NSMutableArray alloc] init];
    [PFUser logInWithUsername:@"awesome" password:@"password"];
    NSString *getCurrentUserSchoolKey = [[PFUser currentUser] objectForKey:@"schoolKey"];
    NSString *currentUserSchoolKey = [NSString stringWithFormat:@"%@", getCurrentUserSchoolKey];
    PFQuery *queryForTeachers = [PFUser query];
    [queryForTeachers orderByAscending: @"username"];
    [queryForTeachers whereKey: @"role" equalTo:@"Teacher"];
    [queryForTeachers whereKey:@"schoolKey" equalTo:currentUserSchoolKey];
    [queryForTeachers findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"%@ %@", error, [error userInfo]);
    }
    else {
        self.teachers = objects;
        NSLog(currentUserSchoolKey);
        [self.tableView reloadData];
    }
}];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.recipients = [[NSMutableArray alloc] init];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    PFUser *user = [self.teachers objectAtIndex:indexPath.row];

if (cell.accessoryType == UITableViewCellAccessoryNone) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [recipients addObject:user.objectId];
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
    [recipients removeObject:user.objectId];
}

NSLog(@"%@", recipients);
}

当我在这里登录收件人,它们包含了所有正确的价值观。然而,这是我的SendMessageViewController,我通过从SelectTeacherTableViewController数组:

When I log the recipients here, they do contain all of the correct values. However, this is my SendMessageViewController where I pass the array from the SelectTeacherTableViewController:

SendMessageViewController.m(code缩短):

SendMessageViewController.m (Code Shortened):

#import "SendMessageViewController.h"
#import <Parse/Parse.h>
#import <MobileCoreServices/UTCoreTypes.h>
#import "SelectTeacherTableViewController.h"

@interface SendMessageViewController ()
@end

@implementation SendMessageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    SelectTeacherTableViewController *recipientList = [[SelectTeacherTableViewController alloc] init];
    NSMutableArray *recipients = [[NSMutableArray alloc] initWithArray:recipientList.recipients];
    NSLog(@"%@", recipientList);
}

这是在这儿,当我登录的收件人,数组为空。

It's here where when I log the recipients, the array is empty.

我读过有关这一点,并遇到了一些解决方案,称有关创建傻瓜文件,或者使用AppDelegate中存储的全局变量,但是没有这些解决方案的一些事情似乎为我工作。我有,当我Segue公司的数据丢失的感觉。是否有任何地方我在code出了错?我有点新的Objective-C和这个问题已经窃听我小时。谢谢你。

I've read about it a little and encountered some solutions which said some things about creating simpleton files, or using the AppDelegate to store global variables in, but none of those solutions seemed to work for me. I have a feeling that the data is being lost when I segue. Is there anywhere where I have gone wrong in my code? I'm a little new to Objective-C and this problem has been bugging me for hours. Thanks.

如果您需要查看更多code,就问我。谢谢你。

If you need to see any more code, just ask me. Thanks.

推荐答案

本: SelectTeacherTableViewController * recipientList = [[SelectTeacherTableViewController的alloc]初始化]; 创建一个新对象。任何你添加到收件人里面的一些其他 SelectTeacherTableViewController 是无关紧要的数组。

This: SelectTeacherTableViewController *recipientList = [[SelectTeacherTableViewController alloc] init]; creates a new object. Anything you added to a recipients array inside some other SelectTeacherTableViewController is irrelevant.

如果您需要使用您添加​​到原始对象的信息,您必须通过引用该对象...不是创建一个新的。

If you need to use the information that you've added to the original object, you must pass a reference to that object…not create a new one.

这篇关于路过的NSMutableArray其他类返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆