Adding Java-style methods to NSString

Source: Internet
Author: User

Adding Java-style methods to NSString

Article Directory

I can't stand it. nsstring lengthy method calls, check the document before each Write. In particular, the way to get rid of the extra space before and after it is ridiculously long. In other languages, java, the corresponding method is called Trim. For python, The corresponding method is called Strip. But what about objective-c? is the following paragraph:

[selfstringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset];

In fact, I also understand that the advantage of the long function name of OBJECTIVE-C is that it is clear to know what the current method is. But some common methods, simple method names can also express the clear meaning, and easier to Remember. The biggest problem with long method names is that the code writes halfway through the document and interrupts the idea, so I spent a day at the end of the week wrapping up the Objective-c NSString class and replacing the corresponding method calls with Java. So I do not have to write so long and can't remember the method name every time I use it.

The main encapsulated method is defined as Follows:

@interfaceNSString (Wrapper)
- (Unichar) CharAt: (Int) index;
- (Int) CompareTo: (Nsstring*) anotherstring;
- (Int) Comparetoignorecase: (Nsstring*) str;
- (BOOL) Contains: (Nsstring*) str;
- (BOOL) StartsWith: (Nsstring*) prefix;
- (BOOL) EndsWith: (Nsstring*) suffix;
- (BOOL) Equals: (Nsstring*) anotherstring;
- (BOOL) Equalsignorecase: (Nsstring*) anotherstring;
- (Int) Indexofchar: (Unichar) ch;
- (Int) Indexofchar: (Unichar) Ch FromIndex: (Int) index;
- (Int) Indexofstring: (Nsstring*) str;
- (Int) Indexofstring: (Nsstring*) Str fromIndex: (Int) index;
- (Int) Lastindexofchar: (Unichar) ch;
- (Int) Lastindexofchar: (Unichar) Ch FromIndex: (Int) index;
- (Int) Lastindexofstring: (Nsstring*) str;
- (int) lastindexofstring: (nsstring*) str fromIndex: ( int) index;
-(nsstring *) substringfromindex: (int) Beginindex toindex: (int) endIndex;
-(nsstring *) tolowercase;
-(nsstring *) touppercase;
-(nsstring *) trim;
-(nsstring *) replaceall: (nsstring*) origin with :(nsstring*) replacement;
-(nsarray *) split: (nsstring*) separator;
@end

Look at the method body, all of a sudden feel a lot of ease, and then do not have to write every time to check the Document. The code is on github, you can get the code with the following command or directly access the URL Https://github.com/tangqiaoboy/xcode_tool:

Clone [email Protected]:tangqiaoboy/xcode_tool.git
CD Xcode_tool/nsstringwrappeer

If you want to add this Wrapper to an existing project, just copy my files: NSStringWrapper.h and nsstringwrapper.m to your own project. When using the note I modified a little, I will look for the Non-existent return value set to 1, so as to be consistent with the Java language, if you want to use nsnotfound as a result of the search does not exist, please modify the file nsstringwrapper.m the top of the macro definition: javanotfound, as shown below:

If you don't like it, you can change the 1 to Nsnotfound.
#define JavaNotFound-1

Have fun!

Ways to add Java-style to NSString

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.