不知道为什么我有“EXC Bad Access”错误
我已经用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法将
scanf()
转换为NSString
。您需要扫描到常规 C 字符串(确保为其分配内存),然后您可以使用stringWithUTF8String:
或其他类似方法从中构造NSString
线。You can't
scanf()
into anNSString
. You need to scan into a regular C string (make sure you allocate memory for it), and then you can construct theNSString
from that usingstringWithUTF8String:
, or something along those lines.不要猜测:在调试器下运行应用程序,当它崩溃时,检查回溯。您还可以查看 ~/Library/Logs/DiagnosticReports/foo.crash 中的回溯。
您想做什么,从文件中逐行读取数据?使用
text = [NSString stringWithContentsOfFile:path]
然后在所有换行符上拆分text
会容易得多:然后您可以循环并获取玩家名称:
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 splittext
on all newline characters:You can then just loop across and grab the player names: