Cannot Subscript a Value of Type '[Nsobject:Anyobject]' with an Index of Type 'String'

Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'UIImagePickerController.Infokey'

You’re using an older version of Swift, to get the image URL:

let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL

I suggest that you upgrade to Swift 5.0 in the future

Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'NSKeyValueChangeKey'

This is the correct signature of observeValue(forKeyPath:of:change:context:) (Swift 4):

func observeValue(forKeyPath keyPath: String?, 
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?)

Regardless of language version, the key for you is to change change: [NSObject : AnyObject] to change: [NSKeyValueChangeKey : Any]?.

Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'String' in swift4

You can try

 func application(_ application: UIApplication, 
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

if let aps = userInfo["aps"] as? [String:Any] {

}
}

//

OR

func  application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {}

Cannot subscript a value of type 'Dictionary NSObject, AnyObject ' with an index of type 'String error

Your code uses a lot of inappropriate objective-c-ish API.

This is the recommended Swift way to read and parse a property list file

if #available(iOS 7.1, *) {   
//ローカルplist格納先を取得
do {
let strWorkURL = try FileManager.default.url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let strPlistURL = strWorkURL.appendingPathComponent("\(S_DIRECTORY_NAME)/\(S_PLIST_NAME)")
let strPlistData = try Data(contentsOf: strPlistURL)
guard let dicPref = try PropertyListSerialization.propertyList(from: strPlistData, format: nil) as? [String:Any] else { return }
for (key, value) in dicPref {
UserDefaults.standard.set(value, forKey: key)
}
} catch { print(error) }
}

And don't use performSelector(inBackground:) either. Use GCD

DispatchQueue.global().async {
stopIndicator()
}

Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String'

Should be:

var apples = userDefaults.dictionaryForKey("apples_array")
println(apples?[appleId])

The issue here is that type [NSObject : AnyObject]? implies an optional type, which means you're attempting to call a subscript on what is essentially an enum. When you try to do this, there's no subscript declared, so the system chokes.

By adding the ? we're saying, unwrap this value if possible, and then call the subscript. This way the system infers to look on type [NSObject : AnyObject] for subscript declarations and everything is ok.

You could also use ! to force an unwrap, but this will crash if apples is nil. Another possible way to write this would be:

let apples = userDefaults.dictionaryForKey("apples_array") ?? [:]
println(apples[appleId])

This way, apples is no longer optional and it will always have the subscript syntax. No unwrapping necessary.

Cannot subscript a value of type '[NSObject : Any]' with an index of type 'String'

SWIFT 4 Solution:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

guard let image = info[UIImagePickerController.InfoKey.originalImage]
as? UIImage else {
return
}

imagePicked.image = image
//dismiss(animated:true, completion: nil)
}

Cannot subscript a value of type '[AnyObject]' with an index of type 'AnyObject'

Don't declare your array as [AnyObject] because you clearly want to store a concrete type - Dictionary. Use instead

var rows: [[String : AnyObject]] = [[:]]

let firstLine: [String : AnyObject] = ["time": time, "Playing": false]
rows.append(firstLine)

Cannot subscript a value of [AnyObject]? with an index of type Int

The problem isn't the cast, but the fact that self.objects seems to be an optional array: [AnyObject]?.

Therefore, if you want to access one of its values via a subscript, you have to unwrap the array first:

var user2: PFUser
if let userObject = self.objects?[indexPath.row] {
user2 = userObject as! PFUser
} else {
// Handle the case of `self.objects` being `nil`.
}

The expression self.objects?[indexPath.row] uses optional chaining to first unwrap self.objects, and then call its subscript.


As of Swift 2, you could also use the guard statement:

var user2: PFUser
guard let userObject = self.objects?[indexPath.row] else {
// Handle the case of `self.objects` being `nil` and exit the current scope.
}
user2 = userObject as! PFUser


Related Topics



Leave a reply



Submit