2016-03-30 10 views
0
ben 2 masa yaşıyorum

için biri için yüklemi nasıl eklenir, şimdi ürünü belirli bir seçenek olan almak istiyorum.çekirdek veri Ürün ve birden fazla seçenek olabilir Seçenek</p> <p>Bir ürün birçok ilişki Çekirdek Veri iOS

Ben yüklemi yazmak için nasıl hiçbir fikrim yok nerede nedeniyle yakalanmamış istisna 'NSInvalidArgumentException', akıl uygulamayı sonlandırılması, hata ile bu

enter image description here

Onun çöker (*** için fıkra: çoğa tuşu' Bunun için kod

-(Product*)getProdcutFromDB{ 

AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate]; 
NSManagedObjectContext *context = appDelegate.managedObjectContext; 

NSError *error; 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *orderEntity = [NSEntityDescription 
            entityForName:@"Product" inManagedObjectContext:context]; 
[fetchRequest setEntity:orderEntity]; 
NSPredicate* predicate = [NSPredicate predicateWithFormat:@" options.optionID == '14'"]; 
[fetchRequest setPredicate:predicate]; 
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 

Product* orderEntityForProdcut=nil; 
if (fetchedObjects.count>0) { 
    orderEntityForProdcut=(Product*)[fetchedObjects objectAtIndex:0]; 
    NSLog(@"%@",orderEntityForProdcut.productName); 
    NSLog(@"%@",orderEntityForProdcut.options); 

    return orderEntityForProdcut; 
} 

return nil; 
} 

cevap

1

altında çalıştıklarında burada izin verilmiyor' ) kullanarak, SUBQUERY() kullanmak zorunda.

(ayrı dize kolay SO okumak için yapmaktır.) Kenara

NSString *predicateFormat = @"SUBQUERY(options, $o, $o.optionID == %@)[email protected] > 0"; // The "> 0" condition could also be "== 1", depending on your need. 

NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, optID]; 

: Bu dizelerinde gömmemeli özellik adlarına en iyisidir.

// These would be defined somewhere that could be imported as needed. 
NSString *const kOptionsKey = @"options"; 
NSString *const kOptionIDKey = @"optionID"; 

NSString *predicateFormat = @"SUBQUERY(%K, $o, $o.%K == %@)[email protected] > 0"; 

NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, 
             kOptionsKey, kOptionsIDKey, optID]; 

fikri mülkiyet adı değiştirilirse, tek sürekli güncellenmesi gereken tüm olmasıdır: Bu yukarıdaki yazmak daha iyidir. Bu ayrıca, bir dize içinde özellik adında bir yazım hatası oluşmasını da engeller.

cevap için, Benim sorunum Şimdi kod burada Ürüne sahip optionId = "" ve optionId listesini almak mümkün bu kodla sonra Çoklu ile doğrulama'yı VE koşulu

kullanıyor güncelleme çözüldüğünü @Avi

+0

sayesinde, büyük çalışma, ayrıca ben eklemek nasıl önermek eder misiniz "Ben gerekir ürününü gerekiyorsa demek burada" vE 12,13 ve 14 –

+0

numaralı seçeneklere sahip olmanız yeterli Bunların ac ayrı ayrı ve sonra 'NSCompoundPredicate' kullanarak birleştirir. – Avi

+0

Eğer ben yeniyim örnek esas almamız :( –

0

Teşekkür = "" ve benzeri .....

-(Product*)getProdcutFromDBNew{ 

AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate]; 
NSManagedObjectContext *context = appDelegate.managedObjectContext; 

NSError *error; 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *orderEntity = [NSEntityDescription 
            entityForName:@"Product" inManagedObjectContext:context]; 
[fetchRequest setEntity:orderEntity]; 

// These would be defined somewhere that could be imported as needed. 
NSString *const kOptionsKey = @"options"; 
NSString *const kOptionIDKey = @"optionID"; 

NSString *predicateFormat = @"SUBQUERY(%K, $o, $o.%K == %@)[email protected] > 0"; 


NSPredicate *predicate1 = [NSPredicate predicateWithFormat:predicateFormat, 
          kOptionsKey, kOptionIDKey, @"12"]; 
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:predicateFormat, 
          kOptionsKey, kOptionIDKey, @"14"]; 


// Add the predicates to the NSArray 

NSArray *subPredicates = [[NSArray alloc] initWithObjects:predicate1, predicate2, nil]; 

NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates]; 



// NSPredicate* predicate = [NSPredicate predicateWithFormat:@" ANY options.name == 'Size'"]; 
[fetchRequest setPredicate:compoundPredicate]; 

NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 

Product* orderEntityForProdcut=nil; 
if (fetchedObjects.count>0) { 
    orderEntityForProdcut=(Product*)[fetchedObjects objectAtIndex:0]; 
    NSLog(@"%@",orderEntityForProdcut.productName); 
    NSLog(@"%@",orderEntityForProdcut.options); 

    return orderEntityForProdcut; 
} 

return nil; 

}

İlgili konular