Category (category)

Source: Internet
Author: User

Category (category)

Category-string reverse output

 

 

Directory

NSString + Reverse. h

#import <Foundation/Foundation.h>@interface NSString (Reverse)-(NSString *)reverse;@end

NSString + Reverse. m

# Import "NSString + Reverse. h "@ implementation NSString (Reverse)-(NSString *) reverse {NSMutableString * str = [NSMutableString string]; // for (int I = (int) self. length-1; I> = 0; I --) {// reverse char ch of the string = [self characterAtIndex: I]; // Add a string to [str appendFormat: @ "% c", ch];} return str ;}@ end

NSString + Catel. h

#import <Foundation/Foundation.h>@interface NSString (Catel)-(NSNumber *)lengthAsString;@end

NSString + Catel. m

#import "NSString+Catel.h"@implementation NSString (Catel)-(NSNumber *)lengthAsString{    int len= (int)self.length;    return [NSNumber numberWithInt:len];}@end

Main. m

# Import <Foundation/Foundation. h> # import "NSString + Reverse. h "# import" NSString + Catel. m "int main (int argc, const char * argv []) {@ autoreleasepool {NSString * str = @" abc "; // output NSLog in reverse order (@" % @", [str reverse]); NSLog (@ "% d", [str lengthAsString]. intValue);} return 0 ;}

Running result

Category (Category) --- set reverse output

 

Directory

 

NSArray + Reversre. h

#import <Foundation/Foundation.h>@interface NSArray (Reversre)-(NSArray *)reverse;@end

NSArray + Reversre. m

# Import "NSArray + Reversre. h "@ implementation NSArray (Reversre)-(NSArray *) reverse // implement the reverse output method {NSMutableArray * array = [NSMutableArray array]; // less than the number of sets for (int I = (int) self. count-1; I> = 0; I --) {// Add a set using the inverted objectAtIndex [array addObject: [self objectAtIndex: I] of the addObject set;} return array;} @ end

Main. m

# Import <Foundation/Foundation. h> # import "NSArray + Reversre. h "int main (int argc, const char * argv []) {@ autoreleasepool {// NSArray * array = [NSArray arrayWithObjects: @" ", @ "B", @ "c", @ "d", @ "e", nil]; NSArray * arr = [array reverse]; NSLog (@ "% @", arr);} return 0 ;}

Running result:

 

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.