我在Java和.Net中編寫了一段時間,但從未真正使用C或Objective C.我仍試圖理解一些概念。我正在研究一個簡單的程序,以瞭解如何創建一系列結構。我相信我是對的。我很難弄清楚如何訪問子類並將值存儲到我創建的子類中。不知道爲什麼我有「EXC Bad Access」錯誤
我猜我得到的錯誤,因爲我使用scanf。任何人都可以提供幫助嗎?
這是我到目前爲止。
#import <Foundation/Foundation.h>
//Player Prototype: Stores name and wins so far. It can also print out the name and wins
@interface Player : NSObject
{
NSString *name; //Player name
NSInteger wins; //Player wins
NSInteger losses; //Player losses
NSInteger bp; //extra value for anything I might need in the future.
}
@property (retain, nonatomic) NSString *name;
@property NSInteger wins;
@property NSInteger losses;
@property NSInteger bp;
@end
//Next part
@implementation Player
@synthesize name;
@synthesize wins;
@synthesize losses;
@synthesize bp;
@end
//Brackets
@interface Bracket : NSObject
{
NSMutableArray *playerarray;
Player *addplayer;
}
@property (retain, nonatomic) NSMutableArray *playerarray;//array of players
@property (retain, nonatomic) Player *addplayer;//player and data
-(void) SetUp;
@end
//Starting Bracket, working with only 8. Later moving up to 32
@implementation Bracket
@synthesize playerarray;
@synthesize addplayer;
-(void) SetUp;//sets up the array
{
int i;//counting fun!
playerarray = [[NSMutableArray alloc] init];//initialize a bracket
for(i = 0; i < 8; i++)//To add the players
{
Player *addplayerx = [Player new];//New instance of Player
NSString *p;//Not sure if I need two of them.
NSString *tempname = @"bye";
NSLog(@"Player %d Name:", i);
scanf("%s",&p);
tempname = p;
NSLog(@"%s", tempname);
addplayerx.name = p;
NSLog(@"%s", addplayerx.name);
addplayerx.wins = 0;
addplayerx.losses = 0;
addplayerx.bp = 0;
[playerarray addObject: addplayerx];
[addplayerx release];
[p release];
}
}
@end
//End function
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Bracket *starting = [Bracket new];
[starting SetUp];
[pool drain];
return 0;
}
謝謝!這爲我解決了這個問題。我仍然習慣它,但是由於你的建議,我已經取得了很大的進步。 – Duckzero 2010-10-20 00:19:38