iOS development Getting Started tutorial (i)

Source: Internet
Author: User

Introduction to OC Syntax (i) data types and operators

1.1 Notes
Comments, like other languages, allow single-line, multiline comments, and a canonical code that requires some formal comments, as follows:
/*
This is a multi-line
Comments
*/
This is a multi-line comment
Nested annotations are not supported by the OC language for the time being. (This feature is supported in Swift)

1.2 Identifiers and variables

1.2.1 Delimiter
1. Semicolon
Each sentence ends with a semicolon, a line can have multiple statements, and a statement can span multiple lines, but variable names cannot be separated
Legal:
NSString *name = @ "Hello"; int age = 20;
NSString * Hello = [@ "Good" stringbyappendingstring:
@ "I also good"];
Not valid:
NSString *my
name [email protected] "Zhang San";

2. Curly Braces
{} appears in pairs, used in functions, if statements, switch statements, ect.
3. Square brackets
Square brackets are used inside the array, inside the dictionary, and inside the method call.
Such as:
arr[2]=2;
dict[@ "id"] = 2;
NSString *string = @ "haha";
[Stirng length];

4. Parentheses
Parameter passing, and so on.
5. Spaces
Space. The division between Word and Word
6. Polka Dots
The invocation of variables, etc.

1.1.2 Designator Rules
The Object_c language identifier must be either a letter underline or a dollar sign, followed by an alphanumeric or an underscore, and a dollar sign.
1. Identifiers are case-sensitive
2. Identifiers cannot be keywords, but can contain keywords
3. Identifiers cannot contain spaces
4. Identifiers can only have US $ $ sign, cannot have other symbols

1.1.3 Keywords
The keyword will render a specific color in Xcode. Here are examples of commonly used keywords
Auto break case Char const continue default do double else enum
extern float for goto if int long register return short signed sizeof
Static struct switch typedef union unsigned void volatile while

1.2 Basic data types
1.2.1. Shaping
1.2.2.NSLog output format
D with signed decimal output
o Octal unsigned output
X hex Unsigned output
u unsigned decimal output integer
C with character output, only one character is output
s output C-style string
F Output in decimal form with 6-bit digits by default
E output floating-point numbers as an exponent, with the number part default output 6 bits
g automatically selects%e or%f output, and does not output meaningless 0
P The address of the hex output pointer
@ Output Object-c Object

1.2.3. Character type
1. Commonly used character type
\b Backspace
\ n line break
\ r return character
\ t tab
\ "Double quotation marks
\ ' Single quotation mark
\ \ counter Slash
2. Note: When developing in Xcode, you must use the escape character or you will get an error.

1.2.4 Floating Point type
Floating-point numbers include decimal, as well as in the form of scientific calculation methods. such as 12.34, 5E2 and so on.
Note: Dividing the floating-point number by 0.0 will get positive infinity or negative infinity, and dividing the integer by 0.0 to get the boundary value of the integer range, such as 10/0.0=2147483647
-3/0.0 =-2147483647
1.2.5 Enumeration
Enumerations are used more in OBJECT-C development.
Directly on the code
Enum Animals{dog=2,cat=1,pig,cow,hen};
Define an enumeration variable
Enum Animals Mycat,mydog;
Mycat = cat;
Mydog = dog;
NSLog (@ "%d and%d", mycat,dog)//output: 1 and 2

To define an anonymous enumeration variable
enum {male, famale}me,you;
me = male;
you = 1;

1.2.6 Boolean
The bottom of the object-c is actually 0 and 1 for the two underlying values of bool and yes and no with signed Char
The system header file is defined as follows:
typedef signed Char BOOL;
#if!defined (YES)
#define YES (BOOL) 1;
#endif
#if!defined (YES)
#define NO (BOOL) 0;
#endif
can be assigned to bool type
BOOL B1 = 1;
BOOL b2 = 100;

Error
BOOL B3 = 256;//This is wrong, BOOL is a 8-bit signed char. Out of bounds.

1.2.7 Type Conversions
Type conversion is like the water in the bottle pour to each other, the small pour into the big inside basically no problem. But the big pour in the small inside needs to be cautious, may have the problem.
int intvalue = 33000;
Short shortvalue = Intvalue;
Must have spilled.
Look at the overflow process (never learned the principle of computer composition may not understand ...) )
Learn First {
The original code is directly converted to binary, but the computer saves all the integers in the form of a complement. The rule of complement is as follows
The complement of positive numbers is the same as the original code, the complement of the negative is its inverse code plus one, the inverse code is to the original code bitwise reverse, but the highest bit remains unchanged

}
3000 of the original code
00000000000000001000000011101000
Truncate to short only retains 16 bits
0000000000000000 1000000011101000//The highest bit is 1, indicating a negative number
Minus 1 Gets the inverse code 1000000011100111.
In addition to the sign bit, the other bit counter, get the original code
The final complement: 1111111100011000

1.2.8 operation symbol (subtraction omitted here)
& Bitwise AND
| bitwise OR
~ bitwise NON, bitwise reverse
^ Bitwise XOR OR
<< Bitwise LEFT Shift
>> Bitwise RIGHT Shift
Note: Moving the n bit to the left is like multiplying the N by 2 and moving the n bit to the right is divided by the N-Squared of 2 (the largest integer that returns the division result if it is not divisible).
The operand before the bitwise operation itself will not change.









iOS development Getting Started tutorial (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.