2012-01-30 49 views
1

我有一个xcode项目,它有2个类 - 干&玩家,我试图确保我的代码从面向对象的角度来看是可靠的,我相信这是可以接受的编程习惯。播放器类访问我的Stem类中的信息。在程序中调用单独类的类

我想从茎中访问数组索引 - 我可以当我尝试从球员做到这一点,我不允许作为干是未申报的使用

stem.index[i] 

从我的视图控制器做到这一点。我已经尝试将Stem.h导入我的Player.m文件&,在播放器中声明stem(类似于视图控制器中的这种做法),仅用于获取错误(在'Stem'之前获得预期的说明符 - 限定符列表)。

这样做的正确方法是什么?请原谅,因为我对此比较陌生。感谢提前:)

编辑

下面是一些代码,可能会揭示一些事情的光,在我的viewController声明干&球员

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVAudioPlayer.h> 
#import "Stem.h" 
#import "Player.h" 

@interface TestApp_v1ViewController : UIViewController { 

Stem *stem; 
Player *player; 

我的Alloc &初始化我的两个对象干&播放器在viewController.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 
NSLog(@"Init Successful"); 

[self loadMOV]; 
[self setupInterface]; 

stem = [[Stem alloc] init]; 
player = [[Player alloc] init]; 

} 

然后,我移动到Stem.h我声明再次干(这样干是访问时stem.h导入到player.h球员 - 按照glogic的评论)

#import <Foundation/Foundation.h> 

@interface Stem : NSObject { 

int *index; 

int numberOfStems; 

Stem *stem; 

} 

@property(nonatomic,readwrite) int *index; 
@property(nonatomic, retain) Stem *stem; 

Player.h看起来是这样的:

// Player.h 
#import <Foundation/Foundation.h> 
#import "Stem.h" 

@interface Player : NSObject { 

NSMutableArray *player1; 

} 

@property(nonatomic,retain) NSMutableArray *player1; 

-(void) playAudio; 

@end 

最后,在播放器.m我尝试访问索引阵列

#import "Player.h" 

@implementation Player 
@synthesize player1; 


-(void)playAudio { 

NSLog(@"play audio called"); 

NSLog(@"index[0] is: %i", stem.index[0]); 

} 

@end 

我仍然被告知stem是未声明的,任何想法?

编辑#2 - 添加裸机程序

我希望这被认为是好,但我已经决定要发布我的程序(简化到最基本的要素)。我认为这可能是解决这个问题的唯一方法 - 因为很多课程都在进行。我一直试图让这对现在arrrgh小时工作...

我alloc'd &初始化我干&的球员对象在的viewController - 我认为这是去了解这一点的最好办法,但也许有一个更好的方法。

//Part (i) 
// TestApp_v1ViewController.h 

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVAudioPlayer.h> 
#import "Stem.h" 
#import "Player.h" 

@interface TestApp_v1ViewController : UIViewController { 

Stem *stem; 
Player *player; 

} 

@property(nonatomic, retain) Stem *stem; 
@property(nonatomic, retain) Player *player; 

@end 

//Part (ii) 
// TestApp_v1ViewController.m 

#import "TestApp_v1ViewController.h" 
#import <MediaPlayer/MediaPlayer.h> 

@implementation TestApp_v1ViewController 

@synthesize stem; 
@synthesize player; 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
[super viewDidLoad]; 
NSLog(@"Init Successful"); 

stem = [[Stem alloc] init]; 
[stem loadURLs]; 

player = [[Player alloc] init]; 
[player playAudio]; 

int index = stem.value; 
NSLog(@"r is: %i", index); //checking to see if I can get a value from the index array - this works fine, so 'value' can be accessed from the viewController 

} 

这里我声明的int数组&整型“值”,我想从我的球员在以后访问(这是什么原来是这个问题)

//Part (iii) 
// Stem.h 

#import <Foundation/Foundation.h> 

@interface Stem : NSObject { 

NSMutableArray *urlArray; 

int *index; 

int value; 

int numberOfStems; 

Stem *stem; 

} 

- (void)loadURLs; 
- (void)randomiseAudioForInitialPlay; 

@property(nonatomic,retain) NSMutableArray *urlArray; 
@property(nonatomic,readwrite) int *index; 
@property(nonatomic,readwrite) int numberOfStems; 
@property(nonatomic,readwrite) int value; 
@property(nonatomic, retain) Stem *stem; 

@end 

//Part (iv) 
// Stem.m 

#include <stdio.h> 
#import "Stem.h" 


@implementation Stem 

@synthesize numberOfStems; 
@synthesize urlArray; 
@synthesize index; 
@synthesize stem; 
@synthesize value; 

