【问题标题】:Gaining access to an NSMutableArray访问 NSMutableArray
【发布时间】:2011-07-11 23:11:13
【问题描述】:

我是数组和对象的新手。我有一个类 HowToPlay.h(当然也有一个 .m)他们定义了两个数组

NSMutableArray *nibs;
NSMutableArray *unusedNibs;
@property (nonatomic, retain) NSMutableArray *nibs;
@property (nonatomic, retain) NSMutableArray *unusedNibs;

然后我们跳入.m, 我为数组写了所有的东西,

nibs = [[NSMutableArray alloc]initWithObjects:@"Question 2", @"Question 3", nil];
self.unusedNibs = nibs;
[nibs release];

然后我还有另一个名为 Question 1 的类,我需要能够在该类中使用这个数组,并且能够更改它,但将更改保留在 HowToPlay.m 文件中。

这就是为什么,基本上这个数组会加载随机的 NIB 文件,然后在它们被使用时从数组中删除它们。

在问题 1.m 中,我是如何使用数组的

random = arc4random() % [self.unusedNibs count];
NSString *nibName = [self.unusedNibs objectAtIndex:random];
[self.unusedNibs removeObjectAtIndex:random];

if (nibName == @"Question 3") {
    Question_3 *Q3 = [[Question_3 alloc] initWithNibName:@"Question 3" bundle:nil];

    Q3.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:Q3 animated:YES];
    [Q3 release];
}

if (nibName == @"Question 2") {
    Question_2 *Q2 = [[Question_2 alloc] initWithNibName:@"Question 2" bundle:nil];

    Q2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:Q2 animated:YES];
    [Q2 release];
}

这一切似乎工作正常,但我遇到的问题是数组中的对象似乎没有被删除,即使这条线运行

[self.unusedNibs removeObjectAtIndex:random];

我试过了

[HowToPlay.unusedNibs removeObjectAtIndex:random];

我收到一条错误消息,提示 expected '.' before '.' token

在我看来,我有权读取数组,但不能更改它。有什么办法可以解决这个问题,所以我可以改变数组?谢谢

更新:

这是整个 HowToPlay.h 文件的内容:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

int score;
int usedQ1;
int usedQ2;
int usedQ3;
NSMutableArray *nibs;
NSMutableArray *unusedNibs;

@interface HowToPlay : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {


UIPickerView *selectType;
NSMutableArray *selectArray;

AVAudioPlayer *audioPlayer;

UIActivityIndicatorView *progress;
UIButton *worldButton;
UIButton *politicsButton;
UIButton *starButton;

}
@property (nonatomic, retain) NSMutableArray *nibs;
@property (nonatomic, retain) NSMutableArray *unusedNibs;

@property (nonatomic, retain) IBOutlet UIButton *worldButton;
@property (nonatomic, retain) IBOutlet UIButton *politicsButton;
@property (nonatomic, retain) IBOutlet UIButton *starButton;

@property (nonatomic, retain) IBOutlet UIPickerView *selectType;
@property (nonatomic) int usedQ1;
@property (nonatomic) int usedQ2;
@property (nonatomic) int usedQ3;

@property (readwrite) int score;






-(IBAction)World:(id)sender;
- (IBAction)Politics:(id)sender;
-(IBAction)Stars:(id)sender;




@end

#import "MainViewController.h"
#import "Question 1.h"
#import "Question 2.h"
#import "Question 3.h"

我会在之后导入,否则我会出错

