2

我目前通過創建一個NSMutableDictionary,然後使用NSKeyedArchiver保存30個整數到一個文件。Xcode - 保存大量整數到文件的最簡化方法

@interface: 
NSInteger highScoreE; 
NSInteger highScoreM; 
NSInteger highScoreH; 
NSInteger highScoreI; 

NSInteger highScoreComE; 
NSInteger highScoreComM; 
NSInteger highScoreComH; 
NSInteger highScoreComI; 

NSInteger totalGamesWonE; 
NSInteger totalGamesWonM; 
NSInteger totalGamesWonH; 
NSInteger totalGamesWonI; 

NSInteger totalGamesLostE; 
NSInteger totalGamesLostM; 
NSInteger totalGamesLostH; 
NSInteger totalGamesLostI; 

NSInteger totalPointsForE; 
NSInteger totalPointsForM; 
NSInteger totalPointsForH; 
NSInteger totalPointsForI; 

NSInteger totalPointsAgainstE; 
NSInteger totalPointsAgainstM; 
NSInteger totalPointsAgainstH; 
NSInteger totalPointsAgainstI; 

NSInteger highScore; 
NSInteger highScoreCom; 
NSInteger totalGames; 
NSInteger totalGamesWon; 
NSInteger totalGamesLost; 
NSInteger totalPointsFor; 
NSInteger totalPointsAgainst; 

@property(nonatomic) NSInteger highScoreE; 
@property(nonatomic) NSInteger highScoreM; 
@property(nonatomic) NSInteger highScoreH; 
@property(nonatomic) NSInteger highScoreI; 

@property(nonatomic) NSInteger highScoreComE; 
@property(nonatomic) NSInteger highScoreComM; 
@property(nonatomic) NSInteger highScoreComH; 
@property(nonatomic) NSInteger highScoreComI; 

@property(nonatomic) NSInteger totalGamesWonE; 
@property(nonatomic) NSInteger totalGamesWonM; 
@property(nonatomic) NSInteger totalGamesWonH; 
@property(nonatomic) NSInteger totalGamesWonI; 

@property(nonatomic) NSInteger totalGamesLostE; 
@property(nonatomic) NSInteger totalGamesLostM; 
@property(nonatomic) NSInteger totalGamesLostH; 
@property(nonatomic) NSInteger totalGamesLostI; 

@property(nonatomic) NSInteger totalPointsForE; 
@property(nonatomic) NSInteger totalPointsForM; 
@property(nonatomic) NSInteger totalPointsForH; 
@property(nonatomic) NSInteger totalPointsForI; 

@property(nonatomic) NSInteger totalPointsAgainstE; 
@property(nonatomic) NSInteger totalPointsAgainstM; 
@property(nonatomic) NSInteger totalPointsAgainstH; 
@property(nonatomic) NSInteger totalPointsAgainstI; 

@property(nonatomic) NSInteger highScore; 
@property(nonatomic) NSInteger highScoreCom; 
@property(nonatomic) NSInteger totalGames; 
@property(nonatomic) NSInteger totalGamesWon; 
@property(nonatomic) NSInteger totalGamesLost; 
@property(nonatomic) NSInteger totalPointsFor; 
@property(nonatomic) NSInteger totalPointsAgainst; 


@implementation; 

NSString *nssTotalPointsFor = [NSString stringWithFormat:@"%i", totalPointsFor]; 
NSString *nssTotalPointsForE = [NSString stringWithFormat:@"%i", totalPointsForE]; 
NSString *nssTotalPointsForM = [NSString stringWithFormat:@"%i", totalPointsForM]; 
NSString *nssTotalPointsForH = [NSString stringWithFormat:@"%i", totalPointsForH]; 
NSString *nssTotalPointsForI = [NSString stringWithFormat:@"%i", totalPointsForI]; 
//create dictionary 
NSMutableDictionary* myDict = [[NSMutableDictionary alloc] init]; 

//add things to dictionary (game stats) 
[myDict setObject:nssTotalPointsFor forKey:@"totalPointsFor"]; 
[myDict setObject:nssTotalPointsForE forKey:@"totalPointsForE"]; 
[myDict setObject:nssTotalPointsForM forKey:@"totalPointsForM"]; 
[myDict setObject:nssTotalPointsForH forKey:@"totalPointsForH"]; 
[myDict setObject:nssTotalPointsForI forKey:@"totalPointsForI"]; 

//get the path 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentPath = [paths objectAtIndex:0]; 
NSString *path = [documentPath stringByAppendingPathComponent:@"stats.save"]; 

