0

I have updated my Xcode to version 7.3, which is compatible with iOS 9.3. My sort() method, which is let sortArr = saveDeals?.sort({ $1[2] as! String > $0[2] as! String});, is giving me an "ambiguous use of subscript" error. Please let me know what I can do to fix this error. Thank you!

func sortSaveDealsArr(saveDeals: [AnyObject]?) -> [AnyObject]
{
    let sortArr = saveDeals?.sort({ $1[2] as! String > $0[2] as! String});
    return sortArr!;
}
1
  • What is saveDeals? Everything depends on that. Commented Mar 28, 2016 at 3:05

1 Answer 1

2

The problem is that saveDeals is typed as a [AnyObject]?. Thus your $0 and $1 are each an AnyObject, and you can't subscript an AnyObject. You need to cast each of these things to something that is itself subscriptable. I don't know what's in your array so I can't say what that would be. Is saveDeals an array of arrays? If so, you need to cast saveDeals to that.

Sign up to request clarification or add additional context in comments.

2 Comments

saveDeals is actually an array of arrays. Once I changed the code to func sortSaveDealsArr(saveDeals: [[AnyObject]]?) -> [[AnyObject]] { let sortArr = saveDeals?.sort({ $1[2] as? String > $0[2] as? String}); return sortArr!; }, the error went away and worked as intended. Thanks :)
Fantastic. Thanks for reporting back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.