iOS Development--Project Combat Summary & Show you the essence of objective-c

Source: Internet
Author: User

I'll show you the essence of objective-c.

1: Interface and implementation

    • @interface...@end
    • @implementation...@end
    • @class

Interface (header file)

Implementation file

Forward reference

Note: Categories extend the behavior of existing classes by adding new classes and instance methods. As a rule, categories are defined in their own .{h,m} files,

2: Instance variable visibility

    • @public
    • @package
    • @protected
    • @private

@public: Instance variables can person->age = 32" be read directly using symbols.

@package: instance variable is public unless it is specified outside the frame (64-bit schema only)

@protected: Instance variables can only be accessed by their classes and their derived classes

@private: Instance variables can only be accessed by their classes

3: Properties

    • @property
    • @synthesize
    • @dynamic

One thing to note about attributes is that, starting with Xcode 4.4, we no longer need explicit compositing attributes. The @interface attributes declared in the implementation are automatically synthesized (along with the underlined Ivar name, for example @synthesize propertyName = _propertyName ).

@dynamic are relative to the @synthesize, they are used to modify the @property to generate the corresponding getter and setter methods. But @ dynamic means that the getter and setter methods of this member variable are not generated directly by the compiler, but are generated either manually or at run time.

4: Protocol

    • @protocol
    • @required
    • @optional

Defines a set of methods that will be implemented in classes that obey the protocol, as if they were added to the interface of that class.

You can customize a protocol in more depth by specifying a method that is necessary and optional. The optional method is stub on the interface so that it can be completed automatically by Xcode but does not generate a warning if the method is not implemented. The protocol method is required by default

5: Exception Handling

    • @try
    • @catch
    • @finally
    • @throw

Objective-c mainly through NSError the communication of unexpected abnormal state. While other languages use exception handling, OBJECTIVE-C will downgrade exceptions and programmer errors to the behavior of true exceptions.

6: Object Constants

    • @""
    • @42@3.14@YES,@‘Z‘
    • @[]
    • @{}
    • @()

@"": Returns an object that is initialized by the Unicode content in quotation marks NSString .

@42, @3.14 , @YES , @‘Z‘ : Returns an object that is initialized by a related class NSNumber , such as @42[NSNumber numberWithInteger:42] , or @YES[NSNumber numberWithBool:YES] . Support for further specifying types using suffixes, such as @42U[NSNumber numberWithUnsignedInt:42U] .

@[]: Returns an object with a colon-delimited list of objects as content NSArray . For example, @[@"A", @NO, @2.718][NSArray arrayWithObjects:@"A", @NO, @2.718, nil] (note that closing a tag in an array constant nil is not required).

@{}: Returns an object that is initialized as content by a specific key-value pair, in the NSDictionary format: @{@"someKey" : @"theValue"} .

@(): Dynamically evaluates encapsulated representations and returns appropriate object constants based on their values (for example, return, const char* NSString int return NSNumber , etc.). )。 This is also 枚举 the specified way of using numeric constants and values.

7:objective-c Constants

    • @selector()
    • @protocol()

@selector(): Returns a pointer to a selector with a specific name SEL . Used -performSelector:withObject: in similar ways.

@protocol(): Returns a pointer to a protocol with a specific name Protocol * . Used -conformsToProtocol: in similar ways.

8:c Constants

    • @encode()
    • @defs()

@encode(): Returns the type encoding of a type. This type value can be used for NSCoder -encodeValueOfObjCType:at the first parameter encoding in.

@defs(): Returns the layout of a objective-c class. For example, to define a NSObject struct with the same layout, you only need this:

9: Optimization

    • @autoreleasepool{}
    • @synchronized{}

@autoreleasepool{}: If your code contains tight loops that create a lot of temporary objects, you can @autorelease optimize them by more aggressively releasing these short-lived, locally scoped objects. @autoreleasepoolReplace and improve the old and slow and cannot be used in Arc NSAutoreleasePool .

@synchronized(){}: This instruction self provides a convenient way to ensure that a particular block is safely executed in a particular environment. The deadlock in this case is expensive, so for classes that are thread-safe at a particular level, it is recommended to use a dedicated NSLock property or use OSAtomicCompareAndSwap32(3) the underlying deadlock function.

10: Compatible

    • @compatibility_alias

Allows existing classes to have different names as aliases.

iOS Development--Project Combat Summary & Show you the essence of objective-c

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.