[IOS development] Object, ios development object

Source: Internet
Author: User
Tags array definition

[IOS development] Object, ios development object

.



1. One-dimensional array



1. One-dimensional array Definition



(1) array Definition


Array Definition Format:Type arrayName [len];

--Default Initialization: After the array is set, if the int array is initialized to 0 by default, if it is a floating point type, the default element is 0.0, if it is a pointer type array, the default type is null;



(2) digital Address Calculation


Address properties of array elements: Array elements are stored consecutively;

--Array header address: The array variable is a pointer that stores the first address of the element in the array;

--Element Address Calculation Formula: Element address = array first address + Memory occupied by array elements * element index value;




2. array Initialization


Number of array elements:

--Initialize all: Initializes all elements of the array;

--Partial Initialization: Initializes some elements in the array;


No number of arrays specified: If no number of arrays is specified, the array length must be initialized. The array length is automatically inferred based on the number of initialized elements;


Array Length Calculation:Sizeof (arrayName)/sizeof (arrayName [0]);



3. Sample Code


Sample Code:

--Code content:

/*************************************** * *********************************> File Name: 12-oneArray.m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 12/7 16:30:31 2014 ************************************ ***********************************/# import <foundation/Foundation. h> int main (int argc, char * argv []) {@ autoreleasepool {// defines the array format type arrayName [length] int array [5]; // defines the length, fully assigned int array1 [5] = {0, 1, 2, 3, 4}; // defines the length, not fully assigned int array2 [5] = {0, 1, 2}; // The length is not defined. int array3 [] = {0, 1, 2} must be initialized }; // define the C language String Array char * array4 [] = {"csdn", "octopus", "hello "}; // define the Object-C string array NSString * array5 [] ={@ "csdn", @ "octopus", @ "hello "}; // traverse array2for (int I = 0; I <sizeof (array2)/sizeof (array2 [0]); I ++) {NSLog (@ "array2 [% d] = % d", I, array2 [I]) ;}}
-- Execution result:

octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 12-oneArray.m octopus-2:oc octopus$ ./a.out 2014-12-07 16:38:58.238 a.out[379:507] array2[0] = 02014-12-07 16:38:58.241 a.out[379:507] array2[1] = 12014-12-07 16:38:58.242 a.out[379:507] array2[2] = 22014-12-07 16:38:58.242 a.out[379:507] array2[3] = 02014-12-07 16:38:58.243 a.out[379:507] array2[4] = 0




3. Multi-dimensional array



1. Two-dimensional array Definition


Two-dimensional array Definition Format: Type arrayName [rowLength] [columnLength];

--Array Length: The number rowLength on the left is the number of one-dimensional arrays, that is, the number of rows;



2. Multi-dimensional array Initialization


Several initialization Methods: Example int array [3] [5];

--Initialize all second-level Arrays: Three one-dimensional arrays can be initialized. The number of elements in one-dimensional arrays is random;

--Initialize some secondary Arrays: It can be initialized with less than three one-dimensional arrays, only part of which is initialized;

--Omitting the length of a two-dimensional array: If Initialization is performed when an array is declared, the length of the first array can be omitted;

--Omitting the second-level array brackets during initialization: The parentheses of the one-dimensional array can be omitted. The system automatically fills in the initialized part by default;



3. Example


Sample Code:

--Object-C code:

/*************************************** * *********************************> File Name: 12-multiArray.m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 4. 12/11 00:59:14 2014 ************************************ ***********************************/# import <foundation/Foundation. h> int main (int argc, char * argv []) {@ autoreleasepool {printf ("1. initialize part of all arrays \ n "); int array1 [3] [5] = {1, 2}, {5, 7}, {2, 4 }}; for (int I = 0; I <sizeof (array1)/sizeof (array1 [0]); I ++) {for (int j = 0; j <sizeof (array1 [0])/sizeof (array1 [0] [0]); j ++) {printf ("% d \ t ", array1 [I] [j]);} printf ("\ n");} printf ("\ n2. initialize part of the array \ n "); int array2 [3] [5] = {1, 2, 4, 6, 7}, {1}; for (int I = 0; I <sizeof (array2) /sizeof (array2 [0]); I ++) {for (int j = 0; j <sizeof (array2 [0]) /sizeof (array2 [0] [0]); j ++) {printf ("% d \ t", array2 [I] [j]);} printf ("\ n");} printf ("\ n3. omitting the length of the array \ n"); int array3 [] [5] = {1, 2, 4, 6, 7 },{ 1 }}; for (int I = 0; I <sizeof (array3)/sizeof (array3 [0]); I ++) {for (int j = 0; j <sizeof (array3 [0])/sizeof (array3 [0] [0]); j ++) {printf ("% d \ t", array3 [I] [j]);} printf ("\ n ");} printf ("\ n4. omit array braces \ n"); int array4 [] [5] = {1, 2, 4, 6 }; for (int I = 0; I <sizeof (array4)/sizeof (array4 [0]); I ++) {for (int j = 0; j <sizeof (array4 [0])/sizeof (array4 [0] [0]); j ++) {printf ("% d \ t ", array4 [I] [j]);} printf ("\ n ");}}}

