Sto cercando il modo migliore per leggere i dati da parte di una funzione in un coredata e ripetere i risultati dopo il fatto.'AnyObject' non ha un membro chiamato 'contactName' Blocco del mio loop
Ho provato un certo numero di modi, ma sembra essere appeso al tentativo di ottenere le specifiche parti di dati all'interno di ogni oggetto.
Ecco il mio setup.
func readData()->NSArray{
let entityName:String = "Properties"
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
// Specify what this will be working with
let request = NSFetchRequest(entityName: entityName)
// This will return instance of the object needed. Without which this would be
// a pain to work with. This bit saves a few steps.
request.returnsObjectsAsFaults = false;
// Get the data and put it into a variable
var results = context.executeFetchRequest(request, error: nil)
return results!
}
E lo chiamo io qui ..
var properties = Properties()
var getInfo = properties.readData()
println(getInfo)
for eachInfo:AnyObject in getInfo{
println(eachInfo)
}
E questo discariche fuori qualcosa di simile ...
<NSManagedObject: 0x7f87fa711d60> (entity: Properties; id: 0xd000000000040000 <x-coredata://F627AD12-3CEC-4117-8294-616ADEE068DC/Properties/p1> ; data: {
acreage = 0;
conditionId = 0;
contactBusinessName = nil;
contactEmail = nil;
contactName = Bob;
contactPhoneNumber = 456456456;
isFav = nil;
latitude = 0;
longitude = 0;
price = 0;
propertyCity = nil;
propertyId = nil;
propertyState = nil;
propertyStreetAddress = nil;
propertyType = 0;
propertyZip = nil;
squareFeet = 0;
year = 0;
})
E che è grande. Ma quando vado ad accedere ai dati all'interno con il codice sottostante ....
for eachInfo:AnyObject in getInfo{
println(eachInfo.contactName)
}
Ho il seguente errore.
" 'ANYOBJECT' non ha un membro denominato 'NomeContatto'"
Sono sicuro che ci sia qualcosa di semplice che mi manca, ma non riesco a trovare on-line.
Qualsiasi aiuto o anche un modo migliore sarebbe molto apprezzato.
Risposta.
In primo luogo, aggiungere lo spazio dei nomi agli oggetti del core-data. http://i.stack.imgur.com/qNNLo.png
In secondo luogo, digitare l'output della funzione in modo che i dati vengano digitati sull'oggetto personalizzato. In questo caso è chiamato "Proprietà".
func readData()->Array<Properties>{
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
// Specify what this will be working with
let request = NSFetchRequest(entityName: entityName)
// This will return instance of the object needed. Without which this would be
// a pain to work with. This bit saves a few steps.
request.returnsObjectsAsFaults = false;
// Get the data and put it into a variable
var results = context.executeFetchRequest(request, error: nil)
return results! as Array<Properties>
}
Infine, richiamare la funzione e ripetere il ciclo dei dati.
var properties = Properties()
var getInfo = properties.readData()
for eachInfo in getInfo{
println(eachInfo.contactName)
}
Anche quando lo faccio, dal lato ricevente vuole ancora trattarlo come "AnyObject". provoca problemi con questo .. proprietà var = Properties() var = getInfo properties.readData() per eachInfo: Array in getInfo { println (eachInfo.contactName) } E questo causa un errore EXC_BAD_INSTRUCTION. proprietà var = Properties() var = getInfo properties.readData() per eachInfo in getInfo { println (eachInfo.contactName) } –
vostro metodo funziona. Devo solo assicurarmi di aver aggiunto lo spazio dei nomi quando ho installato gli oggetti Coredata. Posterò la risposta –