Objective-c Basic Framework (beginner-Summary) and objective-c framework

Source: Internet
Author: User

Objective-c Basic Framework (beginner-Summary) and objective-c framework

A framework is actually a software package that contains multiple classes. The Mac operating system provides dozens of frameworks to help developers quickly develop applications on Mac systems. These include some basic frameworks that provide the basic framework for all program development. Several common classes include: string (NSString), number (NSNumber), array (NSArray ), dictionary (NSDictionary), set (NSSet), etc. These classes are frequently used, so you must have a good grasp of them. Let's introduce these classes in detail.

  1. Number object (NSNumber)

Why use a numeric object? We are familiar with the use of int and other data types to generate explicit numeric variables, but because many classes (such as NSArray) require the use of objects, but the numerical variables declared such as int are not objects, cannot be used in these classes. NSNumber is a digital object class.

The following example shows how to use this numeric object.

  

1 # import <Foundation/Foundation. h> 2 3 int main (int argc, const char * argv []) {4 @ autoreleasepool {5 NSNumber * myNumber, * floatNumber, * intNumber, * doubleNumber; 6 7 // create an interger object 8 intNumber = [NSNumber numberWithInt: 1]; 9 NSLog (@ "intNumber = % li", [intNumber integerValue]); 10 11 // create a long object 12 myNumber = [NSNumber numberWithLong: 0 xababab]; 13 NSLog (@ "myNumber = % lx", [myNumber longValue]); 14 15 // create a char object 16 myNumber = [NSNumber numberWithChar: 'C']; 17 NSLog (@ "myNumber = % c", [myNumber charValue]); 18 19 // create a float object 20 floatNumber = [NSNumber numberWithFloat: 3.14]; 21 NSLog (@ "floatNumber = % f", [floatNumber floatValue]); 22 23 // create a double type object 24 doubleNumber = [NSNumber numberWithDouble: 3.11]; 25 NSLog (@ "doubleNumber = % lg", [doubleNumber doubleValue]); 26 27 // determine whether the values of the two objects are equal 28 if ([in TNumber isEqualToNumber: floatNumber]) 29 NSLog (@ "intNumber = floatNumber"); 30 else31 NSLog (@ "intNumber! = FloatNumber "); 32 33 // compare the value size of two objects 34 if ([intNumber compare: floatNumber] = NSOrderedAscending) 35 NSLog (@" small number on the left "); 36 else37 NSLog (@ "small number on the right"); 38} 39 return 0; 40}

 

 

  2. String object

A String constant is a string of characters enclosed by a symbol @ and a pair of double quotation marks. For example, @ "hello world ". Character constants and string constants are different in quantity. There are three main differences between them:

1. characters are enclosed by single quotes, while string constants are enclosed by @ and double quotes.

2. A character constant has only one character, while a String constant can have multiple characters.

3. character constants only occupy one byte. The number of bytes of A String constant is increased by 1 by the number of characters. The number of characters added is the terminator '\ 0 '.

In Objective-C, NSString is used to operate strings, rather than c/c ++ char *. It adds the @ symbol before the string. For example, @ "apple ". The following uses an example to demonstrate its usage in oc:

2.1 unmodifiable string (NSString)

  

