1

我似乎得到一個我無法理解的錯誤。希望這不會太混亂和長期,但因爲我似乎無法在我的搜索中找到任何答案,所以我認爲這可能是必要的。在我的計劃,我有幾個實體:CoreData/RegEx錯誤:無法從對象創建正則表達式

ItemSold 發票 客戶

Customer的可以有很多InvoicesInvoices可以有很多ItemSolds

在我的界面生成器中,我有三個表格。一張表代表客戶,另一張代表發票,最後一張代表發票中的itemsold。 因此,當您選擇一位客戶時,他們的發票將顯示在發票表中。一旦您點擊發票,該發票上的項目就會出現在最後一個表格中。

我在桌子上選擇時似乎會出錯。有趣的是,在另一種觀點中,我可以做到這一點(當我試圖根據它們與類型的關係對項目進行排序時)。但是我無法在這裏工作。這是我取來獲取應存放的物品:

if (managedObjectContext == nil) 
{ 
    managedObjectContext = [(HistoryController *)[[NSApplication sharedApplication] delegate] managedObjectContext]; 
} 
NSError *error = nil; 

// fetch all customers 
NSFetchRequest *fetchRequest2 = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity2 = [NSEntityDescription entityForName:@"Customer" 
              inManagedObjectContext:managedObjectContext]; 
[fetchRequest2 setEntity:entity2]; 
fetchedCustomers = [managedObjectContext executeFetchRequest:fetchRequest2 error:&error]; 
if (fetchedCustomers == nil) { 
    NSLog(@"ERROR"); 
} 
[fetchRequest2 release]; 
// end of customer fetch 

// fetch all invoices 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Invoice" 
              inManagedObjectContext:managedObjectContext]; 
[fetchRequest setEntity:entity]; 

NSLog(@"A"); 
if ([customerTable selectedRow] >= 0) 
{ 
    NSLog(@"B"); 

    int index = (int)[customerTable selectedRow]; 

    NSLog(@"%@", index); 
    NSString *customerString = [[fetchedCustomers objectAtIndex:(index)] valueForKey:@"CustomerNumber"]; 
    NSNumber *customerNumber = [NSNumber numberWithInt:([customerString intValue])]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"customer.CustomerNumber like %@", customerNumber]; 
    [fetchRequest setPredicate:predicate]; 
} 

fetchedInvoices = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
if (fetchedInvoices == nil) { 
    NSLog(@"ERROR"); 
} 
[fetchRequest release]; 
// end of invoice fetch 

// fetch specific invoice 
NSFetchRequest *fetchRequest3 = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity3 = [NSEntityDescription entityForName:@"ItemSold" 
              inManagedObjectContext:managedObjectContext]; 
[fetchRequest3 setEntity:entity3]; 

if ([invoicesListTable selectedRow] >= 0) 
{ 

    NSLog(@"C"); 
    int index = (int)[invoicesListTable selectedRow]; 

    NSLog(@"%i", (int)[invoicesListTable selectedRow]); 
    NSLog(@"%i", index); 
    NSString *invoiceString = [[fetchedInvoices objectAtIndex:(index)] 
           valueForKey:@"InvoiceNumber"]; 
    NSNumber *invoiceNumber = [NSNumber numberWithInt:([invoiceString intValue])]; 
    NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"invoice.InvoiceNumber like %@", invoiceNumber]; 
    [fetchRequest3 setPredicate:predicate3]; 
} 

NSLog(@"D"); 

fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest3 error:&error]; 
if (fetchedObjects == nil) 
{ 
    NSLog(@"ERROR"); 
} 
[fetchRequest3 release]; 

[invoiceTable reloadData]; 
NSLog(@"D2"); 

}

如果我執行,並點擊一排,這是我得到:

2012-02-13 16:42:35.326 InventoryProgram[7056:707] A

2012-02-13 16:42:35.327 InventoryProgram[7056:707] C

2012-02-13 16:42:35.327 InventoryProgram[7056:707] 0

2012-02-13 16:42:35.328 InventoryProgram[7056:707] 0

2012-02-13 16:42:35.328 InventoryProgram[7056:707] D

2012-02-13 16:42:35.329 InventoryProgram[7056:707] Can't create a regex expression from object 2.

我展示字母的輸出,是想說明,其中控制如下。隨着我選擇一行,輸出正確的行號(0),並且實際上使它經過了我認爲是問題的地方。輸出字母D,它通過NSPredicate區域,這正是我認爲正則表達式問題的來源。我假設它現在到達executeFetchRequest,並遇到分配給它的NSPredicate的問題。

有沒有人有任何想法/任何東西在這裏指向我在正確的方向?我無法將自己的思想包圍在問題所在之處。我真的希望這裏沒有太多的代碼,我只是想給我所有的信息。

回答

8

替換:

NSString *invoiceString = [[fetchedInvoices objectAtIndex:(index)] 
          valueForKey:@"InvoiceNumber"]; 
NSNumber *invoiceNumber = [NSNumber numberWithInt:([invoiceString intValue])]; 
NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"invoice.InvoiceNumber like %@", invoiceNumber]; 

有了:

NSString *invoiceString = [[fetchedInvoices objectAtIndex:(index)] 
           valueForKey:@"InvoiceNumber"]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"invoice.InvoiceNumber = %@", invoiceString]; 

解決了這個問題。