我需要在嘗試設置值之前檢查NSEntityDescription
項是否存在。我有一個來自JSON的數據字典,不想嘗試設置我的對象中不存在的鍵。檢查是否存在NSEntityDescription項
Appointment *appointmentObject = [NSEntityDescription insertNewObjectForEntityForName:@"Appointments" inManagedObjectContext:[[DataManager sharedInstance] managedObjectContext]];
for (id key in dict) {
// Check if the key exists here before setting the value so we don't error out.
[appointmentObject setValue:[dict objectForKey:key] forKey:key];
}
你說得很好!由於我依靠JSON響應來提供密鑰,因此它們很可能具有響應普通選擇器的密鑰。 – Bot 2012-03-13 20:52:15
爲什麼有人不喜歡這個? – Bot 2012-03-13 21:56:18