2010-10-19 84 views
2

我在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; 
} 

回答

4

您不能將scanf()轉換爲NSString。你需要掃描到一個普通的C字符串(確保你爲它分配內存),然後你可以使用stringWithUTF8String:或其他東西來構建NSString

+0

謝謝!這爲我解決了這個問題。我仍然習慣它,但是由於你的建議,我已經取得了很大的進步。 – Duckzero 2010-10-20 00:19:38

2

不要猜測:在調試器下運行應用程序,當它崩潰時,檢查回溯。您還可以查看〜/ 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]; 
} 
+0

我打算在後面添加閱讀文件的功能。我目前只是試圖添加命令行中的值來實踐。我現在正在嘗試打印播放器對象數組,並獲取存儲位置的地址。這並不能幫助我太多,因爲我想知道名字等等。 :( – Duckzero 2010-10-20 00:22:09

+0

沒關係,我只是想出瞭如何擺脫地址,並取而代之的價值。 – Duckzero 2010-10-20 00:28:35