ios - 访问可变数组时遇到问题

标签 ios objective-c arrays sorting object

我在尝试索引用户名的数组中重新分配数据时遇到问题。我能够将我的原始数组分成单独的对象,但无法将值发送到我稍后需要引用的新数组。我的 self.userNamesArray = userNames; 行中 userNames 的值和计数是正确的。但是在那之后,当我登录 self.userNamesArray 时,我得到了 (null)。任何提示,因为我不完全确定我欢呼!

.h

@property (nonatomic, copy) NSMutableArray *userNamesArray;

.m

- (void)viewWillAppear:(BOOL)animated {

self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];

PFQuery *query = [self.friendsRelation query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
    else {
        self.friends = objects;

        NSArray *users = [self.friends valueForKey:@"username"];
        NSLog(@"username:%@", users);
        //Create an array of name wrappers and pass to the root view controller.
        NSMutableArray *userNames = [[NSMutableArray alloc] initWithCapacity:[self.friends count]];

        for (NSString *user in users) {

        componentsSeparatedByCharactersInSet:charSet];
            NSArray *nameComponents = [user componentsSeparatedByString:@" "];

            UserNameWrapper *userNameWrapper = [[UserNameWrapper alloc]    initWithUserName:nil nameComponents:nameComponents];

            [userNames addObject:userNameWrapper];
        }

        self.userNamesArray = userNames;

        NSLog(@"userNamesArray:%@",self.userNamesArray);

        [self.tableView reloadData];
    }

这是我需要引用 self.userNamesArray 的代码,它再次编译为 nil。

- (void)setUserNamesArray:(NSMutableArray *)newDataArray {

if (newDataArray != self.userNamesArray) {
    self.userNamesArray = [newDataArray mutableCopy];
    if (self.userNamesArray == nil) {
        self.sectionsArray = nil;
        NSLog(@"user names empty");
    }
    else {
        [self configureSections];
    }
}

最佳答案

将可变数组的属性方法更改为以下:-

    @property (nonatomic, retain) 
    NSMutableArray *userNamesArray;

关于ios - 访问可变数组时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19614082/

相关文章:

ios - UITableView didSelectRowAtIndexPath 从未调用过

ios - iOS 7 Matchismo作业1斯坦福的问题

ios - 使用 nspredicate 将 for 循环中的 NSArray 与给定输入进行比较

arrays - 是否可以在 F# 中定义列表的长度?

python - 使用 interp1d 时,数组末尾的 NaN 会逐渐出现

ios - 如何通过使用特定值比较数组来获得预期结果

objective-c - 将 .plist 文件与 iCloud 同步

ios - 如何根据当前区域设置 NSDateFormatter 自动进行 12/24 小时格式化

php - 如何在 Yii2 应用程序的多选下拉列表中显示选定的值?

objective-c - 将一个数组分成四个元素的单独数组加上提醒