我是以UILabels
编程方式绘制的.他们从数据库中获取大小.所以我不能只使用sizeToFit
.我已经实现了UILabels
一个以传递比率重绘的函数.所以我需要找到的是UILabel
我视图中的文本,需要重新绘制最大比例UILabels
.所以最后我需要做这样的事情:
double ratio = 1.00;
for (UILabel* labels in sec.subviews) {
float widthLabel = labels.frame.size.width;
float heightLabel = labels.frame.size.height;
float heightText = //get the text height here
float widthText = //get the text width here
if (widthLabel < widthText) {
ratio = MAX(widthText/widthLabel,ratio);
}
if (heightLabel < heightText) {
ratio = MAX(heightText/heightLabel, ratio);
}
}
//redraw UILabels with the given ratio here
Run Code Online (Sandbox Code Playgroud)
那么如何获得文本的高度和宽度大小,因为我的一些文本不适合标签我不能简单地使用标签边界?我正在使用Xcode 5和iOS 7.
我希望用户传递两个参数或将其留空.例如:
./program 50 50
Run Code Online (Sandbox Code Playgroud)
要么
./program
Run Code Online (Sandbox Code Playgroud)
当我尝试使用时int main(int argc, char *argv[])
,我所做的第一件事就是改变char *argv[]
,int *argv[]
但它没有用.我想要的是用户只需输入0到100之间的两个整数.所以如果它不是两个整数那么它应该给出一个错误.
我有点想用类型给出错误(就像我以前在C#上编程的那样),但无论我输入什么,argv [1]都会一直是'char'类型.
所以我所做的就是
for (int i = 0; i <= 100; i++) {
//printf("%d", i);
if (argv[1] == i) {
argcheck++;
printf("1st one %d\n", i);
}
else if (argv[2] == i) {
argcheck++;
printf("2nd one %d\n", i);
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.此外,它在编译时发出警告,但如果我改为argv
使用atoi(argv[1])
,那么它会产生分段错误(核心转储)错误.
我需要一种简单的方法来解决这个问题.
编辑:
所以我解决了atoi()
,它给出分段错误的原因是因为我在没有参数的情况下尝试使用null值.所以我通过添加额外的cond来修复它.但现在问题是,如果价值是让我们说的话
./program asd asd
Run Code Online (Sandbox Code Playgroud)
然后输出atoi(argv[1])
将是0.有没有办法改变这个值?
我知道这个问题有重复,但我的情况在这里有所不同.
当用户返回主页(void)applicationDidEnterBackground
时,从AppDelegate
类中调用.但是一旦用户按下主页按钮,我不希望用户再次看到这个视图控制器,所以我有一个名为(void)goToBeginning
切换到另一个视图控制器的方法.我希望能够从AppDelegate调用此方法.我真的不想用NotificationCenter
它.此处选择的解决方案:
从app委托调用视图控制器方法
对我来说不起作用,因为它初始化新对象,而我希望能够调用已在视图中的对象.我怎样才能做到这一点?我使用的是iOS 7和XCode 5.
我搜索了这个,并试图找到有关它的文档,但没有这样做.
问题很简单.我有一个List
让我们说foo的话.如果我做
foo.forEach(this::doSomething)
再次使用相同的行,foo
我每次都会有相同的迭代次序吗?
如果是的话,怎么样foo.stream().forEach()
?
你好不能为C#找到它,我正在尝试这样的事情
for (int j = mediumNum; j < hardNum; j++; && int k = 0; k < mediumNum; k++);
Run Code Online (Sandbox Code Playgroud)
但它不起作用.任何有效的方法???
如标题所述,这是我的代码:
class Foo {
public:
Foo (int charSize) {
str = new char[charSize];
}
~Foo () {
delete[] str;
}
private:
char * str;
};
Run Code Online (Sandbox Code Playgroud)
对于这个类,有什么区别:
int main () {
Foo* foo = new Foo(10);
delete foo;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和
int main () {
Foo* foo = new Foo(10);
foo->~Foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我编写了一个简单的脚本,它应该读取整个目录,然后通过删除HTML标记将HTML数据解析为普通脚本,然后将其写入一个文件.
我有8GB内存和大量可用的虚拟内存.当我这样做时,我有超过5GB的RAM可用.目录中最大的文件是3.8 GB.
脚本是
file_count = 1
File.open("allscraped.txt", 'w') do |out1|
for file_name in Dir["allParts/*.dat"] do
puts "#{file_name}#:#{file_count}"
file_count +=1
File.open(file_name, "r") do |file|
source = ""
tmp_src = ""
counter = 0
file.each_line do |line|
scraped_content = line.gsub(/<.*?\/?>/, '')
tmp_src << scraped_content
if (counter % 10000) == 0
tmp_src = tmp_src.gsub( /\s{2,}/, "\n" )
source << tmp_src
tmp_src = ""
counter = 0
end
counter += 1
end
source << tmp_src.gsub( /\s{2,}/, "\n" )
out1.write(source)
break
end
end
end …
Run Code Online (Sandbox Code Playgroud) 我正在尝试运行异步进程,我不希望程序等到这些进程执行结束.我发现这个问题如何从Java程序中异步运行shell脚本,但它没有我想要的答案.
我正在做的是我只是运行bash进程,运行之后,我不希望Java程序等到它完成.这就是我所做的:
public void runCommandLine(String directory) throws IOException {
Thread commandLineThread = new Thread(() -> {
try {
ProcessBuilder processBuilder = new ProcessBuilder(
"/bin/bash");
processBuilder.directory(new File(directory));
Process process = processBuilder.start();
try (OutputStreamWriter osw = new OutputStreamWriter(process.getOutputStream())) {
osw.write(command);
}
printStream(process.getErrorStream(), true);
printStream(process.getInputStream(), true);
} catch (IOException ex) {
ex.printStackTrace();
}
});
commandLineThread.start();
System.out.println("Task Dispatched");
}
Run Code Online (Sandbox Code Playgroud)
我还在main方法的末尾添加了另一个打印输出,所以我得到了这个输出:
Task Dispatched
Task Dispatched
End of psvm
Run Code Online (Sandbox Code Playgroud)
但是,由于这两个进程尚未终止,程序不会终止.
我该如何解决这个问题?
我所做的只是将项目更新为Xcode 5.现在,当我尝试运行测试时,我收到此错误
if [ "${TEST_AFTER_BUILD}" = "YES" ]; then
Error ${LINENO} "RunUnitTests is obsolete. To run unit tests for your target, use the Test scheme action in the Xcode IDE and the test action in xcodebuild."
Note ${LINENO} "You can remove the Run Script build phase that invokes RunUnitTests from your unit test bundle target."
# Exit with EX_UNAVAILABLE to indicate that this subsystem is unavailable.
exit 69
fi
Run Code Online (Sandbox Code Playgroud)
哪里Error ${LINENO} "RunUnitTests is obsolete. To run unit tests for …
我正在做interviewstreet.com的样本测试.它有3个问题,这些是公开的.所以我认为讨论这些问题没什么坏处.
我的问题是
问题2/3(树的直径)树的直径是树中两个叶子之间最长路径上的节点数.下图显示了一个直径为9的树,形成最长路径末端的叶子是阴影的(请注意,每个长度为9的树中有多个路径,但没有超过九个节点的路径).
特别要注意树T的直径是以下数量中最大的:
给定树的根节点,返回树的直径
样本测试案例:
输入#00:考虑树:
输出#00:5
说明:树的直径为5
我在C++中的答案是:
int traverse(node* r) {
if (r == NULL) { return 0;}
return max(traverse(r->left),traverse(r->right))+1;
}
int diameterOfTree(node * r) {
return traverse(r->left)+traverse(r->right)+1;
}
Run Code Online (Sandbox Code Playgroud)
有14个测试用例,但其中有2个是错误的.我找不到我错过的案例.我真的不认为这是一个基本情况,所以我错过了什么?
c++ ×2
ios ×2
java ×2
objective-c ×2
algorithm ×1
appdelegate ×1
asynchronous ×1
c ×1
c# ×1
counter ×1
destructor ×1
for-loop ×1
frame ×1
java-8 ×1
java-stream ×1
loops ×1
memory ×1
process ×1
ruby ×1
tree ×1
uilabel ×1
unit-testing ×1
xcode ×1
xcode5 ×1