groue / GRMustache.swift

Flexible Mustache templates for Swift
http://mustache.github.com/
MIT License
597 stars 155 forks source link

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

Closed crarau closed 8 years ago

crarau commented 8 years ago

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

groue commented 8 years ago

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:

Thanks again for opening thise issue, stay tuned!

groue commented 8 years ago

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

groue commented 8 years ago

Next release is coming shortly.

groue commented 8 years ago

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

groue commented 8 years ago

rdar://23122734 (http://openradar.appspot.com/radar?id=4952382538514432)