我有一個按鈕,每當它被按下時應該增加一個,結果會在文本框中顯示一個新的字符串。我有一個核心數據管理器類,它包含一個應該檢索我的數組的方法,但每當我按下應用程序崩潰按鈕。我留下了一個錯誤,說明...UIButton從核心數據增加一個數組,從另一個類中完成
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UsefulCodes rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x6000000a3f60'"
「UsefulCodes」是我的核心數據實體的名稱,而「代號」和「codeDescription」分別來回我presetList和codeDescArray屬性。我認爲它不工作,因爲我試圖將一個核心數據對象放入一個文本字段中,但數組中的所有對象都是字符串,我如何才能將它成功傳遞到文本字段?我將在下面發佈相關代碼。
從CoreDataManager -
@implementation CoreDataManager
+(CoreDataManager*)sharedInstance {
static CoreDataManager *sharedObject;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedObject = [[CoreDataManager alloc] init];
});
return sharedObject;
}
-(void)storeListFirstTime {
//http://morsecode.scphillips.com/morse.html
NSArray* presetList = [NSArray arrayWithObjects:@"AS",
@"BCNU",
@"CL",
@"CT",
@"CUL",
@"K",
@"QSL",
@"QSL?",
@"QRX?",
@"QRV",
@"QRV?",
@"QTH",
@"QTH?",
@"R",
@"SN",
@"SOS",
@"73",
@"88",
nil];
NSArray* codeDescArray = [NSArray arrayWithObjects:@"Wait",
@"Be seeing You",
@"Going off air",
@"Start Copying",
@"See you later",
@"Over",
@"I acknowledge receipt",
@"Do you acknowledge",
@"Should I wait",
@"Ready to copy",
@"Are you ready to copy?",
@"My location is ...",
@"What is your location?",
@"Roger",
@"Understood",
@"Distress message",
@"Best regards",
@"Love and kisses",
nil];
//Saves the initial list of items
for(int i = 0; i < presetList.count; i++) {
NSManagedObjectContext *context = [self context];
UsefulCodes *codeObj = [NSEntityDescription insertNewObjectForEntityForName:@"UsefulCodes"
inManagedObjectContext:context];
codeObj.codeName = [presetList objectAtIndex:i];
codeObj.codeDescription = [codeDescArray objectAtIndex:i];
NSLog(@"CODEOBJ: %@", codeObj);
}
[self saveContext];
}
//THIS IS THE METHOD I AM CALLING FOR MY BUTTON METHOD
-(NSArray*)fetchAllRecords {
NSManagedObjectContext *context = [self context]; //this context is conected to persistant container
NSError *error = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"UsefulCodes"];
NSArray *records = [context executeFetchRequest:request
error:&error];
if(error == nil && records.count > 0) {
return [records copy];
}
else {
//Handle error by returning new array
return [NSArray new];
}
}
上面的代碼只是設置我硬編碼到我的陣列的初始值(我僅在從presetList陣列得到的值顯示在文本框感興趣)以及適應我添加到任何陣列的任何新代碼和說明。現在,我的「MainViewController」負責循環訪問我的數組。
在頭文件中我聲明瞭一個名爲num的屬性。
@property(nonatomic)int num;
這是執行文件中的方法。
- (void)getQuickMorseCode {
NSArray *array = [[CoreDataManager sharedInstance] fetchAllRecords];
if(_num == 0 || _num > 0) {
if(_num >= array.count){
_num = 0;
}
//GETTING THE SPECIFIC INDEX OF THE ARRAY, THIS IS WHERE THE PROBLEM LIES
self.morseTextfield.text = array[_num];
[self convertInput:self.morseTextfield.text];
[self buttonAppear];
_num++;
}
}
我的目標是要顯示在我CoreDataManager文件presetList數組中的字符串,但我無法得到那個傳遞到我的文本字段。歡迎任何對我的問題的見解。
謝謝,這工作就像一個魅力! – jumpman007