--Execution result:

Octopus-2: oc octopus $ clang-fobjc-arc-framework Foundation 12-multiArray.m octopus-2: oc octopus $. /. out 1. initialize part of all arrays 1200057700240002. initialize part of the array content 1246710000000003. the length of the array is omitted 12467100004. omit the array braces 1246710000.



3. String, character array and string-related functions



1. Comparison between NSLog and printf


Comparison between NSLog and printf:

--Different output string formats: Printf outputs a string in the C language format, for example, "string". NSLog outputs an Object-C string, for example, @ "string ";

--Excessive NSLog operations: NSLog automatically adds new line breaks and other operations;


Example:

/*************************************** * *********************************> File Name: 12-NSLogVSprintf.m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 4. 12/11 01:18:17 2014 ************************************ ***********************************/# import <foundation/Foundation. h> int main (int argc, char * argv []) {@ autoreleasepool {NSLog (@ "this is the NSLog output string "); printf ("this is the string output by printf ");}}

-- Execution result:

Octopus-2: oc octopus $ clang-fobjc-arc-framework Foundation 12-NSLogVSprintf.m octopus-2: oc octopus $. /. out 01:19:07. 370. out [1925: 507] This is the string output by NSLog, which is the string output by printf octopus-2: oc octopus $



2. Character arrays and strings


Define character array: The following two methods are equivalent;

--Use string Initialization: Char array [] = "fuck". After this method is used, '\ 0' is automatically added to the end ';

--Array-based Initialization: Char array [] = {'F', 'U', 'C', 'k', '\ 0 '};


Length of character array: An array consisting of 4 characters. Its length is 5, followed by '\ 0 ';


Sample Code:

/*************************************** * *********************************> File Name: 12-charArray.m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 4. 12/11 01:27:12 2014 ************************************ ***********************************/# import <foundation/Foundation. h> int main (int argc, char * argv []) {@ autoreleasepool {char char_array [] = "fuck"; char char_array1 [] = {'F ', 'U', 'C', 'k', '\ 0'}; printf ("char_array size: % lu, char_array1 size: % lu \ n", sizeof (char_array ), sizeof (char_array1 ));}}

-- Running result:

Octopus-2: oc octopus $ clang-fobjc-arc-framework Foundation 12-charArray.m octopus-2: oc octopus $./a. out char_array size: 5, char_array1 size: 5



3. common string processing functions



Sample Code:

--Code:

/*************************************** * *********************************> File Name: 12-strMethod.m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 4. 12/11 01:34:08 2014 ************************************ ***********************************/# include <string. h> # import <Foundation/Foundation. h> int main (int argc, char * argv []) {@ autoreleasepool {char c = '1'; char c1 = 'a'; char c2 = 'a '; char c3 = '\ T'; NSLog (@ "character-related function"); printf ("c = % c is a number or number: % d \ n", c, isalnum (c); printf ("c = % c: % d \ n", c, isalpha (c )); printf ("c = % c is a control character: % d \ n", c, iscntrl (c); printf ("c = % c is a control number: % d \ n ", c, isdigit (c); printf (" c = % c: % d \ n ", c, isgraph (c )); printf ("c1 = % c: % d \ n", c1, islower (c1); printf ("c1 = % c: % d \ n ", c1, isupper (c1); printf (" c1 = % c: % d \ n ", c1, ispunct (c1 )); printf ("c3 = % c is blank: % d \ n", c3, isspace (c3); printf ("c1 = % c is a hexadecimal character: % d \ n ", c1, isxdigit (c1); NSLog (@" string related function "); char array [20] =" fuck "; char array1 [] = "bitch"; // note that the string array must have enough space to store the array1printf ("% s concatenated string \ n", strcat (array, array1); printf ("location where array appears c % s \ n", strchr (array, 'C'); printf ("compare the size of array and array1: whether array is greater than array1: % d \ n ", strcmp (array, array1 ));}}


--Execution result:

Octopus-2: oc octopus $ clang-fobjc-arc-framework Foundation 12-strMethod.m octopus-2: oc octopus $. /. out 02:11:08. 608. out [2164: 507] character-related function c = 1 is a number or number: 1 c = 1 is a letter: 0c = 1 is a control character: 0c = 1 whether it is A control number: 1c = 1 whether it is printable character: 1c1 = A whether it is A lowercase letter: 0c1 = A whether it is A capital letter: 1c1 = A whether it is A punctuation character: 0c3 = whether it is A blank character: 1c1 = A whether it is A hexadecimal character: 12014-12-11 02:11:08. 610. out [2164: 507] string-related function fuckbitch concatenates string array where c appears ckbitch compare the size of array and array1: whether array is greater than array1: 4



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.