【发布时间】: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