Objective-C (1)

Source: Internet
Author: User
Tags uicontrol

I am a java developer with some experience. Four years of java and three years of Android, I believe I don't need to talk much about why I want to learn IPhone. Because you know. Learning IPhone is certainly a piece of cake for me. First, the development language of the IPhone. objectie-c is an object-oriented language. Although I can compare it with java, the idea is similar. If everything can copy the java idea, through analogy, induction, I believe that even if you do not write objective-c code, you can understand the code and objective-c programming. But I would like to say that it is still necessary for us to spend a week to see why objctvie-c is, because we need to understand their basic api, which provides the methods and technologies. Only by understanding and mastering can you perform objective-c development better. Secondly, we all know that Android is a copy of the IPhone, and there are almost all Android components, IPhone, and IPhone components that Android does not have. In this way, we need to rewrite our own components in Android, it can be used directly on the IPhone, so IPhone is simpler than Android. I am only planning to master the technologies related to IPhone development in my spare time.

 

Two books are recommended: "studious Objective-c" and "IPhone application development getting started and practice", which are based on a wide range of content, all the knowledge points are in place. As for the future, we should first improve the basic knowledge and keep Objective-C unchanged, obj-c and java have too many similarities, so I cannot spend too much time on the conventional path and learning a little bit. We are mainly familiar with the ideas, then I just need to scan the obj-c API and find out what methods are available. So I want to start with java: for example, what are the basics of java: syntax, operation, object-oriented, Collection framework, thread, I/O, network communication, database, generic, annotation, reflection, etc.: So I guess: obj-c will be similar to these related things, as long as the two languages provide the same idea, it must be the same things. Even if it is slightly different, but we grasp the main line, there will not be too many deviations. Well, don't talk about other nonsense: Objective-C language features: 1. object-oriented: encapsulation, inheritance (only single inheritance is supported as java), polymorphism 2. method Declaration: you must add the "-" character before the method, and use the ":" symbol between the variable and the method name, for example,-(IBAction) changeNumber :( id) sender; IBAction is the output data type, changeNumber is the method name, ":" is followed by the input parameter information, "id" is the data type of the input parameter, and sender is the input variable, if you do not enter the parameter ":" and its subsequent declarations, you can remove them, for example,-(int) getNumber3. for example, id something. If you do not know the Data Type of something, you can use id, the id is dynamically determined (also called dynamic binding) at runtime. 4. int and NSNumber: one is an Integer data type and the other is an object data type, such as int and Integer in java. Conversion: Convert to NSNumber object + (NSNumber *) numberWithInt :( int) value to obtain the NSNumber value:-(int) intValue; 5. nil can be null: for example, if (member = nil) return; equivalent to: if (! Member) return; Value: member = nil; [member setCollege: nil]; the object can be empty (different from java. If you call a null object method in java, an error will be reported), so you do not need to check whether the object is null before calling the method: member = nil; [member getFree] 6. BOOL: Boolean value: the value is YES/NO or 1/0. YES or 1 indicates true. The Boolean value BOOL enable = NO; enable = 0; is used to determine the Boolean value: if (enable = YES )... Or if (enable )... 7. NSData; NSMutableData: Data Type (class) for storing binary data, 8. NSDate, NSCalendarDate: Data Type of the date (class) 9. objective-C class is the base class of all classes, such as objecte10in java. User-defined classes: such as (Company *) company; 11. method: The "-" method is called the instance method, and the "+" method is called the class method (in my understanding, you can directly use this method when declaring a variable, without instantiation, the static method in java can be directly called.) The "+" method used in sound is a class method, for example, the declared array method on the NSArray array: + arrayWithObjects (id) firstObj ,... // Each array element is followed by the nil end. Another format that is completely different from java/C ++ is the second parameter declaration. Let's take a look at an example. This is a school-class registration course method. The parameters include the course name and student name:-(void) enrollClass (NSString *) className student :( NSString *) student; the odd one is the second parameter. It has a statement similar to the method (student :) which can be called like this: [school enrollClass: @ "IPhone" student: @ "king"]; the second parameter must contain the declared name (student :). We understand the method as a message, the class as the receiver, and the method parameter as the message parameter, the above enrollClass method has two messages (enrollClass and student), or, for simplicity, you think that the second (or more) in the method declaration) "parameter name:" must be added before the parameter declaration, and "parameter name:" must also be added during the call :". 12.

String NSString

Declaration: NSString * address = @ "welcome to shenzhen"; format: NSString * name = @ "king-IPhone"; NSString * log = [NSString stringWithFormat: @ "I am '% @'", name]; "% @" is used to represent the value of a string. In addition, % d can be used to represent an integer, as shown below: Number. text = [NSString stringWithFormat: @ "% d", sliderValue]; append string: NSString * address = @ "welcome come to shenzhen"; NSString * word = [address stringByAppendingString: @ "do IT"]; Comparison and determination of the string:-(BOOL) isw.tostring :( NSString *) string; // compare whether the two strings are the same-(BOOL) hasPrefix :( NSString *) string; // judge the start character-(int) intValue; // convert it to an integer-(double) doubleValue: // convert it to a double value. Instance: NSString * name = @ "king-IPhone"; NSString * age = @ "36"; If ([name hasPrefix: @ "king"]) {…} If ([age intValue]> 30 ){...} Similar to java, NSString itself cannot be modified: If you need to modify the string, you can use NSMutableString. NSMutableString provides the method for appending strings:-(void) appendString :( NSString *) String;-(void) appendFormat :( NSString *) string; instance: NSMutableString * name = [NSMutableString new]; [name appendString @ "king"]; 13. format of the call method:

 

