Access properties via subscripting in Swift -
i have custom class in swift , i'd use subscripting access properties, possible?
what want this:
class user { var name: string var title: string subscript(key: string) -> string { // here return // return property matches key… } init(name: string, title: string) { self.name = name self.title = title } } myuser = user(name: "bob", title: "superboss") myuser["name"] // "bob"
update: reason why i'm looking i'm using grmustache render html templates. i'd able pass model object grmustache renderer…
grmustache fetches values keyed subscripting objectforkeyedsubscript: method , key-value coding valueforkey: method. compliant object can provide values templates.
https://github.com/groue/grmustache/blob/master/guides/view_model.md#viewmodel-objects
(grmustache author here)
until swift-oriented mustache library out, suggest having classes inherit nsobject (so have valueforkey:
method). grmustache fetch values method.
in case still not work (blank values in rendering), may try disable grmustache security features (see https://github.com/groue/grmustache/blob/master/guides/security.md#disabling-safe-key-access)
should experience other trouble, please open issue right repository: https://github.com/groue/grmustache/issues
edit february 2, 2015: grmustache.swift out: http://github.com/groue/grmustache.swift
Comments
Post a Comment