Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSManagedObject Can't conform to protocol in Swift

Tags:

ios

swift

I need a shared interface for a NSManagedObject and a regular NSObject. In Objective-c,I could use a protocol to achieve that. But in Swift i get this runtime error. Any Solution? Thanks in advance!

protocol Product {
    var code: String { get set }
    var sp: String { get set }
}

class Stock: NSManagedObject, Product {
    @NSManaged var code: String
    @NSManaged var sp: String
}

Error: Undefined symbols for architecture i386: "__TFC11YellowPages5Stockg2spSS", referenced from: __TFC11YellowPages5Stockm2spSS in Stock.o "__TFC11YellowPages5Stockg4codeSS", referenced from: __TFC11YellowPages5Stockm4codeSS in Stock.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)

like image 521
WeaponJ Avatar asked Nov 29 '14 15:11

WeaponJ


1 Answers

This worked for me. Try it for yourself and see if it works:

class MyEntity: NSManagedObject {

    @NSManaged var testAttribute: String
}

@objc
protocol MyProtocol {

    var testAttribute: String { get set }
}

extension MyEntity: MyProtocol { }

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        let myContext = appDelegate.managedObjectContext!
        let entity: MyEntity = NSEntityDescription.insertNewObjectForEntityForName("MyEntity", inManagedObjectContext: myContext) as MyEntity
        foo(entity)
        println(entity.testAttribute)
    }

    func foo(var object: MyProtocol) {
        object.testAttribute = "bar"
    }
}

The below also worked, but I think the above is a better way to do it:

@objc
protocol MyProtocol {

    var testAttribute: String { get set }
}

class MyEntity: NSManagedObject, MyProtocol {

    @NSManaged var testAttribute: String
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        let myContext = appDelegate.managedObjectContext!
        let entity: MyEntity = NSEntityDescription.insertNewObjectForEntityForName("MyEntity", inManagedObjectContext: myContext) as MyEntity
        foo(entity)
        println(entity.testAttribute)
    }

    func foo(var object: MyProtocol) {
        object.testAttribute = "bar"
    }
}
like image 162
Daniel T. Avatar answered Nov 15 '22 05:11

Daniel T.