小编ewo*_*wok的帖子

检测导航栏上按下"后退"按钮的时间

我需要在导航栏上按下后退按钮(返回上一屏幕,返回父视图)按钮时执行一些操作.

是否有一些方法可以实现捕获事件并触发一些操作以在屏幕消失之前暂停和保存数据?

iphone xcode objective-c ios

120
推荐指数
8
解决办法
12万
查看次数

sqlite中有自动增量吗?

我试图primary keySqlite3中创建一个自动递增的.我不确定这是否真的可行,但我希望只需要指定其他字段.

例如:

CREATE TABLE people (id integer primary key auto increment, first_name varchar(20), last_name varchar(20));
Run Code Online (Sandbox Code Playgroud)

然后,当我添加一个值时,我希望只需要这样做:

INSERT INTO people
VALUES ("John", "Smith");
Run Code Online (Sandbox Code Playgroud)

这甚至可能吗?

我在Windows 7 sqlite3下运行cygwin.

sql sqlite cygwin

97
推荐指数
7
解决办法
13万
查看次数

如何删除lxml中的元素

我需要使用python的lxml基于属性的内容完全删除元素.例:

import lxml.etree as et

xml="""
<groceries>
  <fruit state="rotten">apple</fruit>
  <fruit state="fresh">pear</fruit>
  <fruit state="fresh">starfruit</fruit>
  <fruit state="rotten">mango</fruit>
  <fruit state="fresh">peach</fruit>
</groceries>
"""

tree=et.fromstring(xml)

for bad in tree.xpath("//fruit[@state=\'rotten\']"):
  #remove this element from the tree

print et.tostring(tree, pretty_print=True)
Run Code Online (Sandbox Code Playgroud)

我想要打印:

<groceries>
  <fruit state="fresh">pear</fruit>
  <fruit state="fresh">starfruit</fruit>
  <fruit state="fresh">peach</fruit>
</groceries>
Run Code Online (Sandbox Code Playgroud)

有没有办法在不存储临时变量并手动打印的情况下执行此操作,如下所示:

newxml="<groceries>\n"
for elt in tree.xpath('//fruit[@state=\'fresh\']'):
  newxml+=et.tostring(elt)

newxml+="</groceries>"
Run Code Online (Sandbox Code Playgroud)

python xml lxml

79
推荐指数
4
解决办法
5万
查看次数

如何使用python的urllib设置标头?

我对python的urllib很新.我需要做的是为发送到服务器的请求设置自定义标头.具体来说,我需要设置Content-type和Authorizations标头.我查看了python文档,但是我找不到它.

python header http urllib

65
推荐指数
3
解决办法
12万
查看次数

存储对特定文件的更改

我有一个很大的git项目,我愚蠢地导入到eclipse并运行autoformat.现在,项目中的每个文件都显示为已修改.而不是提交我的格式化文件,我宁愿还原我只格式化的所有文件,而不是其他更改.例如:

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#   (commit or discard the untracked or modified content in submodules)

#     modified: dir/file1.cpp
#     modified: dir/file1.h
#     modified: dir/file2.cpp
#     modified: dir/file2.h
#     modified: dir/file3.cpp
#     modified: dir/file3.h
#     modified: dir/file4.cpp
#     modified: dir/file4.h
Run Code Online (Sandbox Code Playgroud)

我知道file2.cpp,file2.hfile3.cpp已与内容修改(即,不只是格式化).我想隐藏对这三个文件的更改,然后签出一个旧版本,以便我可以在以后重新应用这些文件的更改.我宁愿避免这样的事情:

$ …
Run Code Online (Sandbox Code Playgroud)

git git-stash

45
推荐指数
4
解决办法
4万
查看次数

Java:重载构造函数之间的选择

根据这个问题,当尝试在不明确的重载构造函数之间进行选择时,Java将选择"最具体"的选项.在这个例子中:

public class Test{
    private Test(Map map){
        System.out.println("Map");
    }
    private Test(Object o){
        System.out.println("Object");
    }
    public static void main(String[] args){
        new Test(null);
    }
}
Run Code Online (Sandbox Code Playgroud)

它会打印出来

"地图"

但是,我试图找出"最具体"的含义.我认为这意味着"最不模糊",如"可能指的是最不可能的类型".在这种情况下,Object可能是任何不是原始的东西,而Map可能只是Map? extends Map.基本上,我假设选择了接近继承树的叶子的类.当一个类是另一个类的子类时,这是有效的:

public class Test{
    private Test(A a){
        System.out.println("A");
    }
    private Test(B b){
        System.out.println("B");
    }
    public static void main(String[] args){
        new Test(null);
    }
}

class A{}

