Listing 19-2. Using EnumsmachineState == State.Inactive ? println(

Listing 19-2. Using EnumsmachineSta

Listing 19-2. Using Enums
machineState == State.Inactive ? println("Machine Inactive") :
println("Machine State Unknown")
In Listing 19-2, the text “Machine Inactive” will appear since the value of
machineState is State.Inactive.
Enum types can be defined as their own value types, or you can assign
other types to an enumeration, including integers, strings, and floating-point
numbers.
To assign another type to an enumeration, you must specify the enum type
and then use the assignment operator to assign the values. For instance, if
you wanted to define an enumeration for the alphabet while keeping track
of the position of each letter in the alphabet, you could use the code in
Listing 19-3.
Listing 19-3. Integer Enum
enum Alphabet:Int{
case A = 1
case B, C, D, E, F, G, H, I
case J, K, L, M, N, O, P, Q
case R, S, T, U, V, W, X, Y, Z
}
In Listing 19-3, you defined the enum type Alphabet as an Int. This means
each Alphabet value has a corresponding integer value. You also need to
provide each enum value with a corresponding integer value. You did this
for case A, which has a value of 1. Since this is an integer type, Swift can
figure out that you want the remaining enum values to correspond to integer
values in the order you listed them.
While you would use Alphabet in the same way as State, with Alphabet you
can reference the raw integer value using the toRaw() function.
For instance, if you want to add the integer value of A to the integer value
of B, you would use the code in Listing 19-4.
Listing 19-4. Accessing Raw Enum Values
let result = Alphabet.A.toRaw() + Alphabet.B.toRaw()
toRaw() is a function. See Chapter 27 for a detailed example of Swift
functions.
0/5000
จาก: -
เป็น: -
ผลลัพธ์ (ไทย) 1: [สำเนา]
คัดลอก!
Listing 19-2. Using EnumsmachineState == State.Inactive ? println("Machine Inactive") :println("Machine State Unknown")In Listing 19-2, the text “Machine Inactive” will appear since the value ofmachineState is State.Inactive.Enum types can be defined as their own value types, or you can assignother types to an enumeration, including integers, strings, and floating-pointnumbers.To assign another type to an enumeration, you must specify the enum typeand then use the assignment operator to assign the values. For instance, ifyou wanted to define an enumeration for the alphabet while keeping trackof the position of each letter in the alphabet, you could use the code inListing 19-3.Listing 19-3. Integer Enumenum Alphabet:Int{case A = 1case B, C, D, E, F, G, H, Icase J, K, L, M, N, O, P, Qcase R, S, T, U, V, W, X, Y, Z}In Listing 19-3, you defined the enum type Alphabet as an Int. This meanseach Alphabet value has a corresponding integer value. You also need toprovide each enum value with a corresponding integer value. You did thisfor case A, which has a value of 1. Since this is an integer type, Swift canfigure out that you want the remaining enum values to correspond to integervalues in the order you listed them.While you would use Alphabet in the same way as State, with Alphabet youcan reference the raw integer value using the toRaw() function.For instance, if you want to add the integer value of A to the integer valueof B, you would use the code in Listing 19-4.
Listing 19-4. Accessing Raw Enum Values
let result = Alphabet.A.toRaw() + Alphabet.B.toRaw()
toRaw() is a function. See Chapter 27 for a detailed example of Swift
functions.
การแปล กรุณารอสักครู่..
ผลลัพธ์ (ไทย) 3:[สำเนา]
คัดลอก!
รายการ 19-2 . การใช้ enums
machinestate = = รัฐ ใช้งาน ? println ( " เครื่องจักรงาน println ( " " ) :

สภาพเครื่องที่ไม่รู้จัก " ) ในรายการ 19-2 ข้อความ " เครื่องไม่ได้ใช้งาน " จะปรากฏขึ้นเนื่องจากค่า

machinestate คือรัฐ ไม่ได้ใช้งาน ประเภทนัมสามารถกําหนดเป็นประเภทตามตัวอักษรของตนเอง หรือคุณสามารถกำหนดประเภทอื่นๆ ที่แจง
, ได้แก่ จำนวนเต็ม , สตริง , จุด -

และตัวเลขเพื่อกำหนดชนิดอื่นที่มีการ คุณจะต้องระบุประเภทของนัม
แล้วใช้งานโอเปอเรเตอร์กำหนดค่า ตัวอย่างเช่นถ้าคุณต้องการที่จะมีการกำหนด

สำหรับตัวอักษรในขณะที่การติดตามของตำแหน่งของแต่ละตัวอักษร คุณสามารถใช้รหัสในรายการ 19-3
.
รายการ 19-3 . จำนวนเต็มนัมนัมตัวอักษร int {

: A = 1
B , C , D , E , F , G , H ,
กรณี J , K , L , M , N , O , P , Q
กรณี R , S , T , U , V , W , X , Y , Z
}
ในรายการ 19-3 คุณกำหนดนัมชนิดตัวอักษรเป็น int ซึ่งหมายความว่า
แต่ละตัวอักษรคุณค่าจะมีค่าจำนวนเต็มที่สอดคล้องกัน คุณต้อง
ให้แต่ละนัมค่าด้วยค่าจำนวนเต็มที่สอดคล้องกัน คุณ
สำหรับกรณี ซึ่งมีมูลค่า 1 ตั้งแต่นี้เป็นจำนวนเต็มชนิดรวดเร็วสามารถ
คิดออกว่าคุณต้องการที่เหลือนัมค่านิยมให้สอดคล้องกับค่าจำนวนเต็ม
ในลำดับที่คุณระบุไว้พวกเขา .
ในขณะที่คุณจะใช้ตัวอักษรในแบบเดียวกับรัฐ กับตัวอักษรที่คุณ
สามารถอ้างอิงค่าจำนวนเต็มดิบโดยใช้ฟังก์ชั่น toraw() .
ตัวอย่างเช่นหากคุณต้องการเพิ่มค่าจำนวนเต็มของ ค่าจำนวนเต็ม
B คุณจะใช้รหัสในรายการ 19-4 .
รายการ 19-4 .การเข้าถึงดิบนัมค่า
ให้ผล = ตัวอักษร . . toraw() ตัวอักษร B . toraw()
toraw() เป็นฟังก์ชัน เห็นบทที่ 27 ตัวอย่างรายละเอียดของการทำงานที่รวดเร็ว

การแปล กรุณารอสักครู่..
 
ภาษาอื่น ๆ
การสนับสนุนเครื่องมือแปลภาษา: กรีก, กันนาดา, กาลิเชียน, คลิงออน, คอร์สิกา, คาซัค, คาตาลัน, คินยารวันดา, คีร์กิซ, คุชราต, จอร์เจีย, จีน, จีนดั้งเดิม, ชวา, ชิเชวา, ซามัว, ซีบัวโน, ซุนดา, ซูลู, ญี่ปุ่น, ดัตช์, ตรวจหาภาษา, ตุรกี, ทมิฬ, ทาจิก, ทาทาร์, นอร์เวย์, บอสเนีย, บัลแกเรีย, บาสก์, ปัญจาป, ฝรั่งเศส, พาชตู, ฟริเชียน, ฟินแลนด์, ฟิลิปปินส์, ภาษาอินโดนีเซี, มองโกเลีย, มัลทีส, มาซีโดเนีย, มาราฐี, มาลากาซี, มาลายาลัม, มาเลย์, ม้ง, ยิดดิช, ยูเครน, รัสเซีย, ละติน, ลักเซมเบิร์ก, ลัตเวีย, ลาว, ลิทัวเนีย, สวาฮิลี, สวีเดน, สิงหล, สินธี, สเปน, สโลวัก, สโลวีเนีย, อังกฤษ, อัมฮาริก, อาร์เซอร์ไบจัน, อาร์เมเนีย, อาหรับ, อิกโบ, อิตาลี, อุยกูร์, อุสเบกิสถาน, อูรดู, ฮังการี, ฮัวซา, ฮาวาย, ฮินดี, ฮีบรู, เกลิกสกอต, เกาหลี, เขมร, เคิร์ด, เช็ก, เซอร์เบียน, เซโซโท, เดนมาร์ก, เตลูกู, เติร์กเมน, เนปาล, เบงกอล, เบลารุส, เปอร์เซีย, เมารี, เมียนมา (พม่า), เยอรมัน, เวลส์, เวียดนาม, เอสเปอแรนโต, เอสโทเนีย, เฮติครีโอล, แอฟริกา, แอลเบเนีย, โคซา, โครเอเชีย, โชนา, โซมาลี, โปรตุเกส, โปแลนด์, โยรูบา, โรมาเนีย, โอเดีย (โอริยา), ไทย, ไอซ์แลนด์, ไอร์แลนด์, การแปลภาษา.

Copyright ©2024 I Love Translation. All reserved.

E-mail: