JSA986
JSA986

Reputation: 5936

AnyObject does not have a member named "sort"

I have an array var savedDataArray: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("savedDataArray")G which I want to sort ascending

Im checking if its empty, then unwrapping it, however i'm still getting the "does not have a member named" error?

if savedDataArray?.count>0{
            savedDataArray!.sort{$0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
        }

error Any object does not have a member named "sort"

So then i tried reversing the array and big surprise the same error for .reverse

I thought by checking if the array is nil, then force unwrapping, these errors wouldnt be relevant

Upvotes: 1

Views: 230

Answers (1)

Chackle
Chackle

Reputation: 2269

Don't access the array as an "AnyObject". Try pulling it as an Array object.

var savedDataArray: Array<String>? = NSUserDefaults.standardUserDefaults().objectForKey("savedDataArray") as? Array<String>;

Substitute the "String" type for whatever you're pulling the objects in your Array as. This allows you to use the "sort" member.

Upvotes: 6

Related Questions