I've programmed for a while in Java and .Net, but never really used C or Objective C. I'm still trying to understand a few concepts. I was working on a simple program just to see how I can make an array of structures. Which I believe I got right. I'm having a hard time figuring out how to access the subclasses and store values to the subclasses I created.
I'm guessing I'm getting the error because of my use of scanf. Can anyone offer any help?
Here's what I have so far.
#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", temp开发者_如何学Pythonname);
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;
}
You can't scanf()
into an NSString
. You need to scan into a regular C string (make sure you allocate memory for it), and then you can construct the NSString
from that using stringWithUTF8String:
, or something along those lines.
Don't guess: run the application under the debugger, and when it crashes, examine the backtrace. You can also look at the backtraces in ~/Library/Logs/DiagnosticReports/foo.crash.
What are you trying to do, read data line-by-line from a file? It would be much easier to just use text = [NSString stringWithContentsOfFile:path]
then split text
on all newline characters:
NSCharacterSet *newlines = [NSCharacterSet newlineCharacterSet];
NSArray *lines = [text componentsSeparatedByCharactersInSet:newlines];
You can then just loop across and grab the player names:
NSMutableArray *players = [NSMutableArray arrayWithCapacity:[lines count]];
NSString *whitespace = [NSCharacterSet whitespaceCharacterSet];
for (NSString *line in lines) {
NSString *name = [line stringByTrimmingCharactersInSet:whitespace];
Player *player = [[[Player alloc] init] autorelease];
player.name = name;
[players addObject:player];
}
精彩评论