Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GRMustache interferes with the AnyObject subscript (Cast from 'MustacheBox!' to unrelated type 'String' always fails) #12

Closed
crarau opened this issue Oct 12, 2015 · 5 comments

Comments

@crarau
Copy link

crarau commented Oct 12, 2015

Before using Mustache.swift the codebase was accessing the subscript of an AnyObject (that was a Dictionary underneath). I know it's not a best practice but this was working.
After adding Mustache, it seems that the subscript access is override and a MustacheBox is returned.

I looked at the MustacheBox and I can't figure out why this is happening.

image

We easily fixed the issue by not treating the Dictionary like an AnyObject but like an Dictionary. But I'd like to understand why MustacheBox interfered with the AnyObject subscript.

Thanks for the library, it's so useful!

Note: I'm using the Swift2 branch

@crarau crarau changed the title MustacheBox interferes with the AnyObject subscript MustacheBox interferes with the AnyObject subscript (Cast from 'MustacheBox!' to unrelated type 'String' always fails) Oct 12, 2015
@crarau crarau changed the title MustacheBox interferes with the AnyObject subscript (Cast from 'MustacheBox!' to unrelated type 'String' always fails) GRMustache interferes with the AnyObject subscript (Cast from 'MustacheBox!' to unrelated type 'String' always fails) Oct 12, 2015
@groue
Copy link
Owner

groue commented Oct 13, 2015

Hello @crarau

Things are indeed weird:

let dict:[String: AnyObject] = ["key": "value"]
let anyObject = dict as AnyObject

let v2 = anyObject["key"]   // Alt-click on v2: MustacheBox!
print(v2.dynamicType)       // Prints ImplicitlyUnwrappedOptional<MustacheBox>
print(v2.value)             // Crash

let v3 = anyObject["key"]!  // Alt-click on v3: MustacheBox
print(v3.dynamicType)       // Prints _NSContiguousString

So we're facing a paticularly crazy Swift bug.

After refactoring the subscript functions declared by GRMustache, I could spot the origin of the problem:

final public class MustacheBox : NSObject {
    public subscript (key: String) -> MustacheBox
}

OK, so:

  • I'll open a bug report to Apple.
  • I'll refactor this subscript function into a regular function.
  • I'll publish a new GRMustache.swift version.

Thanks again for opening thise issue, stay tuned!

@groue
Copy link
Owner

groue commented Oct 13, 2015

For what it's worth, Xcode 7.1 beta 3 (7B85) still has the issue.

@groue groue closed this as completed in afa6d83 Oct 14, 2015
@groue
Copy link
Owner

groue commented Oct 14, 2015

Next release is coming shortly.

@groue
Copy link
Owner

groue commented Oct 14, 2015

Please update to GRMustache v0.11.0. Oh, and please stick to tagged versions. Forget about branches, because they may contain unstable code.

@groue
Copy link
Owner

groue commented Oct 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants