Objective CアプリケーションをSwift 3にアップデートしています。前のテーブルから渡されたオブジェクトに関連付けられた入力データを保存するにはどうすればよいですか? Objective Cのアプリが正常に動作が、私は同等の構文はスウィフト3.Swiftのコアデータにデータを保存
のObjective Cのコードのためになるものをうまくすることはできませんされていますスウィフト3コードがある
- (void)insertNewObject:(NSString*) strRain;
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
Rainfall *altRainfall = (Rainfall*)[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
altRainfall.gauge = strRain;
altRainfall.primaryGauge = self.detailItem;
altRainfall.dateRecorded = todaysDate;
altRainfall.sortDate = [NSDate date];
NSError *error = nil;
if (![context save:&error])
{
NSDictionary *dict = [error userInfo];
NSString *errMsg = [dict valueForKey:@"errMessage"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" message:errMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[self.managedObjectContext deleteObject:altRainfall];
}}
:
func save(strRain: String) {
let entity = NSEntityDescription.entity(forEntityName: "Rainfall", in: coreDataStack.context)!
let newRainfall = NSManagedObject(entity: entity, insertInto: coreDataStack.context)
newRainfall.setValue(strRain, forKeyPath: "gauge")
newRainfall.setValue(todaysDate, forKeyPath: "dateRecorded")
newRainfall.setValue(sortDateItem, forKeyPath: "sortDate")
print(strRain)
print(todaysDate)
print(sortDateItem)
do {
try coreDataStack.context.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
altRainfall.primaryGauge = self.detailItemをSwiftに置き換えるにはどうすればいいですか?
なぜ 'newRainFall.someProperty = someValue'行のすべてを' newRainFall.setValue(someValue、forKeyPath: "someKey") 'に変換していますか? – rmaddy
私はnewRainfall.gauge = strRainなどを試して、エラーを受けてforKeyPathメソッドに切り替えました。 – user1698875