About the book sample code: Meaning bound, meaning [xxxx]()

Asked 2 years ago, Updated 2 years ago, 33 views

Please let me know two things about the sample code of the book.
Reference: iPhone/iPad Programming Bible: Eiichi Fudurukawa

// Creating a puzzle game
classViewController:UIViewController {
   let BTN_START = 0
   letsCREEN=UISscreen.mainScreen().bounds.size
   // →Q1 "What grammar and what are you trying to output in this sentence?"

   var_GAMEVIEW —UIView?
   var_TITLELABEL:UILabel?
   var_piece = [UIImageView]()
   var_data=[Int]()
   // → Q2 "What does [xxxx]() in the above two sentences mean (grammar)?
   var_SHUFFLE —Int=0

// Continued…………

swift

2022-09-30 17:13

2 Answers

letsCREEN=UISscreen.mainScreen().bounds.size
// →Q1 "What grammar and what are you trying to output in this sentence?"

The main focus of this question is not in the meaning of bound.Bounds means UISscreen properties and coordinates around the screen, but for more information, search the UISscreen class reference (accessed from the Xcode help window) or search the Internet using keywords such as UISscreen bound.
The main focus of this question is on three dots.I don't think many proficient programmers care, but all three have different meanings.First, let's divide this line into three lines.

let screen:UISscreen=UISscreen.mainScreen()
let screenRectangle —CGRect=screen.bounds
let screenSize —CGSize=screenRectangle.size

The dot (.) on the first line connects Receiver (class or instance) with Method (method).It means to execute the method to Receiver.Here we instruct the UISscreen class to return the main screen instance.PC and Mac can connect multiple displays and get the main screen is easy to imagine, but iPhone/iPad may be difficult, but iPhone/iPad may be used as a controller for video games.If you use it that way, it's a multi-screen environment.

The dot in the second line means accessing the property bound of the screen instance.The property returns a value similar to the method.bound returns a value of type CGRect.
The dot in the third line accesses the property size of the Structure CGRect.Access to properties is common, so there is no significant difference from the second line.The property size returns the CGSize type.

From the above, the answer to Q1 is "Use Dot Syntax to print the screen size in CGSize type."

 var_piece=[UIImageView]()
var_data=[Int]()
// → Q2 "What does [xxxx]() in the above two sentences mean (grammar)?

Swift uses parentheses ([]) for Array and Dictionary.
In C language, the array declaration is

interray[];

Let's say.If you make a similar declaration on Swift,

var array: Int

is the case.The type of element in an array is enclosed in parentheses and is the type of array.
In Swift, the object (which is an expression that includes classes, instances, structures, enumerators, arrays, dictionaries, strings, integers, real numbers, etc.) initiators (I think Java calls constructors).

Type name ( )

in the format

// Initiate Integer Int
value = Int()
// String Initiative
var string = String()

So the answer to Q2 is

 var_piece=[UIImageView]()
// Array Initiator with UIImageView type as an element
var_data=[Int]()
// Initiator for array with Int type as an element

is the case.
By the way, if you leave the parentheses empty, you will generate an array of 0 elements (empty).


2022-09-30 17:13

A1:letsCREEN=UISscreen.mainScreen().bounds.size Meaning

replacing the terminal screen size with the variable sCREEN

*UISscreen.mainScreen() determines that the object that can be retrieved is the object on the terminal screen

A2:[xxxx]( ) Meaning

xxxx creating an empty array of types

Write "xxxx array and initialize with empty array"

 variable array: [xxxx] = [ ] // array is any variable name

"If you write like this, ""Create a variable with an undecided type and initialize with an empty array of xxxx types"""

"
var array=[xxxx]()

As a result, both arrays of type xxxx are created similarly


2022-09-30 17:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.