// save to file 
[NSKeyedArchiver archiveRootObject:myDict toFile:path]; 

然後我對其他整數做類似的操作。

然後我要讀這些字符串回整數:

//get the path 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentPath = [paths objectAtIndex:0]; 
NSString *path = [documentPath stringByAppendingPathComponent:@"stats.save"]; 

//create dictionary 
NSMutableDictionary* myDict = [[NSMutableDictionary alloc] init]; 

//read from file 
myDict = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; 
NSString *nssPlayerHighScore = [myDict objectForKey:@"playerHighScore"]; 
highScore = [nssPlayerHighScore intValue]; 

NSString *nssComputerHighScore = [myDict objectForKey:@"computerHighScore"]; 
highScoreCom = [nssComputerHighScore intValue]; 

NSString *nssTotalPointsFor = [myDict objectForKey:@"totalPointsFor"]; 
totalPointsFor = [nssTotalPointsFor intValue]; 

NSString *nssTotalPointsAgainst = [myDict objectForKey:@"totalPointsAgainst"]; 
totalPointsAgainst = [nssTotalPointsAgainst intValue]; 

NSString *nssTotalGames = [myDict objectForKey:@"totalGames"]; 
totalGames = [nssTotalGames intValue]; 

NSString *nssTotalGamesWon = [myDict objectForKey:@"totalGamesWon"]; 
totalGamesWon = [nssTotalGamesWon intValue]; 

NSString *nssTotalGamesLost = [myDict objectForKey:@"totalGamesLost"]; 
totalGamesLost = [nssTotalGamesLost intValue]; 

NSString *nssPlayerHighScoreE = [myDict objectForKey:@"playerHighScoreE"]; 
highScoreE = [nssPlayerHighScoreE intValue]; 

NSString *nssComputerHighScoreE = [myDict objectForKey:@"computerHighScoreE"]; 
highScoreComE = [nssComputerHighScoreE intValue]; 

,然後等爲其他整數爲好。

我覺得這種方式非常煩瑣和煩人,但我不知道任何其他方式來保存整數。

什麼是最簡單的挽救東西的方法?

我不知道是否NSKeyedArchiver是問題,或者我應該讓字符串包含多個整數(eg.NSString * nssTotalPointsFor = [NSString stringWithFormat:@「%i,%i,...」, totalPointsFor,totalPointsAgainst,...] ;.

但後來我將如何改變數字串中回整數?

還是我使用一個數組不知何故?

給我你的我想法和我很多建議。

Thx很多我對編程知之甚少。

+1

只是一個想法,如果要修剪下來總的行數在你的代碼,你可以像這樣聲明你的整數。 「NSInteger一,二,三,四,五;」 –

+0

好的,謝謝我不知道! – burkel

+0

沒問題,很高興幫助! –

回答

3

將這些數字存儲到「NSString」對象中並使用該大塊代碼進行存檔確實看起來確實非常麻煩。

爲什麼不這樣做"NSNumber" objects,它有內置的歸檔功能?您可以將所有這些「NSNumber」對象放入數組中(例如「highScoreENumber = [[NSNumber alloc] initWithInt: highScoreE];」)並將其寫入文件,然後在隨後的啓動時加載文件。

編輯:

我不能口你對你的代碼,但檢查了這一點:

@interface: 
{ 
    NSNumber * highScore; 
    NSNumber * highScoreCom; 
} 

@property(nonatomic, retain) NSNumber * highScore; 
@property(nonatomic, retain) NSNumber * highScoreCom; 

@implementation; 

//create dictionary 
NSMutableDictionary* myDict = [[NSMutableDictionary alloc] init]; 

if(myDict) 
{ 
    //add things to dictionary (game stats) 
    [myDict setObject:highScore forKey:@"highScore"]; 
    [myDict setObject:highScoreCom forKey:@"highScoreCom"]; 

    //get the path 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentPath = [paths objectAtIndex:0]; 
    NSString *path = [documentPath stringByAppendingPathComponent:@"stats.save"]; 

    BOOL successfulWrite = [myDict writeToFile: path atomically: YES]; 
    if(successfulWrite == NO) 
    { 
     NSLog(@"could not write my file to path %@", path); 
    } 
    // if *not* using ARC, don't forget to release 
    [myDict release]; 
} 
+0

對不起,我不認爲我知道該怎麼做。非常讚賞。 – burkel

+0

我該如何編碼?對不起,很痛苦我只是編程方面的新手。 – burkel

+1

讓我知道我的代碼示例是否可以幫助您。 –

相關問題