Objective-c's Introductory learning notes _ios

Source: Internet
Author: User
Tags abstract definition reserved what inheritance uikit


For a language to learn, individuals feel that the most important is its grammatical format and those keywords.



Because for most of the basic language now, each language is a way of thinking to communicate with the computer.



Therefore, in fact, each language is defined or encapsulated a good base class and class library, in fact, are very similar.



For example int,double several basic types, String, array, collection, and dictionary data types.



So when you're programming like you want to communicate with the computer as a string, it's easy to think of a string class in C, which is also stirng in Java



(If, of course, you have at least one language base at the same level of abstraction), that OC, there is a class, but for some reason a prefix is added.



So for the class to define the function to use, may be different languages, the first is the function name (this is actually not worried about, now get the IDE ToolTips too strong, really bad you can document),



The second is that the function has to call the format, which I simply understand as a grammatical format.






I. Overview of the grammar



1. First, the composition of a class



This one knows the basics of C. h and. m files. One is the header file and one is the entity file.



OC is generally said to be the declaration of documents and implementation documents



In simple terms, the H file is the overall description of a class, M file is the H file statement to achieve the description


Copy Code code as follows:

//
NonoAppDelegate.h
MultiViews
//
Created by Nono on 12-4-19.
Copyright (c) 2012 Nonowithlilith. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface Nonoappdelegate:uiresponder <UIApplicationDelegate,UITabBarControllerDelegate>
{
Uitabbarcontroller *tabbarcontroller;
UIWindow *window;

}

@property (Retain, nonatomic) Iboutlet UIWindow *window;
@property (Retain, nonatomic) Iboutlet Uitabbarcontroller *tabbarcontroller;
@end





2. See header file format and declaration



#import This keyword has nothing to say. called import in Java. is to introduce the dependent libraries and classes that you want to use for your current class.



@interface declare the beginning keyword. followed by the definition of the amount class name: The following is the inherited class Uiresponder,< xxxxxxxxx> a bit similar to the Java interface, of course, there is an OC



More accurate definition is called protocol,<> put protocol class. The whole statement ended with @end.



{} Inside is often said to declare member variables.



@property attribute, I temporarily understand the meaning of a member variable as well. This property is more closely related to the synthesize keyword in the later m file.



Some parameters of the (retain, nonatomic) property. This specific can go to see the document, estimated C language students should be not unfamiliar, I am not very clear each parameter is more suitable for occasions.



Iboutlet keyword, this is related to the nib file, as long as the object in the nib file need to be associated with this modification (such as my nib file has a window control, and the code in the connection to this object



, then use this keyword to declare), after the declaration will see that this code has a hollow circle at the left end, when you and the nib file in the control associated with a solid circle.


Copy Code code as follows:

//
Nonoappdelegate.m
MultiViews
//
Created by Nono on 12-4-19.
Copyright (c) 2012 Nonowithlilith. All rights reserved.
//

#import "NonoAppDelegate.h"

@implementation Nonoappdelegate

@synthesize window;
@synthesize Tabbarcontroller;

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
Override point for customization after application launch.
Self.window.backgroundColor = [Uicolor Whitecolor];
Self.window.rootViewController = Self.tabbarcontroller;
[Self.window makekeyandvisible];
return YES;
}
@end

3. Implementation document





Implmentationxxx @end. Let's not say that.



@synthesize This keyword is somewhat interesting. After this keyword notation, the compiler automatically implicitly generates a setter and getter,oc on the idea that it automatically implements a



Access mode. Then we know that the OC language is the customary way, a method and a property must have a declaration before use, is the H and M files this structure. That's for the H header file.



We are not understandably, this is a statement to the compiler, I will implement this object access method in the implementation file, and the M implementation is only a keyword after we let the compiler do this thing.



4. The example concludes by saying that the method of object in OC can be called



For example, in Java I want to call the Functiona () method of a object, directly using the point notation A.functiona ();



In the OC is a bit peculiar expressed as [a functiona], if the method is passed parameters, then follow functiona:obj




second, the key words
1.NS prefix. Most classes of OC have this prefix. To understand the reason, you can go to see the OC history.



2.Bool This data type has changed somewhat, in common language is true and False,oc is yes and no.



3. @protocol keyword, this is the declaration of the Protocol class, and @end used, to see the literal awareness.



For the protocol we mentioned above, somewhat similar to the Java interface, when it is well known that the method declared in the interface must be fully implemented. Java of course to such a strong self means has extended



Abstract the class of abstraction to this coercive means a mitigation, OC in the Protocol class directly affirmed this point, for some methods do not need to be enforced, optional, with the optional.



This you can jump into the details of a protocol class to see the good, I believe that in the future after more code contact will have a deeper understanding.



4. There may be a small doubt type (ID) for people who have just contacted the OC language, which can be understood as generics in the Java language.






Three, Oop thought



The concept of object-oriented is something that everyone should have.



What inheritance, encapsulation, polymorphism these are just like memorizing words. However, the simple still need to use vernacular can say.



First of all, encapsulation: encapsulation I think is actually the most primitive basis of OOP thought. After the introduction of encapsulation, the concept of object can be derived. We put an object and its own attributes and more able to package, is a pull away and then fusion process.



