我觉得我错过了Groovy处理字符串的方法.我意识到它们是不可变的,但我想要做的是在运行时插入一个值.我无法弄清楚如何.让我在Python中给出一个非常简单的例子(作为"可执行的伪代码")来说明我的意思.然后我将给出我在Groovy中尝试过的内容.
蟒蛇
# string_sample.py
class MyClass(object):
greeting = 'Hello, %s!'
def __init__(self):
object.__init__(self)
def sayHello(self, name):
print self.greeting % name
if __name__ == '__main__':
m = MyClass()
m.sayHello('Mario')
Run Code Online (Sandbox Code Playgroud)
以上版画:你好,马里奥!
Groovy的
// string_sample.groovy
class MyClass {
def greeting = "Hello, ${name}!"
MyClass() {
}
void sayHello(name) {
println greeting
}
}
m = new MyClass()
m.sayHello('Mario')
Run Code Online (Sandbox Code Playgroud)
上面的Groovy脚本抱怨name
未知:
Caught: groovy.lang.MissingPropertyException: No such property: name for class: MyClass
我理解发生了什么以及为什么.我只是不确定该怎么做.我意识到String.format
可以使用,这不是很糟糕:
String greeting = "Hello, %s!"
// Omitted...
void sayHello(name) …
Run Code Online (Sandbox Code Playgroud) 我意识到下面的代码示例是你永远不应该做的事情.我的问题只是一个有趣的问题.如果你分配一块内存,然后移动指针(一个禁忌),当你释放内存时,释放的块的大小是多少,内存在哪里?这是人为的代码片段:
#include <stdio.h>
#include <string.h>
int main(void) {
char* s = malloc(1024);
strcpy(s, "Some string");
// Advance the pointer...
s += 5;
// Prints "string"
printf("%s\n", s);
/*
* What exactly are the beginning and end points of the memory
* block now being deallocated?
*/
free(s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这就是我认为我发生的事情.被释放的内存块以保存字符串"s"的字节开头.持有"Some"的5个字节现在丢失了.
我想知道的是:5个字节在内存中的位置是否紧跟原始1024字节的末尾,也就是说,它们是否只是单独存在?
任何人都知道编译器的作用是什么?这是不确定的?
谢谢.
问题:在IB中使用自动布局进行设计时,UIView大小被"锁定"
我上传了一段20秒的视频来说明我的问题,但让我来描述一下.您可能会认识到这个项目:它出自Apple的WWDC 2012"自动布局简介".我在Interface Builder中有一个简单的iPhone视图,我已经在底部有一个按钮和标签.下一步是添加UIView.事情变得丑陋.
当我将UIView拖出Interface Builder中的调色板并进入iPhone窗口时,一旦我放开它,它似乎就会被锁定到它的起始大小.我将它放在左上角,我想将它水平拖动到窗口的右侧,并垂直向下拖动它(减去填充)按钮和底部标签的位置.但是,当我尝试调整它时,我不能.它坚持原来的大小!
说明我的问题的视频在这里:
http://www.youtube.com/watch?v=jsW4UwnCEkw
您可以看到我抓住视图的右边缘并尝试将其拉到窗口的右侧,但它不会展开.然后,您可以看到我将整个视图移动到右侧(它保持其大小),然后尝试相反的方法:抓住左侧并尝试展开它以使其大小靠近窗口的左侧.在这两种情况下,视图都只是锁定到其原始大小.
是什么赋予了!Interface Builder中的自动布局是最令人沮丧的事情.如何通过拖动调整视图大小?谢谢!
(注意:Xcode 4.6.3.)
编辑:我解决了我的问题。(见下文。)
问题
我是 NetBeans 的新手,我遇到了一个问题,涉及将资源加载到我的 servlet 应用程序,该应用程序通过 NetBeans 运行,使用 Tomcat 作为服务器。一切都很好,直到我尝试使用 Mustache 模板库(在 Java 中)构建我的响应。此时,抛出异常:
com.github.mustachejava.MustacheException: File not under root: /opt/catalina/bin
Run Code Online (Sandbox Code Playgroud)
这个异常是在我尝试编译模板文件的代码中抛出的。我喂路径资源(模板文件)的compile
方法MustacheFactory
。我的代码如下所示:
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile(getFormTemplatePath());
Run Code Online (Sandbox Code Playgroud)
我的研究
我查看了 Mustache 代码,尽我所知,这是发生了什么。Mustache 在加载资源时会进行安全检查,试图确保该资源位于文件系统中应用程序的根目录下。由于 NetBeans 使用某种魔法在 Tomcat 服务器上运行代码,而项目代码实际上位于文件系统的其他位置,Mustache 认为发生了一些可疑的事情。
换句话说,它可以找到文件;它只是不喜欢它在哪里找到它。似乎 Mustache/opt/catalina/bin
作为应用程序的根,而模板文件实际上位于更像的路径:~/NetBeansProjects/MyProject/WEB-INF/template_file.mst
.
Mustache 代码看起来像这样(这样你就可以检查我的推理):
try {
// Check to make sure that the file is under the file root or current directory.
// Without this check you might accidentally open a …
Run Code Online (Sandbox Code Playgroud)