C # comparison with objective-c Language Features

Source: Internet
Author: User

Compare the c # language with objective-c and record what you think is similar. Those who have learned objc believe they are no stranger to the category. It allows us to add additional methods to the original class without source code, is it possible to imagine that the extension method in c # is very similar. Yes, although they are called differently, they provide the same functionality. Next, let's talk about Protocol in objc. This is a Class-independent concept. Any class can implement the defined Protocol, which is similar to the interface in c, only the Protocol in objc has two optional and mandatory concepts. What are the two concepts? Optional means that when you implement this Protocol, you can choose to implement it or not, and the required option means you must implement it. How do we know whether the implementation class implements this optional method when the code is running? We can use the runtime characteristics of objc to judge it. The Code is as follows: 1 if ([self. delegate respondsToSelector: @ selector (aSelector)]) 2 {3 [self. delegate aSelector]; 4} In addition, I personally think this is the implementation of function programming in objc, much like lambda in c, however, if we want to access or modify a variable in the block body, we need to add two underscores before the variable so that we can modify it in the block body, it is also an implementation of closures in objc. KVO key value observation allows us to notify us of changes when class attributes change. It is actually an application of the "Observer" mode, this is a bit like the mechanism we implemented using MVVM in WPF. In MVVM, we inherit the VM from INotifyPropertyChanged and use it for notification, in objc, the following methods are used to complete 1-(void) addObserver :( NSObject *) observer forKeyPath :( NSString *) keyPath options :( NSKeyValueObservingOptions) options context :( void *) context; 2-(void) removeObserver :( NSObject *) observer forKeyPath :( NSString *) keyPath; 3-(void) willChangeValueForKey :( NSString *) key; 4-(void) didChangeValueForKey :( NSString *) key; 5-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context;

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.