我想知道为什么下面的Haxe代码编译:
class Test<T> { // 1
var l:List<Dynamic> = new List<Dynamic>(); // 2
public function new() {} // 3
public function add(d:Dynamic):Void { l.add(d); } // 4
public function get():T { return l.pop(); } // 5
public static function main() { // 6
var t:Test<Int> = new Test<Int>(); // 7
t.add("-"); // 8
trace(t.get()); // 9
} // 10
}
Run Code Online (Sandbox Code Playgroud)
我看到的编译问题:
如第1行所示,此类具有类型参数T.在第7行中,指定T为a Int.所以get()函数(第5行)应该只返回一个Int.但是 - 神奇地 - 这个函数返回一个String(第9行).
所以...为什么编译器不抱怨这个?
我有一个 JSON 文件,我想做的是获取这个特定字段“_id”。问题是当我使用json.load('input_file'),它说我的变量 data是一个列表,而不是字典,所以我不能做类似的事情:
for value in data['_id']:
print(data['_id'][i])
Run Code Online (Sandbox Code Playgroud)
因为我不断收到此错误:TypeError: listindexsmust beintegers or slices, not str
我还尝试做的是:
data = json.load(input_file)[0]
Run Code Online (Sandbox Code Playgroud)
这有点管用。现在,我的类型是一本字典,我可以这样访问:data['_id']
但我只从存档中获取第一个“_id”...
所以,我想做的是将所有 '_id' 的值添加到列表中,以供稍后使用。
input_file = open('input_file.txt')
data = json.load(input_file)[0]
print(data['_id'])# only shows me the first '_id' value
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!
[{
"_id": "5436e3abbae478396759f0cf",
"name": "ISIC_0000000",
"updated": "2015-02-23T02:48:17.495000+00:00"
},
{
"_id": "5436e3acbae478396759f0d1",
"name": "ISIC_0000001",
"updated": "2015-02-23T02:48:27.455000+00:00"
},
{
"_id": "5436e3acbae478396759f0d3",
"name": "ISIC_0000002",
"updated": "2015-02-23T02:48:37.249000+00:00"
},
{
"_id": "5436e3acbae478396759f0d5",
"name": "ISIC_0000003",
"updated": "2015-02-23T02:48:46.021000+00:00"
}]
Run Code Online (Sandbox Code Playgroud) 假设我们有一个多行字符串,比如
var s:String = "my first line\nmy second line\nmy third line\nand so on!";
Run Code Online (Sandbox Code Playgroud)
在Haxe中获取(仅)此字符串的第一行的最佳方法是什么?我知道我可以这样做:
static function getFirstLine(s:String):String {
var t:String = s.split("\n")[0];
if(t.charAt(t.length - 1) == "\r") {
t = t.substring(0, t.length - 1);
}
return t;
}
Run Code Online (Sandbox Code Playgroud)
但是我想知道是否有更简单的(预定义)方法...
我将手机联系人收到列表<>并将其保存在数据库中.以下是我的代码.
这是我获取contacts-List的方法
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
try {
SetContentView(Resource.Layout.Main);
TextView txtcount = this.FindViewById<TextView>(Resource.Id.textView1);
List<PersonContact> a1 = GetPhoneContacts();
Phone gp = new Phone();
gp.insertContact(a1);
} catch (System.Exception ex) {
alert(ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
通过以下方法,我试图将联系人存储在数据库中
[WebMethod]
public string insertContact<T>(List<PersonContact> a) {
OpenConnection();
if (a.Count > 0) {
for (int i = 0; i < a.Count; i++) {
string str = "insert into phone_contact (FirstName,LastName,PhoneNumber)values('" + a[i].FirstName + "','" + a[i].LastName + "','" + a[i].PhoneNumber + "')";
SqlCommand cmd = new …Run Code Online (Sandbox Code Playgroud)