使用 iOS NSRegularExpression 进行 Ruby 风格的字符串插值
我正在尝试为我的应用程序中的模型生成 HTML 输出。这将有效地遍历并使用模型中的相关值填充 HTML 文件中的各个位置。我最初只是打算将 HTML 模板作为格式化字符串,但是如果稍后布局或任何内容发生任何更改,那么将值的顺序与它们在模板中出现的顺序相匹配就会变得非常混乱和乏味。
相反,我想做的是对文件运行某种 Ruby 风格的字符串插值。在任何我想要模型值的地方,我都会输入我想要的模型属性的名称,如下所示:#{key.path}
。
然后,我尝试使用以下正则表达式来处理此问题: @"#{([^}]+)}"
。
为了处理这个问题,我使用以下设置:
NSString *processedTemplate = [regex stringByReplacingMatchesInString:template
options:0
range:NSMakeRange(0, template.length)
withTemplate:[self valueForKeyPath:@"$1"]];
但是,当我运行这个时,我收到错误:
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<Plan 0x78349d0> valueForUndefinedKey:]: this class is not
key value coding-compliant for the key $1.'
我期望的是我可以使用正则表达式匹配并使用它来获取模型中的键值编码兼容值。但是,这显然不适用于我使用的方式。
顺便说一句,我认为我使用了这个权利,但是当我运行它来替换 withTemplate:@"$1"
时,我得到 NULL。因此,我尝试使用:
NSString *processedTemplate = [template stringByReplacingOccurrencesOfString:@"#{([^}]+)}"
withString:@"$1"
options:NSRegularExpressionSearch
range:NSMakeRange(0, template.length)];
但是,当我运行它时,它不会替换为 ()
中的部分。所以无论如何,我没有做正确的事情。有人有任何指示/解决方案吗?
更新
因此,withString:
参数似乎会将 @"$1"
解释为正则表达式匹配数据找到的任何内容。是否有另一种方法来检索匹配数据,以便将其传递到 valueForKeyPath:
等方法中?
更新
在我的旁注中,我不知道为什么,但我的正则表达式 #{([^}]+)}
与我期望的不匹配。我将其与任何其他正则表达式模拟器进行比较,似乎都可以很好地匹配它,但它在 iOS 上的 obj-c 中不匹配。我在字符集 #{}
上的转义中缺少什么吗?
I'm attempting to generate an HTML-output for a model in my app. This will effectively go through and fill in various spots inside an HTML file with the relevant values within the model. I was originally just going to take the HTML template as a formatted string, but if anything changes later on the layout or anything, this will just get really messy and tedious to match up the order of the values to the order they appear in the template.
Instead, what I'm trying to do is run a sort of Ruby-style string interpolation of the file. Anywhere I want a value from the model, I put the name of the model attribute I want like so: #{key.path}
.
Then, I'm attempting to process this with the following Regex:@"#{([^}]+)}"
.
To process this, I'm using the following setup:
NSString *processedTemplate = [regex stringByReplacingMatchesInString:template
options:0
range:NSMakeRange(0, template.length)
withTemplate:[self valueForKeyPath:@"$1"]];
However, when I run this, I get the error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<Plan 0x78349d0> valueForUndefinedKey:]: this class is not
key value coding-compliant for the key $1.'
What I expect is that I can use the regex match and use it to grab the key-value coding compliant value in my model. However, this clearly doesn't work the way I'm using it.
On a side note, I think I'm using this right, but when I just run this to replace withTemplate:@"$1"
I get NULL. So, I tried it using:
NSString *processedTemplate = [template stringByReplacingOccurrencesOfString:@"#{([^}]+)}"
withString:@"$1"
options:NSRegularExpressionSearch
range:NSMakeRange(0, template.length)];
However, when I run this it doesn't replace with the section in ()
. So one way or another, I'm not doing something right. Anyone have any pointers/solutions?
Update
So it looks like the withString:
parameter will interpret @"$1"
as whatever the regex match data has found. Is there another way to retrieve match data so it can be passed into such methods as valueForKeyPath:
?
Update
On my side note, I don't know why, but my regex #{([^}]+)}
does not match as I expect it to. Any other regex simulator I put it up against seems to match it just fine, but it doesn't match in obj-c on iOS. Is there something I'm missing with escapes on character set #{}
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个示例中的 self 是什么样的对象?您是否覆盖了
valueForKeyPath:
?方法
-valueForKeyPath:
是为 NSObject 定义的,用于返回基于 KVC 键路径的值。该代码将首先将
foo
发送到anotherObj
,然后将bar
发送到结果,然后将baz
发送到结果并返回最终结果。本质上,运行时抱怨您没有名为
-$1
的方法。顺便说一下,在 Objective-C 中,方法的参数是在方法本身之前评估的,因此
评估 before
stringByReplacingMatchesInString:
和$1
没有任何意义valueForKeyPath:
的特殊属性。What kind of object is
self
in your first example? Have you overriddenvalueForKeyPath:
?The method
-valueForKeyPath:
is defined for NSObject to return a value based on KVC key paths. The codewill first send
foo
toanotherObj
, then sendbar
to the result, then sendbaz
to the result of that and return the final result.Essentially, the runtime is complaining that you don't have a method called
-$1
.And by the way, in Objective-C parameters to methods are evaluated before the method itself, so
is evaluated before
stringByReplacingMatchesInString:
and$1
means nothing special tovalueForKeyPath:
.