मैं इस तथ्य के बाद परिणामों पर कॉर्डटा से डेटा पढ़ने और लूप को डेटा पढ़ने का सबसे अच्छा तरीका ढूंढ रहा हूं।'AnyObject' में 'contactName' नामक कोई सदस्य नहीं है, जो मेरे लूप को अवरुद्ध कर रहा है
मैंने इसे कई तरीकों से आजमाया है लेकिन प्रत्येक वस्तु के भीतर डेटा के विशिष्ट टुकड़ों को प्राप्त करने की कोशिश करने पर लटका हुआ प्रतीत होता है।
यहां मेरा सेटअप है।
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!
}
और मैं इसे यहाँ फोन ..
var properties = Properties()
var getInfo = properties.readData()
println(getInfo)
for eachInfo:AnyObject in getInfo{
println(eachInfo)
}
और वह कुछ इस तरह बाहर उदासीनता ...
<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;
})
और वह महान है। लेकिन जब मैं नीचे दिए गए कोड के साथ डेटा तक पहुंचने के लिए जाता हूं ....
for eachInfo:AnyObject in getInfo{
println(eachInfo.contactName)
}
मुझे निम्न त्रुटि मिलती है।
" 'AnyObject' एक सदस्य नामित नहीं है 'CONTACTNAME'"
मुझे यकीन है कि कुछ सरल मुझे याद आ रही है कि वहाँ हूँ, लेकिन मैं इसे ऑनलाइन नहीं मिल रहा।
कोई सहायता या यहां तक कि एक बेहतर तरीका की सराहना की जाएगी।
उत्तर दिया गया।
सबसे पहले, कोर-डेटा ऑब्जेक्ट्स में नेमस्पेस जोड़ें। http://i.stack.imgur.com/qNNLo.png
दूसरा, फ़ंक्शन के आउटपुट को दृढ़ता से टाइप करें ताकि डेटा कस्टम ऑब्जेक्ट पर टाइप किया जा सके। इस मामले में इसे "गुण" कहा जाता है।
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>
}
अंत में, डेटा पर फ़ंक्शन और लूप पर कॉल करें।
var properties = Properties()
var getInfo = properties.readData()
for eachInfo in getInfo{
println(eachInfo.contactName)
}
यहां तक कि जब मैं ऐसा करता हूं, प्राप्त करने वाले अंत में भी यह "AnyObject" जैसा व्यवहार करना चाहता है। इस के साथ मुद्दों का कारण बनता है .. वर गुण = गुण() वर getInfo = properties.readData() eachInfo के लिए: getInfo में सरणी { println (eachInfo.contactName) } और यह एक EXC_BAD_INSTRUCTION त्रुटि का कारण बनता है। वर गुण = गुण() वर getInfo = properties.readData() getInfo { println (eachInfo.contactName) } में eachInfo के लिए –
आपका विधि काम करता है। मुझे बस यह सुनिश्चित करना होगा कि जब मैं अपनी कॉर्डेट ऑब्जेक्ट्स सेट करता हूं तो मेरे पास नेमस्पेस जोड़ा जाता है। मैं जवाब पोस्ट करूंगा। –