class B extends A{}
Run Code Online (Sandbox Code Playgroud)

"B"

然后我想出了这个:

public class Test{
    private Test(A a){
        System.out.println("A");
    }
    private Test(E e){
        System.out.println("E"); …
Run Code Online (Sandbox Code Playgroud)

java compiler-errors ambiguous-call

35
推荐指数
3
解决办法
1203
查看次数

如果没有记录,如何选择sum -or- 0?

我需要编写一个查询,返回满足特定条件的所有值的总和,但如果没有找到行,查询需要返回0,而不是null.例如:

tab    
+---------------+-----+
| descr         | num |
+---------------+-----+
| hello there   | 5   |
| hi there      | 10  |
| hello         | 10  |
| hi there!     | 15  |
+---------------+-----+
Run Code Online (Sandbox Code Playgroud)

这个查询:

SELECT sum(num) AS val FROM tab WHERE descr LIKE "%hello%";
Run Code Online (Sandbox Code Playgroud)

应该而且确实会回来15.然而:

SELECT sum(num) AS val FROM tab WHERE descr LIKE "%greetings%";
Run Code Online (Sandbox Code Playgroud)

应该返回0,但确实返回null.

有人可以解释一下这是否可能?

mysql sum

34
推荐指数
3
解决办法
4万
查看次数

lua:检索表中的键列表

我需要知道如何在lua中检索表的键集.例如,如果我有下表:

tab = {}
tab[1]='a'
tab[2]='b'
tab[5]='e'
Run Code Online (Sandbox Code Playgroud)

我想要检索一个如下所示的表:

keyset = {1,2,5}
Run Code Online (Sandbox Code Playgroud)

lua keyset lua-table

33
推荐指数
2
解决办法
5万
查看次数

c ++ operator <<的多个定义

我试图覆盖<<一个类的运算符.目的基本上是toString()为我的类实现类似的行为,因此发送它将cout产生有用的输出.使用一个虚拟示例,我有下面的代码.当我尝试编译时,我得到了一个愚蠢的错误:

$ g++ main.cpp Rectangle.cpp
/tmp/ccWs2n6V.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CRectangle const&)':
Rectangle.cpp:(.text+0x0): multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CRectangle const&)'
/tmp/ccLU2LLE.o:main.cpp:(.text+0x0): first defined here
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚为什么会这样.我的代码如下:

Rectangle.h:

#include <iostream>
using namespace std;

class CRectangle {
    private:
        int x, y;
        friend ostream& operator<<(ostream& out, const CRectangle& r);
    public:
        void set_values (int,int);
        int area ();
};

ostream& operator<<(ostream& out, const CRectangle& r){
    return out << "Rectangle: " << r.x << ", " << r.y;
} …
Run Code Online (Sandbox Code Playgroud)

c++ overriding iostream operator-keyword

27
推荐指数
2
解决办法
2万
查看次数

在java中切换类型

在开始之前,我知道这个问题有很多答案可以提出替代方法.我正在寻求这种特殊方法的帮助,以确定是否可行,如果不可能,可能会有类似的方法.

我有一个方法,它接受一个超类并根据传递的对象的类型调用一个方法.例如:

public void handle(Object o){
  if (o instanceof A)
    handleA((A)o);
  else if (o instanceof B)
    handleB((B)o);
  else if (o instanceof C)
    handleC((C)o);
  else 
    handleUnknown(o);
Run Code Online (Sandbox Code Playgroud)

我不能修改子类型来覆盖一个handle()方法,正如这个答案所暗示的那样,因为我不拥有这些类.所以这个instanceof方法就是我所拥有的.

我想用一个switch声明代替if/else,只是因为它更整洁.我知道你只能打开基元和字符串,所以我要切换类名:

switch(o.getClass().getCanonicalName()){
case "my.package.A":
  handleA((A)o);
  break;
case "my.package.B":
  handleB((B)o);
  break;
case "my.package.C":
  handleC((C)o);
  break;
default:
  handleUnknown(o);
  break;
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是规范名称非常长(如12个子包),我不能调用ClassName.class.getCanonicalName()case语句,因为Java不允许这样做.所以我的下一个解决方案是Enum.这是我遇到问题的地方.

我希望我的代码看起来像这样:

public enum Classes {
  A (A.getCanonicalName()),
  B (B.getCanonicalName()),
  C (C.getCanonicalName());
}

switch (o.getClass().getCanonicalName()){
case Classes.A:
  handleA((A)o);
  break;
case Classes.B:
  handleB((B)o);
  break;
case …
Run Code Online (Sandbox Code Playgroud)

java enums types instanceof switch-statement

27
推荐指数
3
解决办法
5万
查看次数