Inherit Nsstring,nsarray how to write

Source: Internet
Author: User

Today, when conducting a string test, I've customized a class that inherits from NSString, but there are some problems, and when you look at the official documentation, you get the following conclusions

In objective-*str =*str = [bfstring stringWithFormat:@ "test"];

Operation Result:

after viewing official documents: To provide a storage mechanism for the characters in a string, these storage mechanisms can be static arrays, dynamically allocated caches, NSString objects, or other data types. The first is the need to provide a storage mechanism for a subclass of strings, followed by two methods to implement@interfacebfstring:nsstring{NSString*_backingstore;}@endthen in the implementation method, overwrite the above two methods length and Characteratindex:, of course, also to overwrite the initialization method@implementationbfstring- (ID) Initwithstring: (NSString *) astring{if(self =[self init]) {_backingstore=[[NSString stringwithstring:astring] copy]; }    returnSelf ;}-(Nsuinteger) length{return[_backingstore length];}-(Unichar) Characteratindex: (Nsuinteger) index{return[_backingstore Characteratindex:index];}@endstoring a string in _backingstore and then reading the string to get the length and character of the subclass string now, there is no problem inheriting this subclass of NSString. This practice also applies to Nsarray, nsdictionary, nsnumber, etc.

Inherit Nsstring,nsarray how to write

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.