Я новичок в ObC и у меня проблема, которую я просто не могу исправить. Могут быть и другие проблемы, но главная проблема заключается в следующем:
- Запуск приложения
- Нажмите кнопку = загрузить новый вид
- В новом viewDidLoad я вызываю другой объект / функцию и отправляю NSMutableArray
- Обработка данных и отправка обратно NSMutableArray
- Сбой приложения, см. Комментарий где. Чаще всего, когда я возвращаюсь назад и снова, но иногда в первый раз
Поскольку я новичок в этом, я думаю, что я много ошибаюсь, но кто-нибудь может взглянуть на код и дать мне совет. Я предположил бы, что у меня есть проблема с выпуском чего-либо.
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@" ");
NSLog(@"viewDidLoad ");
NSLog(@" ");
NSLog(@">>Processing prepareGame<<");
NSMutableArray *propArray1 = [[NSMutableArray alloc] initWithObjects:@"9999", nil]; //Init with dummy numbers
AccessPropertiesFile *readMyProperties = [AccessPropertiesFile new]; //Init function call to read file
NSLog(@"Prepare to call readProperties");
propArray1 = [readMyProperties readPropertiesFile:propArray1];
NSLog(@"Back from readProperties:error after this");
/*
for (NSString *element in propArray1) {
NSLog(@"Elements in prop2Array; %@", element);
}
*/
[readMyProperties release];
[propArray1 release];
}
-(NSMutableArray *)readPropertiesFile:(NSMutableArray *)readDataArray {
NSLog(@"Processing readProperties");
// For error information
NSError *error;
//Prepare File Manager
NSString *filePath = [self dataFilePath];
NSFileManager *fileMgr;
fileMgr = [NSFileManager defaultManager];
NSArray *propertiesArray = [NSArray alloc]; //Alloc array
//Check from what module the call is coming from to ecide what to do
if ([fileMgr fileExistsAtPath: filePath] == NO) {
NSLog (@"File not found");
//File does not exists, this is the first time the game starts
//Set up default parameters
NSString *fileString =@"0\n30\n30\n10\n1\n1\n1\n2\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n";
// Write default parameters to file
[fileString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
propertiesArray = [fileString componentsSeparatedByString:@"\n"]; // each line, adjust character for line endings
}
else { //File exists
NSLog (@"File exists");
NSString *fileString = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding error:nil]; // reads file into memory as an NSString
propertiesArray = [fileString componentsSeparatedByString:@"\n"]; // each line, adjust character for line endings
}
//Clean readDataArray
[readDataArray removeAllObjects];
//Populate return array
for (NSString *element in propertiesArray) {
//NSLog(@"Elements in propertiesArray; %@", element);
[readDataArray addObject:element];
}
NSLog(@"readDataArray: %@", readDataArray);
[propertiesArray release];
[readDataArray autorelease];
NSLog(@"returning from readProperties");
return readDataArray;
}
@end