1

I am trying to learn Swift and decided to use "Swift for Dummies" published in 2015. The sample code used in the book is for XCode 6.X but I am using Xcode 7.2.1.

In chapter 4, there is a code for insertNewObject in MasterViewController.swift but it is not compatible for Xcode 7.X. The offending line is "newManagedObject.setValue.longitude = self.lastLocation.coordinate.longitude" and produces the error "Ambiguous reference to member setValue"

func insertNewObject(sender: AnyObject) {
    let context = self.fetchedResultsController.managedObjectContext
    let entity = self.fetchedResultsController.fetchRequest.entity!
    let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName("Event", inManagedObjectContext: context) as! Event

    // If appropriate, configure the new managed object.
    // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
    newManagedObject.setValue(NSDate(), forKey: "timeStamp")
    newManagedObject.latitude=self.lastLocation.coordinate.latitude
    newManagedObject.setValue.longitude = self.lastLocation.coordinate.longitude

    // Save the context.
    do {
        try context.save()
    } catch {        
        abort()
    }
}
TurboPascal
  • 61
  • 2
  • 6
  • 1
    Why are you still using Xcode 7.x, latest version is 8.3.2 (8E2002). – l'L'l May 10 '17 at 17:57
  • 2
    Swift has changed a lot since 2015, but it is now source-code stable. I think it's worth getting a book for Swift 3 -- otherwise you are going to run into this problem over and over. If you can't, download an older Xcode here: https://developer.apple.com/download/more/ -- then, when you are done, use the latest Xcode to do automatic migration and try to fix from there. – Lou Franco May 10 '17 at 18:05
  • I can't download an old Xcode because I do not have a paid account. – TurboPascal May 10 '17 at 19:12
  • I can't use Xcode 8 because I am using OS X 10.10 and Xcode 8 requires OS X 10.12 – TurboPascal May 10 '17 at 19:19
  • Does the entity have a property named "setValue". If not, it looks like a typo. I can't imagine why the line is not: `newManagedObject.longitude = self.lastLocation.coordinate.longitude` – Scott Thompson May 10 '17 at 20:49

0 Answers0