concept int in category ios

appears as: n Int, int
Anyone Can Create an App: Beginning iPhone and iPad programming

This is an excerpt from Manning's book Anyone Can Create an App: Beginning iPhone and iPad programming.

Some of that should look familiar to you. The let keyword indicates that you’re creating a constant, or a value that you can’t change once it’s set. You named the constant randomNumber and then set it equal to a random number that was cast to an Int. The (arc4random_uniform(6)) is a function that returns a random value between 0 and 5. In Swift, counting always starts with 0 when you’re programming, so the 6 is inclusive of 0 and 5.

The like label won’t work the same way as the description and the name. Do you know why? You defined the like property as an Int when you created the Lion object, and the label’s text field is expecting a string. You could go back and change the Lion object’s like type to a string if you wanted to, but I want it to stay as an Int. There are many ways to get the Int value into a string value, and some programmers have strong feelings about the proper way to do it. I’m of the opinion that if it works and it doesn’t break any coding conventions, then it’s considered proper.

Objective-C Fundamentals

This is an excerpt from Manning's book Objective-C Fundamentals.

Because Objective-C is a statically typed language, whenever you declare a variable, you must also specify the type of data you expect to store in it. As an example, the following variable declaration declares a variable named zombieCount, which is of type int:

int zombieCount;

Short for integer, int is a data type capable of storing a whole number between −2,147,483,648 and +2,147,483,647. In this chapter, you’ll discover many data types that can be used to store a wide range of real-world values. But before we dive in too far, let’s introduce the Rental Manager application that we build over the course of this book.

Figure 2.4. The benefit of using enumerations extends into the debugger. The variable pane displays the name associated with the current value instead of its raw integer value. Even though the variable currentHeading2 is assigned the value South (like currentHeading1), the debugger displays the value 10 due to its data type being int.

In the code snippet shown, the variables currentHeading1 and currentHeading2 are both set to the enumerated value South; the difference is that one variable is of type enum direction, whereas the other is of type int. As you can see, the debugger is able to determine if a variable is of an enumerated data type and will display the name associated with the variable’s current value rather than displaying the raw integer value. This can be an immense benefit during long debugging sessions when the enumeration values represent the state of the application.

To declare an integer variable, use the data type int (which is shorthand for integer), as demonstrated here:

int a;

By default, variables of type int are signed and can represent both positive and negative values. Sometimes you may want to restrict an integer variable to store only positive numbers. You can do this by adding the special qualifier unsigned before the data type, as shown in the following variable declaration:

The iPhone is a vast improvement over the hardware and memory constraints of a traditional cell phone, but it’s still constrained by real-world realities such as a fixed amount of memory being available to applications. In an ideal world, developers wouldn’t have any constraint on the value of integers—they could be infinitely high or low—but, unfortunately, constraints do exist. Declaring a variable of type int allocates a set amount of memory in which to store the value, and hence it can represent only a limited range of values. If you wanted to store infinitely large values, the variable would also require an infinite amount of memory, and that’s clearly not possible.

This is one reason for having unsigned and signed qualifiers. Using the unsigned qualifier trades off the ability to store negative numbers with the ability to double the range of positive values you can store in the same amount of memory. Other qualifiers include short and long, which can be added to an int data type to expand or contract the size of the variable. The most common sizes are listed in table 2.2.

Table 2.2. Common integer data types. Various modifiers can be used to alter the size of a variable and hence the valid range of values that can safely be stored in them.

Data type

Size (bits)

Unsigned range

Signed range

short int 16 0—65,535 −32,768—32,767
int 32 0—4,294,967,295 –2,147,483,648—2,147,483,647
long int 32 0—4,294,967,295 –2,147,483,648—2,147,483,647
long long int 64 0—(264 – 1) –263—(263 – 1)

In Objective-C, int is the default data type for variables and parameters. This means you can remove the keyword int from your variable declaration statement and, in most cases, it will still compile. Therefore, the following two variable declarations are equivalent:

unsigned a;
unsigned int a;

The first variable declaration implicitly implies the presence of the data type int by the presence of the unsigned qualifier. In the second declaration, the int data type is said to be explicitly specified because it’s present in the declaration.