2.1. Data model and types
Variables are used in Swift to name the local variables, arguments, and returns of a function. The outermost function in a
Swift script (akin to ‘‘main’’ in C) is unique only in that the variables in its environment can be declared ‘‘global’’ to make
them accessible to every other function in the script.
Each variable in a Swift script is declared to be of a specific type. The Swift type model is simple, with no concepts of
inheritance or abstraction. There are three basic classes of data types: primitive, mapped, and collection.
The four primary primitive types are integer, float, string, and boolean. Common operators are defined for primitive types,
such as arithmetic, concatenation, and explicit conversion. (An additional primitive type, ‘‘external,’’ is provided for manual
synchronization; we do not discuss this feature here.)
Mapped types are used to declare data elements that refer (through a process called ‘‘mapping,’’ described in Section 2.5)
to files external to the Swift script. These files can then be read and written by application programs called by Swift. The
mapping process can map single variables to single files, and structures and arrays to collections of files. (For example,
we might map the contents of a directory to a Swift array, D. We can then write ‘foreach f in D’ to operate on each file
in the directory.) The language has no built-in mapped types. Instead, users declare mapped type names to specify generic
or specific file types, as desired (for example, type file; type log;).
A variable that is declared to be a mapped file is associated with a mapper, which defines (often through a dynamic lookup
process) the file that is mapped to the variable.
Mapped type and collection type variable declarations can be annotated with a mapping descriptor that specifies the
file(s) to be mapped to the Swift data element(s).
For example, the following lines declare image to be a mapped file type and a variable named photo of type image. Since
image is a mapped file type, it additionally declares that the variable refers to a single file named shane.jpeg:
type image { };
image photo ;
2.1 . แบบจำลองข้อมูลและประเภทของตัวแปรที่ใช้ใน swift
ชื่อท้องถิ่นตัวแปรอาร์กิวเมนต์ และส่งกลับค่าของฟังก์ชัน ฟังก์ชันสุดใน
สคริปต์ Swift ( คล้ายกับ ' 'main ' ' C ) ที่เป็นเอกลักษณ์เฉพาะในตัวแปรของสภาพแวดล้อมสามารถประกาศ ' 'global ' ' เพื่อให้พวกเขาสามารถเข้าถึงทุกฟังก์ชั่นอื่น ๆ
ในสคริปต์แต่ละตัวแปรในสคริปต์ที่รวดเร็วถูกประกาศเป็นชนิดที่เฉพาะเจาะจง แบบจำลองชนิดรวดเร็วง่าย ไม่มีแนวคิด
มรดกหรือนามธรรม มีสามชั้นเรียนพื้นฐานของชนิดข้อมูล : ดั้งเดิม , แผนที่ , และคอลเลกชัน .
4 หลักดั้งเดิมชนิดจำนวนเต็ม , ลอย , สตริงและบูลีน . ผู้ประกอบการทั่วไปที่ถูกกำหนดแบบดั้งเดิม เช่น เลขเรียงต่อกัน
, ,และการเปลี่ยนแปลงที่ชัดเจน ( เพิ่มเติม 'external ดั้งเดิมชนิด ' ' ' มีไว้สำหรับคู่มือ
ตรงกัน เราไม่ได้หารือเกี่ยวกับคุณลักษณะนี้ ที่นี่
แมปประเภทใช้ประกาศข้อมูลองค์ประกอบที่อ้างถึง ( ผ่านกระบวนการที่เรียกว่า ' 'mapping ' ' อธิบายในส่วน 2.5 )
ไฟล์ภายนอกเพื่อสคริปต์ที่รวดเร็วไฟล์เหล่านี้แล้ว สามารถอ่านและเขียนโปรแกรมโดยการเรียกโดยรวดเร็ว
การทำแผนที่สามารถแผนที่ตัวแปรเดียวไฟล์เดียวและโครงสร้างและอาร์เรย์ไปยังคอลเลกชันของแฟ้ม ( ตัวอย่างเช่น
เราอาจด้านเนื้อหาของไดเรกทอรีที่จะอย่างรวดเร็ว เรย์ ดี. เราสามารถเขียน ' foreach f ใน d ' เพื่อใช้งานในแต่ละไฟล์
ในไดเรกทอรี ) ภาษาไม่มีตัวแมปประเภทแทนผู้ใช้ประกาศแมปชื่อประเภทระบุทั่วไป
หรือชนิดของแฟ้มที่เฉพาะเจาะจง ตามที่ต้องการ ( ตัวอย่างเช่น , ชนิดของไฟล์ ; บันทึก ; ชนิด ) .
ตัวแปรที่ถูกประกาศเป็นแมปแฟ้มที่เกี่ยวข้องกับ mapper ที่กำหนด ( มักจะผ่านกระบวนการค้นหา
Dynamic ) เป็นแมปไปยังแฟ้ม
ตัวแปรกำหนดประเภทและคอลเลกชันชนิดตัวแปร การประกาศที่สามารถแสดงด้วยแผนที่หัวเรื่องที่กำหนด
ไฟล์ ( s ) เป็นแมปไปยังข้อมูล swift องค์ประกอบ ( s )
ตัวอย่างเช่นบรรทัดต่อไปนี้ประกาศภาพเป็นแมปไฟล์ชนิดและตัวแปรชื่อภาพถ่ายประเภทภาพ ตั้งแต่
ภาพเป็นแมปไฟล์ประเภทมันยังประกาศว่า ตัวแปร หมายถึง ไฟล์เดียวที่ชื่อ เชนภาพประเภท JPEG :
{ } ;
ภาพ < " เชน JPEG " >
;
การแปล กรุณารอสักครู่..
