【问题标题】:Empty array after adding elements using Titanium Appcelerator mobile 1.7.2使用 Titanium Appcelerator mobile 1.7.2 添加元素后的空数组
【发布时间】:2011-10-17 14:13:12
【问题描述】:

我正在使用 Titanium Appcelerator 移动 API 1.7.2。

创建数组时,我得到了一些奇怪的结果。是我的语法吗?

container.textBoxArray = new Array();
container.textBoxArray[0] = createPasswordTextField(options, '0%');
container.textBoxArray[1] = createPasswordTextField(options, '25%');
Ti.API.log(container.textBoxArray == null);
Ti.API.log('len: ' + container.textBoxArray.length);

输出的结果分别为0(表示假)和'len:0'。有人知道为什么吗?

亚当

编辑: createPasswordTextField 本质上是

function createPasswordTextField(options, left){
    return Ti.UI.createTextField( options... )
}

【问题讨论】:

  • 事实证明:container.textBoxArray = [createPasswordTextField(options, '0%'), createPasswordTextField(options, '25%'), createPasswordTextField(options, '50%'), createPasswordTextField(options, '75%')]; 成功创建了数组。是 Ti.API.log() 崩溃了,所以我评论了它们,然后......它有效吗?我不懂这个平台。

标签: javascript arrays titanium appcelerator


【解决方案1】:

我也遇到了 Titanium 和 Arrays 的问题。你能做的就是试试这个:

container.textBoxArray = [];
container.textBoxArray.push(createPasswordTextField(options, '0%'));
container.textBoxArray.push(createPasswordTextField(options, '25%'));
Ti.API.log(container.textBoxArray == null);
Ti.API.log('len: ' + container.textBoxArray.length);

【讨论】:

  • 这是我以前的语法,它可以工作。但是,当我稍后尝试访问该数组时,内容就消失了。我想这可能是内存寿命问题,所以我尝试了不同的语法。
【解决方案2】:

查看数组内容的日志语句应如下所示

Ti.API.log(JSON.stringify(container.textBoxArray));

【讨论】:

    【解决方案3】:

    我也遇到过。将数组添加到 TiProxy 对象(视图、窗口、按钮等)时,它不会按预期工作。您需要操作“关闭”代理的数组,然后重新设置它。我不知道这是错误还是 TiProxy 对象的属性限制。下面是一个在 iOS 上在 Titanium Mobile SDK 1.7.5 下表现相同的示例:

    var proxy = Ti.UI.createView();  //this can be any TiProxy object
    
    proxy.someArray = [];
    proxy.someArray.push( '1' );
    proxy.someArray.push( '2' );
    Ti.API.info("Array modified directly on TiProxy object" );
    Ti.API.info(proxy.someArray );
    
    var myArray = [];
    myArray.push( '1' );
    myArray.push( '2' );
    proxy.someArray = myArray;
    Ti.API.info("Array modified outside TiProxy object" );
    Ti.API.info( proxy.someArray );
    
    proxy.someArray.push( '3' );
    Ti.API.info("This will be unchanged" );
    Ti.API.info(proxy.someArray );
    
    var changeArray = proxy.someArray;
    changeArray.push('3');
    proxy.someArray = changeArray;
    Ti.API.info("This is how you must do it." );
    Ti.API.info(proxy.someArray );
    

    返回:

    [INFO] Array modified directly on TiProxy object
    [INFO] []
    [INFO] Array modified outside TiProxy object
    [INFO] [ 1,  2 ]
    [INFO] This will be unchanged
    [INFO] [ 1,  2 ]
    [INFO] This is how you must do it.
    [INFO] [ 1, 2, 3 ]
    

    找出 Android 上的行为要困难得多,因为 Ti.API.info(proxy.someArray ); 只返回一个对象引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多