Reputation: 317
I'm puzzled by an error using Core Data (XCode 7.1 / Swift 2.0)
The below code is working perfectly
class DB_PlayerData: NSManagedObject
{
@NSManaged var playerID: NSNumber
}
class PlayerClass
{
var IDPlayer: Int = 0
}
func test()
{
let LocalAppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let ManagedObjectContext = LocalAppDelegate.managedObjectContext
var SelectAll = NSFetchRequest()
var ResultatRecherche: NSArray = []
var gPlayerData = PlayerClass()
SelectAll = NSFetchRequest(entityName: eNomDBFiles.DB_PlayerData.rawValue)
try! ResultatRecherche = ManagedObjectContext?.executeFetchRequest(SelectAll) as! [DB_PlayerData]
if (ResultatRecherche.count == 1)
{
gPlayerData.IDPlayer = Int(ResultatRecherche[0].playerID)
}
else
{
// Do something
}
}
But if the function test
is not in the same swift file in my project as the DB_PlayerData
class declaration, I get the following error: Value of type 'AnyObject' has no member 'playerID'
Entity has been created in the DB model in Xcode with name and class equal to DB_PlayerData
and Module set to project name. I've tried all kind of setup options described here and there was no success.
Any clue why?
Upvotes: 0
Views: 148
Reputation: 2346
You havent told the compiler which type is enclosed in your array.
var ResultatRecherche: [DB_PlayerData] = []
instead of
var ResultatRecherche: NSArray = []
I suggest you always try to use the correct swift type instead of the foundation types such as NSArray. If you absolutely must use NSArray
, you need to cast ResultatRecherche[0]
to the correct type before trying to access a property, otherwise it will have the type of AnyObject.
Upvotes: 1