我有一个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?
我在这样一个方案的各种对象如何与另一个(&正确的语法)互动的想法依然朦胧,所以请原谅我在我的代码:)
请张贴多一点点的代码,以便我们能理解您的问题更好地 – Tomen 2012-01-30 14:55:33
我已经编辑我的职务与一些相关的代码 – Octave1 2012-01-30 16:35:43