1 # import <Foundation/Foundation. h> 2 3 int main (int argc, const char * argv []) {4 @ autoreleasepool {5 6 // stringWithString is used to create another string object based on one string. 7 NSString * str1 = @ "hello world"; 8 NSString * str2 = [NSString stringWithString: str1]; 9 NSLog (@ "str1 = % @, str2 = % @", str1, str2); 10 11 // The NSString formatting method stringWithFormat is provided. In oc, % @ is used to represent the value of a string. 12 NSString * name = @ "xiaozhang"; 13 NSString * str3 = [NSString stringWithFormat: @ "I am % @", name]; 14 NSLog (@ "str3 = % @", str3); 15 16 // Add a new string after a string. 17 NSString * guilin = @ "guilin"; 18 NSString * welcome = [guilin stringByAppendingString: @ "welcome you! "]; 19 NSLog (@" % @ ", welcome); 20 21 // string comparison and judgment 22 BOOL isSame = [str1 isw.tostring: @" str3 "]; 23 if (isSame) 24 NSLog (@ "str1 = str2"); 25 else26 NSLog (@ "str1! = Str2 "); 27 28 NSComparisonResult result = [str1 compare: str2]; 29 if (result = NSOrderedSame) // The content is the same as 30 NSLog (@ "str1 = str2"); 31 else if (result = NSOrderedAscending) // The left side is less than 32 NSLog (@ "str1 <str2"); 33 else // result = NSOrderedDescending // The left side is greater than 34 NSLog (@ "str1> str2") on the right side "); 35 36 // string case-insensitive conversion 37 NSString * str4 = [str1 uppercaseString]; // converts it to uppercase 38 NSString * str5 = [str1 lowercaseString]; // convert to lowercase 39 NSLog (@ "str4 =%@, str5 =%@", str4, str5 ); 40 41 // string truncation 42 NSString * str6 = [str1 substringToIndex: 2]; 43 NSLog (@ "str6 = % @", str6 ); 44 45 // remove the first four strings to form a new string 46 NSString * str7 = [str1 substringFromIndex: 4]; 47 NSLog (@ "str7 = % @", str7 ); 48 49 // new character 50 NSString * str8 = [[str1 substringFromIndex: 2] substringToIndex: 5] formed from a string to a string; 51 NSLog (@ "str8 = % @", str8); 52 53 nsange rang = [str1 rangeOfString: @ "lo"]; 54 NSLog (@ "the start position of the string is % lu, And the length is % lu", rang. location, rang. length); 55 56 if ([str1 rangeOfString: @ "lo"]. location = NSNotFound) 57 NSLog (@ "contained string not found"); 58 else59 NSLog (@ "the start position of the string is % lu, And the length is % lu ", rang. location, rang. length "); 60 61} 62 return 0; 63}

 

2.2 modifiable string (NSMutableString)

NSString itself cannot be modified. If you want to modify it, you can use NSMutableString. NSMutableString is a subclass of NSString. All nsstrings are applicable to NSMutableString, which provides the method for appending strings:

-(Void) appendString: (NSString *) string;

-(Void) appendFormat: (NSSting *) string;

 

For example:

NSMutableString * str = [NSMutableString stringWithString: @ "Giulin"];

[Str appendString: @ "welcome you! "];

 

3. array object

 

An array is a set of ordered objects. In general, elements in an array are of the same type. Similar to variable strings and immutable strings, there are also variable arrays and immutable arrays.

 

3.1 immutable array (NSArray)

 

NSArray is an array class. elements in an array must end with nil. The following is an example to illustrate its usage:

  

  

# Import <Foundation/Foundation. h>

 

Int main (int argc, const char * argv []) {

@ Autoreleasepool {

NSArray * array = [NSArray arrayWithObjects: @ "zhangsan", @ "lisi", @ "wangwu", @ "zhaoliu", nil];

For (int I; I <[array count]; I ++)

NSLog (@ "% @", [array objectAtIndex: I]);

If ([array indexOfObject: @ "xiaoli"] = NSNotFound)

NSLog (@ "xiaoli is not in it! ");

}

Return 0;

}

 

 

 

 

3.2 modifiable array (NSMutableArray)

NSArray is a static array, so you cannot add elements to the array. You must use NSMutableArray to dynamically manage the array. NSMutableArray is a NSArray word class.

    

The following is an example to describe its usage:

  

1 # import <Foundation/Foundation. h> 2 3 int main (int argc, const char * argv []) {4 @ autoreleasepool {5 NSMutableArray * city = [NSMutableArray arrayWithObjects: @ "Xi'an ", @ "Beijing", @ "Shanghai", nil]; 6 [city addObject: @ "Shenzhen"]; // Add an element Shenzhen to the array. 7 [city removeObject: @ "Shanghai"]; // delete an element in the array Shanghai. 8 [city insertObject: @ "Guilin" atIndex: 1]; // insert Guilin to the first element. 9 // [city removeAllObjects]; // delete all elements. 10 for (int I; I <[city count]; I ++) 11 NSLog (@ "% @", [city objectAtIndex: I]); 12 13} 14 return 0; 15 16}

  4. dictionary objects (NSDictionary and NSMutableDictionary)

NSDictionary provides a set of key-value pairs. For example, the dictionary class is used to store Student IDs to names. The number is a key (unique) and the name is a value. Its methods include:

  

The following example shows the specific application:

  

