不知道为什么我有“EXC Bad Access”错误

发布于 2024-09-27 20:35:09 字数 2179 浏览 3 评论 0原文

我已经用 Java 和 .Net 编程了一段时间,但从未真正使用过 C 或 Objective C。我仍在尝试理解一些概念。我正在编写一个简单的程序,只是为了看看如何制作一组结构。我相信我是对的。我很难弄清楚如何访问子类并将值存储到我创建的子类中。

我猜我因为使用​​ 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;
}

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", 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最冷一天 2024-10-04 20:35:09

您无法将 scanf() 转换为 NSString。您需要扫描到常规 C 字符串(确保为其分配内存),然后您可以使用 stringWithUTF8String: 或其他类似方法从中构造 NSString线。

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.

梦里兽 2024-10-04 20:35:09

不要猜测:在调试器下运行应用程序,当它崩溃时,检查回溯。您还可以查看 ~/Library/Logs/DiagnosticReports/foo.crash 中的回溯。

您想做什么,从文件中逐行读取数据?使用 text = [NSString stringWithContentsOfFile:path] 然后在所有换行符上拆分 text 会容易得多:

NSCharacterSet *newlines = [NSCharacterSet newlineCharacterSet];
NSArray *lines = [text componentsSeparatedByCharactersInSet:newlines];

然后您可以循环并获取玩家名称:

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];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文