Inheritance: Inherit from the literal sentence well understood that the son inherits the father. An object derives a new object. Of course, after inheriting a lot of features, you can endorse it.



Polymorphism: Polymorphism In fact better explain, listen to the literal, many forms? Multiple implementations of a thing (of course, this may be intuitive and literal, but it is wrong in rigor) because of the various implementations of an object, how



To see how it is like inheritance, such as I have a fish, then I can derive a shark class and a whale class, which for this practice, the feeling is inherited AH. In fact, they all have the same essential ideas,



It's just a different focus. Inheritance is more like expressing an object deriving a new object, polymorphism is an object derived from multiple objects to change the different implementation of the object, this implementation is the most direct way in the superclass of the different methods of the override and implementation. For example, fish, there is a eat () method, derived from the shark, we all know that sharks eat meat, then we can achieve the Eat method is to eat meat, blue whale we calculate his omnivorous, then the Eat method of the blue whale is to achieve omnivorous eating method. At this point you can simply see the concept of polymorphism in a comprehensive perspective.



A deep understanding of the definition of OOP is akin to seeing the height of the abstraction you are dealing with.






A centralized way of producing new things or a new class



1. The most primitive one is our own abstract definition except for a class. For example, to define the most primitive classes, there is a primitive class object in both Java and OC.



2. Inherit to derive a new class. This is very well understood. From human human, for example, human man,



3. Another is that we are less aware of it, but in fact it is very common to approach: compound, with a few different classes to compound into a new class. The most commonly used on the book to explain the example, car cars class,



Simply we can use the engine engine and the tires wheel to construct the simplest car model.




v. The concept of categories and informal agreements in OC



1.category, the category should be considered a unique term for OC, which defines a category as a way to add a new method to an existing class.



In fact, this way how to listen to how to create a subclass of the concept can be done.



Yes, for a class to expand, the most common way to think about it is to create a subclass of it, and then add new methods that you need to extend.



But sometimes this is inconvenient in OC, with an example of a nsstring in the book. This is the exact words:



/**********



When you want to add new behavior to a new class, we typically create subclasses.



But sometimes subclasses are not convenient. For example, you might want to add some new behavior to the NSString class, but you know that NSString is actually just a foreground representation of a cluster, and therefore cannot



Create subclasses for such a class. Omit 1000 words.



********/



Well, from the above we basically know that there are limitations to creating subclasses, right. The above introduces a concept called a class cluster.



Literally (a bunch of flowers, that is, many flowers in a bundle of the present, the concrete in fact we do not know.) )



The cluster is actually a modest concept. The collective can see the official document http://www.apple.com.cn/developer/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/ Cocoaobjects/chapter_3_section_9.html



I am also new contact this thing, there is such a sentence:



/*****



A class cluster combines some private, specific subclasses under a common, abstract superclass.



****/



I don't know if I can understand it. The class cluster contains components that are private, but we all know that the inherited subclass is not accessible to the private members of the parent class (is this supposed to be true?). )



So the new method we added to the newly created subclass has no access to those things.



Well, the above is a personal understanding, this thing is also listen to a mouthful, specific details can refer to more official documents.



So, well, we generally know the reason why OC introduces the category. With the allocation mechanism of the OC Dynamic Runtime, you can add new methods to the existing classes, hey, this



It sounds cool! (the exact words in the book)



Category creation:


Copy Code code as follows:

@interface NSString (numberconvenience)
-(NSNumber *) Lengthasnumber;
@end//numberconvenience

The statement has two features:
(1) The existing class is located after the @interface keyword, followed by the category name in parentheses. The category name is Numberconvenience, and the category adds a method to the NSString class. In other words: "We add a category called Numberconvenience to the NSString class." "A category with the same name has uniqueness, but you can add as many different name categories as you want.
(2) You can perform the class and the name of the category that you want to add a category to, and you can also list the methods that you add
You cannot add a new instance variable, and there is no instance variable part in the category life.





The implementation of a category is essentially the same as the implementation of a generic class.



Informal agreements and commissions: the concept of informal agreements is actually another way of expressing categories. "Here are some ways you might want to do it, and you can use them to do a better job."



This means that these are optional. For example, we want a better way, we will declare a category to achieve. Then you can use these better methods directly later.



In this way, it's always felt like the sort of thing is an optional protocol to the Protocol.



An optional agreement is merely prescient, and the category is later remedied. At the same time, the method implementation is optional. Optional Protocol has key optionnal affirmed that the category you like efficient method you're declaring that



Don't want to just forget.



Then it is commissioned to calculate a very heavyweight concept in OC. It is said that deep will find a lot of things, mainly I currently only contact a few days, for it in the OC in the overall design model theory is still not very understanding.



Just write the example code, how to feel like listening and callback in Java. It may be better to understand that it will take more contact code to have a deep understanding.




vi. changes in memory management



So the change of memory management is the most noticeable problem in the work of C and C + + members that is memory management.



In contact with ios5.1 before, the Internet read most of the data, memory management has a very important play.



But, when I was studying, I found that this was played down.



The new features of the version now introduce the ARC mechanism. It feels completely the same as Java.



After you create the project selection arc option, you will find that you do not have to manually release the memory in the code. Release has been crossed out = =.


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.