1 # import <Foundation/Foundation. h> 2 3 int main (int argc, const char * argv []) {4 @ autoreleasepool {5 NSDictionary * student = [NSDictionary dictionaryWithObjectsAndKeys: @ "xiaohong ", @ "01", @ "xiaoming", @ "02", @ "xiaoqiang", @ "03", nil]; 6 NSLog (@ "% @", [student objectForKey: @ "01"]); // find the value corresponding to the 01 key. If no value exists, nil is returned. 7 NSLog (@ "count: % li", [student count]); // obtain the number. 8} 9 return 0; 10}

 

Similar to the strings and arrays described above, NSDictionary is also a dictionary that cannot be modified. The variable dictionary is NSMutableDictionary, which can dynamically add and delete elements. Its methods include:

The following is a simple example:

 1 #import <Foundation/Foundation.h> 2  3 int main(int argc , const char *argv[]){ 4     @autoreleasepool { 5         NSMutableDictionary *student = [NSMutableDictionary dictionary]; 6         [student setObject:@"xiaohong" forKey:@"01"]; 7         [student setObject:@"xiaoming" forKey:@"02"]; 8         [student setObject:@"xiaodong" forKey:@"03"]; 9         10         NSLog(@"01: %@",[student objectForKey:@"01"]);11         NSLog(@"02: %@",[student objectForKey:@"02"]);12         NSLog(@"03: %@",[student objectForKey:@"03"]);13         14     }15     return 0;16 }

 

4. Set object (NSSet)

  

A collection object is a combination of Single-value objects, such as a set of 1 to 50 numbers. Operations on collection objects include searching, adding, and deleting members in a set, comparing two sets, and calculating the intersection and union of the two sets. The following program demonstrates some common methods of collections.

  

1 # import <Foundation/Foundation. h> 2 3 @ interface NSSet (printInteger) 4 5-(void) printSet; 6 7 @ end 8 9 @ implementation NSSet (printInteger) 10 11-(void) printSet {12 for (NSNumber * integer in self) {13 printf ("% li", [integer integerValue]); 14} 15 printf ("\ n "); 16} 17 @ end18 19 int main (int argc, const char * argv []) {20 @ autoreleasepool {21 NSMutableSet * set1 = [NSMutableSet setWithObjects: [NSNumbe R numberWithInteger: 1], [NSNumber numberWithInteger: 5], [NSNumber numberWithInteger: 3], nil]; 22 23 NSMutableSet * set2 = [NSMutableSet identifier: [NSNumber numberWithInteger: 7], [NSNumber numberWithInteger: 2], [NSNumber numberWithInteger: 4], nil]; 24 25 if ([set1 isEqual: set2] = YES) // compare whether two sets are equal 26 NSLog (@ "set1 = set2"); 27 else28 NSLog (@ "set1! = Set2 "); 29 30 if ([set1 containsObject: [NSNumber numberWithInteger: 2] = YES) // determine whether the set contains an element 31 NSLog (@ "set1 contains 2"); 32 else33 NSLog (@ "set1 does not contain 2"); 34 35 [set1 addObject: [NSNumber numberWithInteger: 6]; // Add element 36 to the set [set1 removeObject: [NSNumber numberWithInteger: 6]; // delete an element 37 38 [set1 intersectSet: set2]; // calculate the intersection of two sets 39 [set1 unionSet: set2]; // calculate the union of two sets 40 [set1 printSet]; 41} 42 return 0; 43}

 

5. Enumeration access

For arrays, dictionaries, and collections, Objective-C provides an enumeration method to access each element. There are two specific methods.

Method 1:

  

Method 2:

    

The second method is simple, so the second method is often used. The second method is usually called Quick enumeration. The following is an example of dictionary quick enumeration: The Code defines an NSString type object, which is used to save the obtained key every time, and then calls the objectForKey of the dictionary object student based on the retrieved key: method to obtain the value and print it to the console.

  

 1  #import <Foundation/Foundation.h> 2  3  int main(int argc , const char *argv[]){ 4         @autoreleasepool { 5                 NSMutableDictionary *student = [NSMutableDictionary dictionary]; 6                 [student setObject:@"xiaohong" forKey:@"01"]; 7                 [student setObject:@"xiaoming" forKey:@"02"]; 8                 [student setObject:@"xiaodong" forKey:@"03"]; 9     10                 for(NSString *key in student)  11                     NSLog(@"%@:%@",key,[student objectForKey:key]);12     }13     return 0;14 }

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.