- (void)loadURLs 
{ 
NSLog(@"Loading URLs"); 
numberOfStems = 20; 
urlArray = [[NSMutableArray alloc] init]; 

for (int i = 1; i <= numberOfStems; i++) { 
    NSString *soundName = [NSString stringWithFormat:@"stem-%i", i]; 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"]; 
    NSURL *soundFile = [[NSURL alloc] initFileURLWithPath:soundPath]; 
    [urlArray addObject:soundFile]; 
    [soundFile release]; 

} 

[self randomiseAudioForInitialPlay]; 

} 


- (void)randomiseAudioForInitialPlay 
{ 
index = malloc(numberOfStems*sizeof(int)); 
for (int i = 0; i < numberOfStems; i++) 
{ 
    index[i] = i; 
} 

for (int i = (numberOfStems - 1); i > 0; i--) 
{ 

    int randomIndex = arc4random() % i; 
    int tmp = index[i]; 
    index[i] = index[randomIndex]; 
    index[randomIndex] = tmp; 

} 

value = self.index[10];  //this is what needs to be accessed later, from player 

NSLog(@"value at index 10 is:%i", value); 

} 

@end 

在这里,我有' Stem.h',因为玩家需要干才能返回干。值

//Part (v) 
// Player.h 

#import <Foundation/Foundation.h> 
#import "Stem.h" 

@interface Player : NSObject { 

Player *player; 

} 

@property(nonatomic, retain) Stem *stem; 

@property(nonatomic, retain) Player *player; 

-(void) playAudio; 

@end 

这就是事情出错,我的NSLog语句告诉我,值为0,即使我可以看到,它(例如)在干14。编译器也不会给出错误。

//Part (vi) 
// Player.m 

#import "Player.h" 

@implementation Player 

@synthesize player; 
@synthesize stem; 


-(void)playAudio { 

int value = stem.value; 

NSLog(@"value is:%i", value); 

} 

@end 

这是一个面向对象的项目我的第一个适当的去,所以我边干边学,有什么建议,为什么我不能在我的Player类访问stem.value?

我在这样一个方案的各种对象如何与另一个(&正确的语法)互动的想法依然朦胧,所以请原谅我在我的代码:)

+0

请张贴多一点点的代码,以便我们能理解您的问题更好地 – Tomen 2012-01-30 14:55:33

+0

我已经编辑我的职务与一些相关的代码 – Octave1 2012-01-30 16:35:43

回答

3

你在哪里声明干疯狂的n00b错误玩家内?如果你在player.h中声明它,那么你需要在player.h中导入stem.h,而不是player.m。

编辑:是的,它仍然未申明,因为你有视图控制器中声明的不是播放器。嗯,这似乎是很重要的事情。这真的取决于如何您的代码将实际工作

#import Stem.h 

@interface Player : NSObject { 

NSMutableArray *player1; 

} 

@property(nonatomic,retain) NSMutableArray *player1; 
@property(nonatomic,retain) Stem *stem; 

-(void) playAudio; 

@end 

import "Player.h" 

@implementation Player 
@synthesize player1, stem = _stem; 


-(void)playAudio { 

NSLog(@"play audio called"); 

NSLog(@"index[0] is: %i", _stem.index[0]);//stem.index[0] will be a problem here as its not a c array 

} 

@end 

,然后在乌拉圭回合控制器

player.stem = stem; 

和IM不知道为什么乌尔创建茎内的干指针类。

@interface Stem : NSObject { 

int *index; 

int numberOfStems; 
} 

@property(nonatomic,readwrite) int *index; 

,如果它的杆阵列您想要然后创建一个控制器

编辑:你有行之后

player = [[Player alloc] init]; 
[player playAudio]; 

添加

player.stem = stem; 

你有将播放器中的指针分配给您在视图控制器中创建的主干

+0

这应该是一个评论,而不是答案 – Tomen 2012-01-30 14:56:13

+0

为什么呢?我说要在player.h中导入stem.h作为我的答案。似乎苛刻的投票,但生病学会忍受它 – glogic 2012-01-30 15:11:29

+0

我其实是犯了这个错误,所以我找到了有用的答案,不幸的是它并没有完全解决我的问题... – Octave1 2012-01-30 16:49:38

2

最好的方法是在Stem类中创建一个方法,如:

value = [stem valueAtIndex:i];

这样有较低的偶合和值在Stem类持有的方式不被暴露,并可在以后如果有必要改变,而不传接电话。

+0

好吧,这比我目前的方法更好,谢谢,我仍然有问题让玩家“看到”干,虽然...我已经张贴代码上面。 – Octave1 2012-01-30 16:19:30

+0

任何机会,你可以把你的眼睛放在我的代码CocoaFu?将不胜感激:) – Octave1 2012-01-30 23:45:30

+0

晚餐后我会看看。 – zaph 2012-01-31 00:03:47

相关问题