Basic knowledge about object-c

Source: Internet
Author: User

 

The process of program startup is summarized as follows:

1. The main function of the program entry creates the UIApplication instance and the UIApplication proxy instance.

2. Rewrite the startup method in the UIApplication proxy instance and set the first ViewController.

3. Add controls to the first ViewController to implement the application interface.

 

1. Use [] to call a method, and inherit from:

 

2. Instance Object

1. NSString * string1 = [NSString string]; // automatically released

2. NSString * string2 = [[NSString alloc] init]; // manually released after use, alloc allocates memory and instantiates, init Initialization

[String2 release];

 

3. Create an object

In Objective-C syntax, a class is usually divided into two parts. H file declaration, m file implementation.

The methods are all public and the member variables are all protect. (before the method, use the minus sign (public) and the + sign (static) method)

Protocol is equivalent to java Interface

H file, which provides get and set accessors:

 

#import 
 
  @interface Photo : NSObject {NSString* caption;NSString* photographer;}@property (retain) NSString* caption;@property (retain) NSString* photographer;@end
 

 

 

 

M file, @ synthesize command generates setter and getter for us, so what we have to do is
Only the dealloc method is implemented.
The @ synthesize will automatically generate the accessors only when the accessors do not exist.
Use @ synthesize to declare an attribute. You can still implement custom getter and setter.

 

#import Photo.h@implementation Photo@synthesize caption;@synthesize photographer;- (void) dealloc{[caption release];[photographer release];[super dealloc];}@end


 

 


Release objects in the dealloc Method

 

‐ (void) dealloc{    self.caption = nil;    self.photographer = nil;    [super dealloc];}


 

 

4. nil

It is equivalent to the null pointer of other languages. The difference is that calling methods on nil will not cause exceptions.

 

5. Category)

Similar to rewriting (the focus is the brackets between the @ implementation and @ interface lines)

 

#import 
 
  @interface NSString (Utilities)- (BOOL) isURL;@end
 

 

 

 

Note: The name of the class to be overwritten by import + the name in the brackets
#import NSString-Utilities.h@implementation NSString (Utilities)- (BOOL) isURL{if ( [self hasPrefix:@http://] )return YES;elsereturn NO;}@end

6. IBAction, IBOutlet static interface and code Association

 

By adding IBOutlet before the variable, it means that the variable will correspond to a UI object on the interface,

Add IBAction before the method to indicate that the method will correspond to the event on the interface.

 

7. Exception (exception handling is only supported by Mac OS X 10.3 or above)

@ Try {}

@ Catch {Exception * e}

@ Finally {}

 

8. id type. You do not need to know the above type. If you have this method, the response will be returned. You do not need to convert the type as in java to call the method.

 

9. NSEnumerator (NSArray cannot change the length, NSMutableArray can)

NSDictionary is equivalent to java map

 

NSArray *array = [NSArray array ];NSEnumerator *enumerator = [array objectEnumerator];id obj;while ( obj = [enumerator nextObject] ) {printf( %s, [[obj description] cString] );}


 

 

 

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.