简体   繁体   中英

Swift - static let and “<<” meaning

I have the following code

struct Physics {
    static let smallCoin : UInt32 = 0x1 << 1
    static let smallCoin2 : UInt32 = 0x1 << 2
    static let ground : UInt32 = 0x1 << 3
}

I would like to know the meaning of

  • static let
  • UInt32 = 0x1 << 3

<< is the left shift operator. You can better visualize it in binary:

   1        0000 0001
<< 1                ^ shift this one bit to the left
----  =     ---------
   2        0000 0010


   1        0000 0001
<< 2                ^ shift this two bits to the left
----  =     ---------
   4        0000 0100


   3        0000 0011
<< 2                ^ shift this two bits to the left
----  =     ---------
  12        0000 1100

Another property to remember is x << n = x * (2^n) . The opposite of << is >> - the right shift operator.

It looks like the question might come from a need to have some constants for a physics simulation. The Integer might represent the attraction force (or similar) or a set of items in a world. Which would be nice.

Which brings us onto the first part of the question:

Static let There aa few reasons why you might use static let for properties:

  • Use static properties for Constants, and similar configuration
  • Use static properties for expensive objects, so you only have to create an instance once for the type rather than creating an instance once for the type rather than for each instance
  • A factory can use static methods to create complex objects

We are going to focus on the first of these.

The question is about a stuct and this struct could well represent something like Constants. I have this in many of my projects, although I generally use an enum rather than the suggested struct , in the following kind of format

struct Constants {
    static let offset = 10
}

enum {
    static let offset = 10
}

for the difference between struct and enum take a look at this article

which leads us to the question where we have a value for smallCoin, smallCoin2 and ground represented as a static let within the struct .

UInt32 = 0x1 << 3 The type given (of my presumption that this is a Constant). The type given here is UInt32 which means that the value is an unsigned Integer , and then this value is given as 0x1 << 1 , 0x1 << 2 or 0x1 << 3 depending on the property we are referring to.

To understand this, we need to recognize that 0x1 is hexadecimal , and represents the denery value of 1. From there, we are using the https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html :

在此处输入图片说明

The initial value of 1, bitshift by 1 creates the value 2. The initial value of 1, bitshift by 2 creates the value 4. The initial value of 1, bitshift by 3 creates the value 8.

Now why we would need to bitshift a single value like this it is not entirely clear.

As a result, Physics.smallCoin = 2, Physics.smallCoin2 = 4 and Physics.ground = 8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM