Hi!
I am very new to xcode and I am working on a shopping list program that I have followed through a tutorial online. I am currently getting this error and I am unsure what I have missed.
Any help is appreciated. Thanks so so much!
let ud = UserDefaults.standard
if !ud.bool(forKey: "UserDefaultsSeedItems") {
if let filePath = Bundle.main.path(forResource: "seed", ofType: "plist"), let seedItems = NSArray(contentsOfFile: filePath) {
// Items
var items = [Item]()
// Create List of Items
for seedItem in seedItems {
if let name = seedItem["name"] as? String, let price = seedItem["price"] as? Float { //2 Errors here; "Value of type 'Any' has no subscripts"
print("\(name) - \(price)")
// Create Item
let item = Item(name: name, price: price)
// Add Item
items.append(item)
}
}
print(items)
if let itemsPath = pathForItems() {
// Write to File
if NSKeyedArchiver.archiveRootObject(items, toFile: itemsPath) {
ud.set(true, forKey: "UserDefaultsSeedItems")
}
}
}
}
}
@JZX_AP You don't answer all questions, so it is harder to help.
What did print("seedItems", seedItems) actually print?
But maybe seedItem needs to be cast to Item
for seedItem in seedItems {
if let seed = seedItem as? Item {
let name = seed.name //ERROR Value of type 'Any' has no member 'name'
let price = seed.price //ERROR Value of type 'Any' has no member 'price'
print("\(name) - \(price)")
// Create Item
let item = Item(name: name, price: price)
// Add Item
items.append(item)
}
Question: why do you need to create a new Item and not just reuse seed ?