The format is (parameter name can be omitted): [class name Method Name: parameter 1 parameter name: parameter 2...] Number. text = [NSString stringWithFormat: @ "% d", sliderValue]; the first parameter is the format, and the second parameter is the value of the slider. The preceding call method statement (the entire […]) It is called a Message Expression. "method name: parameter" is called a Message, and "method name:" is called a selector ), you can understand a selector as a pointer to a method. The Objective-c2.0 started to support the dot format, same as "." In java/C ++. Format, for example, Int age = [member age]; =? Int age = member. age; Value: [member setAge: 36] =? Member. age = 36 In addition, the "[]" method call allows method nesting, as follows: [[member child] setAge: new Age] ;=> member. child. age = newAge, however, there is a limit on the vertex format, which is limited to one parameter. If there are multiple parameters, the vertex format cannot be used. There are two more points: Objective-c runs dynamically and the methods are bound at runtime.

 

14. selector: SEL

 

In Obj-c, SEL is the data type of the selector. For example, "SEL action = [button action]"-(void) setTarget :( id) target;-(void) setAction: (SEL) action; the following statement sets the Action on a button object to "@ selector (start :)" to call the start method: [button setAction: @ selector (start :)]; if your method has two parameters (Message) such as-(void) setName :( NSString *) name age :( int) age can contain two messages, such as SEL sel = @ selector (setName: age :). Sometimes, when you call Action, you want to determine whether the object exists in the specified method, you can use respondsToSelector to determine, for example, Id obj; // Specify target object SEL sel = @ Selector (start :); // specifies the actionIf ([obj respondsToSelector: sel]) {// determines whether the object has a specified method [obj preformSelector: sel withObject: self] // call the method, followed by the input parameter} if this method does not exist, the application will stop. 15. objective-C Class: Class class, class theClass = [theObject Class]; // obtain the Class information of theObject NSLog (@ "class name is % @", [theClass className]); // specifies the name of the record class to determine whether an object is an object (including a subclass). (For example, do you know java? For example) if ([theObject isKindOfClass: [UIControl class]) {…} If the subclass is not included, you can use: If ([theObject isMemberOfClass: [UIControl class]) {…} NSObject (the base class of all classes)-(NSString *) description // The information array (NSArray) and set (NSSet) + arrayWithObjects :( id) firstObj ,...... // Declare the array, followed by each element, ending with nil;-(unsigned) count; // number of elements in the array-(id) objectAtIndex :( unsigned) index; // specifies the position of the element. -(Unsigned) indexOfObject :( id) object; // The Position of the object in the array. Example: NSArray * city = [NSArray arrayWithObjects: @ "Beijing", @ "Shanghai", @ "Huzhou", nil]; If ([city indexOfObject: @ "Hangzhou"]) {NSLog (@ "Hangzhou is not")} NSArray is a static array and cannot dynamically add elements to this array. You can use NSMutableArray to dynamically manage arrays. NSMutableArray is a subclass of NSArray, common NSMutableArray methods are as follows. + (NSMutableArray *) array; // declare an array (void) addObject :( id) object // Add an element (void) removeObject :( id) object; // Delete the specified Element (void) removeAllObjects from the array; // delete all elements (void) insertObject :( id) object atIndex :( unsigned) index; // Add a new element instance to a specified location: NSMutableArray * city = [[NSMutableArray alloc] init]; [city addObject: @ "Beijing"]; [city addObject: @ "Shanghai"]; [city addObject: @ "Shenzhen"]; [city removeObjectAtIndex: 1]; note that the obj-c index is a collection concept implemented by the NSSet class starting from 1. Its methods include: + s. EtWithObjects :( id) firstObj...: // Declare the set, followed by each element, ending with nil. -(Unsigned) count; // returns the number of sets-(BOOL) containsObject :( id) object; // determines whether the specified object is included in the set and is similar to an array, NSSet itself is also a set that cannot be modified. NSMutableSet is a set that can be modified accordingly. The methods include: + (NSMutableSet *) set; // declare a set-(void) addObject :( id) object; // Add an element to the set-(void) removeObject: (id) object; // delete an element from the set. -(Void) removeAllObjects; // delete all elements in the Set-(void) insersectSet :( NSSet *) otherSet; // intersection of the two sets-(void) minusSet :( NSSet *) otherSet; // all elements not in the specified set NSDictionary (set of key-value pairs) + dictionaryWithObjectsAndKeys :( id) firstObj ,... // Declare a dictionary and end with nil. -(Unsigned) count; // obtain the number of key-value pairs in the dictionary-(id) objectForKey :( id) key; // query the value corresponding to a key, if the nil instance does not exist, return NSDictionary * employees = [NSDictoinary dictionaryWithObjectsAndKeys: @ "Cao", @ "1", @ "Liu Bei", @ "2 ", @ "Sun Quan" @ "3", nil]; // note that the key is in the front and the value is in the back. NSString * firstEmployee = [employees objectForKey: @ "1"]; Modify NSDictionary to dynamically Add/delete Elements Using NSMutableDictionary. The methods include: + (NSMutableDictionary *) dictionary; // declare a dynamic dictionary-(void) setObject :( id) object forKey :( id) key; // set the value and key-(void) removeObjectForKey :( id) key; // Delete the object specified by the key-(void) removeAllObjects; // delete all objects. Instance: Add a group of key-value pairs. NSMutableDictionary * employees = [[NSMutableDictionary alloc] init]; [employees setObject: @ "Zhao Yun" forKey @ "4"] enumeration access: Method 1: NSArry * array =... // Member * member; Int count = [array count]; // obtain the Member array For (I = 0; I <count; I ++) {Member = [array objectAtIndex: i]; // For each member NSLog ([Member discription]); // records the member description in the log} Method 2: For (member * Member in array) {NSLog ([member description]);}

 

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.