Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

IOS: Image viewer error help- invalid redeclaration error

After debugging mistakes with the video, my app seemed to stop working and I can't figure out why. Any advice would be appreciated

import Foundation import CoreData import UIKit

public class Photo: NSManagedObject {

}

extension Photo {

@nonobjc public class func fetchRequest() ->
    NSFetchRequest<Photo> {
    let request = NSFetchRequest<Photo>(entityName: "Photo")
    let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: true)
    request.sortDescriptors = [sortDescriptor]
    return request
}

@NSManaged public var creationDate: NSDate. ----- Error says invalid redeclaration
@NSManaged public var imageData: NSData ------ Error says invalid redeclaration 

}

extension Photo { static var entityName: String { return String(describing: Photo.self) }

@nonobjc class func with(_ image: UIImage, in context: NSManagedObjectContext) -> Photo {
    let photo = NSEntityDescription.insertNewObject(forEntityName: Photo.entityName, into: context) as! Photo

    photo.creationDate = Date() as NSDate
    photo.imageData = UIImageJPEGRepresentation(image, 1.0)! as NSData

    return photo
}

}

extension Photo { var image: UIImage { return UIImage(data: self.imageData as Data)! } }

1 Answer

1- Go to Entities model 2- select Photo model 3- Go to the "Show the Data model Inspector" in the right pan 4- Change the "Codegen" Field to "Manual/None"

For more information you can look to this link : https://stackoverflow.com/questions/40410169/invalid-redeclaration-on-coredata-classes

After run the app first time .. you can change the "Codegen" Field back to "Category/Extension"