我是数组和对象的新手。我有一个类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];
然后,我还有另一个名为问题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];
}
最佳答案
如何检查对象是否实际删除?请发布更多代码,因为从未使用的Nib中删除对象的方式似乎不错。按照此代码:
[self.unusedNibs removeObjectAtIndex:random];
和这段代码
[HowToPlay.unusedNibs removeObjectAtIndex:random];
出现此错误的原因是,您试图使用类名“ HowToPlay”而不是其实例“ self”来访问未使用的Nib。