Ninja
Ninja

Reputation: 358

Why do we put @nonobjc attribute before fetchRequest() declaration?

I'm declaring usual managedobject class like this

 public extension Camper {
 @nonobjc class func fetchRequest() -> NSFetchRequest<Camper> {
  return NSFetchRequest<Camper>(entityName: "Camper")
}

@NSManaged var fullName: String?
@NSManaged var phoneNumber: String?
@NSManaged var reservations: Reservation?

} 

and I'm really confused about intention of @nonobjc attribute in that concrete situation.

Upvotes: 0

Views: 569

Answers (1)

MartinM
MartinM

Reputation: 937

Normally you would use @nonobjc annotations to resolve circular dependency issues that this would generate. So for example, you define something in swift, that uses something that is defined in objc, which uses this "something" from swift again. This would not work because you need to import the bridging header from objc into swift, and vice versa. The compiler cannot resolve this, if you would not supply this @nonobjc annotation.

Refer to Apple's documentation about how to use @objc and @nonobjc: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html

Upvotes: 1

Related Questions