2010年8月19日 星期四

ArrayElementType Metadata tag


原文:
Using Flex4說明手冊中關於ArrayElementType的說明:
When you define an Array variable in ActionScript, you specify Array as the data type of the variable. However, you cannot specify the data type of the elements of the Array.

當你於ActionScript中定義一個Array變數,你指定Array為資料型態,然而你無法指定Array內項目的資料型態。
(在AS3中Vector可以指定陣列中項目的資料型態,Array本身沒有此項功能)

功用:
To allow the Flex MXML compiler to perform type checking on Array elements, you can use the [ArrayElementType] metadata tag to specify the allowed data type of the Array elements, as the following example shows:

你可以藉由ArrayElementType這個metadata tag讓MXML compiler來檢查Array的項目元素是否為ArrayElementType指定資料格式的項目。

使用範例:
//Class
package com
{ 
 import spark.components.Group;
 public class myGroup extends Group
 {
  public function myGroup()
  {
   super(); 
  } 
  //標籤宣告
  [ArrayElementType("Number")]
  public var myArray:Array; 
 }
}

//mxml檔案中
<com:myGroup id="mg">
<com:myArray>
<fx:int>1.0</fx:int>   //此處會有編譯錯誤警告,因為使用<int>
</com:myArray>
</com:myGroup>   
note:
在mxml中若是對myArray使用非Number的項目就會出像錯誤警告,但是此項功能僅對於MXML標籤有作用,若你使用ActionScript來對myArray來操作,並加入非Number資料型態的項目並不會出像編譯警告。
  •  ArrayElementType("Number")]的宣告對ActionScript直接操作是無效的
//在application Complete事件發生後去操作myArray,並給予非Number的項目,是可以通過編譯的
protected function application1_applicationCompleteHandler(event:FlexEvent):void 
{
 mg.myArray.push( "test" );
}

沒有留言:

張貼留言