Google Sign-In for iOS: Error "Cannot Subscript a Value of Type '[String:Anyobject]' with an Index of Type 'String'"

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

There is an error in your code, but the error message you're seeing is incorrect and misleading due to a Swift compiler bug. The actual error message should read: AnyObject is not convertible to [String:AnyObject].

self.story["comments"] returns an AnyObject. To assign that value to self.comments you must first typecast AnyObject to the Dictionary type [String:AnyObject].

For example:

self.comments = self.story["comments"] as! [String:AnyObject]

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

[[String:Any]] is an array. It can be only subscripted by Int index.

You have to iterate over the array for example:

if let reviews = place.details?["reviews"] as? [[String:Any]] {
for review in reviews {
if let authorName = review["author_name"] as? String {
// do something with authorName
}
}
}

Google Sign-In for iOS: error Cannot subscript a value of type '[String : AnyObject]' with an index of type 'String'

function before iOS 9.0

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication, annotation: annotation)
}

or with the method you are using, you can using it like (Prior to iOS 9.0 and above)

func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any])
-> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: [:])
}

or to support both in iOS 9 and above and in iOS 8.0 to iOS 9.0

@available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any])
-> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: [:])
}

//for iOS 8, check availability
@available(iOS, introduced=8.0, deprecated=9.0)
func application(application: UIApplication,openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication!, annotation: annotation)
}

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

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]?.

iOS Google Sign In error

handleURL is looking for arguments of type "String" for sourceApplication and annotation, but options is providing "AnyObject." Casting those dictionary values to "String" oughtta do it.

Try this:

Update: Swift 3

func application(_ application: UIApplication,
open url: URL, options: [UIApplicationOpenURLOptionsKey: Any]) -> Bool
return GIDSignIn.sharedInstance().handle(url,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation] as? String)
}

Swift 2

func application(application: UIApplication,
openURL url: NSURL, options: [String: AnyObject]) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey] as? String)
}

This took me forever, too. Hope that helps!

Cannot subscript a value of type 'JSON' with an index of type '(key: String)'

Try to remove key: and index: from your code, i think will resolve.

ex: self[sub as! String]

Why I Cannot subscript a value of type '[String : [String]]' with an argument of type 'String.SubSequence' (aka 'Substring')?

You can simply use Dictionary's init(grouping:by:) initializer like so,

var animalsDict = Dictionary(grouping: animals) { String($0.first!) }

var animalSectionTitles = animalsDict.keys.sorted()

Output:

print(animalsDict) //["G": ["Giraffe", "Greater Rhea"], "P": ["Panda", "Peacock", "Pig", "Platypus", "Polar Bear"], "E": ["Emu"], "H": ["Hippopotamus", "Horse"], "K": ["Koala"], "L": ["Lion", "Llama"], "R": ["Rhinoceros"], "D": ["Dog", "Donkey"], "B": ["Bear", "Black Swan", "Buffalo"], "M": ["Manatus", "Meerkat"], "W": ["Whale", "Whale Shark", "Wombat"], "S": ["Seagull"], "T": ["Tasmania Devil"], "C": ["Camel", "Cockatoo"]]

print(animalSectionTitles) //["B", "C", "D", "E", "G", "H", "K", "L", "M", "P", "R", "S", "T", "W"]

Google Sign-In IOS Swift 4 AppDelegate Error

You need to implement these 2 methods with this signature

func application(_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
{
if #available(iOS 9.0, *) {

return GIDSignIn.sharedInstance().handle(url,sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: [:])

}

return true

}

public func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {

return GIDSignIn.sharedInstance().handle(url,sourceApplication: sourceApplication, annotation: annotation)

}


Related Topics



Leave a reply



Submit