我还在 HowToPlay 的 ViewDidLoad 部分设置了数组,这是个坏主意吗?

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor]; 

    NSURL *click = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/click.wav",         [[NSBundle mainBundle] resourcePath]]];
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:click error:nil];
    audioPlayer.numberOfLoops = 1;




    progress = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    progress.frame = CGRectMake(0.0, 0.0, 30.0, 30.0);
    progress.center = self.view.center;
    [self.view addSubview: progress];

    usedQ1 = 0;
    usedQ2 = 0;
    usedQ3 = 0;


    selectArray = [[NSMutableArray alloc]init];
    [selectArray addObject:@"World"];
    [selectArray addObject:@"Politics"];
    [selectArray addObject:@"Stars"];
    score = 0;

    nibs = [[NSMutableArray alloc]initWithObjects:@"Question 2", @"Question 3", nil];
    self.unusedNibs = nibs;
    [nibs release];
}

【问题讨论】:

  • 您是否尝试在删除项目前后打印出count
  • 仅供参考(我不知道这是否是问题所在),如果您使用nibName == @"Question 3",那将比较指针,而不是字符串内容。您应该使用 isEqualToString: 来比较字符串。此外,在从阵列中获取 nibName 之后,在将其从阵列中删除之前,您应该保留它,否则它将被释放。
  • 实际上,这段代码中发生了一堆奇怪的事情。您可能想看看您如何使用属性并查看有关所有权的 Obj-C 内存管理文档。目前,您将一个数组分配给“nibs”ivar 和未使用的Nibs property,但随后您将其释放,尽管 nibs 的属性指定其 ivar 中的任何内容都应保留.另请查看您尚未接受答案的问题。
  • 好的,改成'isEqualToString',把删除移到动作的最后,还是不行;(

标签: objective-c arrays ios nib


【解决方案1】:

如何检查对象是否真的被删除了?请发布更多代码,因为从 usedNibs 中删除对象的方式似乎很好。按照这段代码:

[self.unusedNibs removeObjectAtIndex:random];

还有这段代码

[HowToPlay.unusedNibs removeObjectAtIndex:random];

您收到此错误的原因是您尝试使用类名“HowToPlay”而不是其实例“self”来访问未使用的Nibs。

【讨论】:

  • 你如何将 usedNibs 传递给你的 Question 1 类(因为你可以访问一些 usedNibs 那里的属性)?
  • 问题 1 是 HowToPlay 的子类
  • 那么请解释一下如何以及在何处检查此数组中的项目数。
  • 那么在问题 1 中,在 ContinueAction 中,它选择一个随机数,该随机数位于数组 random = arc4random() % [self.unusedNibs count]; 中的对象数量内,然后根据它的名称创建字符串 ` NSString *nibName = [self.unusedNibs objectAtIndex:random];` 然后它继续检查匹配,然后删除它从数组中选择的任何对象` [self.unusedNibs removeObjectAtIndex:random];`
  • 是的,我可以看到,你已经写了:) 我的问题是:当你删除一些项目时,你如何检查项目的数量。你有没有尝试过这样的事情: NSLog(@"Count: %d", [self.unusedNibs count]); [self.unusedNibs removeObjectAtIndex:whicheverIndexYouWish]; NSLog(@"Count: %d", [self.unusedNibs count]);
【解决方案2】:

有几件事:首先,要在 HowToPlay 中调用诸如 usedNibs 之类的属性,看起来您是在调用类而不是实例。您需要创建一个 HowToPlay 对象并将其分配给 Question1 对象中的一个属性,以便一个对象可以调用该问题。 Question1 不应该为未使用的Nibs 调用“self”,因为它不拥有它。

根据您的操作,将计算下一个随机控制器的函数放在 HowToPlay 类中可能更有意义。这样,您的问题视图控制器可以只要求它返回下一个控制器,而不必在每个问题控制器中复制该代码。

【讨论】:

  • 因此,如果我将更改 NIB 操作放在 HowToPlay 类中,它们会是从另一个类中使用它的一种方式吗?谢谢
  • 绝对。创建一个对象并将其分配给一个属性,您可以访问它的方法。
  • 你会怎么做呢? :) 谢谢,你不需要使用我的任何变量和东西,只是一个通用的例子。谢谢!
  • [myHowToPlayObject nextController];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
相关资源
最近更新 更多