Hello:
I am getting the error Value of Type Bool has no subscripts in bellow code but can't figure out how to resolve it. Any help will be appreciated.
The error is on line # 19. That line is supposed to call first record and continue with subsequest records in the plist.
func preloadData() {
print ("Call to preload Data Starts Now")
let preloadedDataKey = "didPreloadData"
UserDefaults.standard.removeObject(forKey: preloadedDataKey)
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: preloadedDataKey) == false {
guard Bundle.main.url(forResource: "SCQ", withExtension:"plist") != nil else {
return
}
var propertyListFormat = PropertyListSerialization.PropertyListFormat.xml //format the property list
var plistData: [String: AnyObject] = [:] // the data
print(plistData)
let plistUrl = Bundle.main.url (forResource: "SCQ", withExtension: "plist") // path of the data
let fileManager = FileManager.default
if let plistData = NSData(contentsOf: plistUrl!) {
var format = PropertyListSerialization.PropertyListFormat.xml
do {
let items = PropertyListSerialization.propertyList(plistData, isValidFor: format)
let item = items[0]// error here
if let attribute = item ["answer"] {
print (item)
}
if let attribute = item ["distractor1"] {
print (item)
}
if let attribute = item ["distractor2"] {
print (item)
}
if let attribute = item ["distractor3"] {
print (item)
}
if let attribute = item ["distractor4"] {
print (item)
}
if let attribute = item ["distractor5"] {
print (item)
}
if let attribute = item ["grade"] {
print (item)
}
if let attribute = item ["id"] {
print (item)
}
if let attribute = item ["answer"] {
print (item)
}
if let attribute = item ["qid"] {
print (item)
}
if let attribute = item ["question"] {
print (item)
}
if let attribute = item ["qValue"] {
print (item)
}
if let attribute = item ["skill"] {
print (item)
}
if let attribute = item ["subject"] {
print (item)
}
if let attribute = item ["topic"] {
print (item)
}
} catch {
print(error)
}
}
As already noted, `PropertyListSerialization.propertyList(_:isValidFor:)` returns Bool, which is used when serializing your Objective-C object and checks if it is serializable with given format.
In your case, you want to deserialize a plist Data, so it is completely useless in your case.
You need to use this method.
You can use it like this:
func preloadData() {
print("Call to preload Data Starts Now")
let preloadedDataKey = "didPreloadData"
UserDefaults.standard.removeObject(forKey: preloadedDataKey)
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: preloadedDataKey) == false {
guard let plistUrl = Bundle.main.url(forResource: "SCQ", withExtension:"plist") else {
return
}
do {
let plistData = try Data(contentsOf: plistUrl)
var format = PropertyListSerialization.PropertyListFormat.xml
if
let items = try PropertyListSerialization.propertyList(from: plistData, format: &format) as? [[String: Any]],
let item = items.first
{
if let answer = item["answer"] {
print(answer)
}
//...
} else {
print("plist is not an Array of Dictionary or is empty")
}
} catch {
print(error)
}
}
}