Skip to content

Instantly share code, notes, and snippets.

@Shilo
Last active April 24, 2018 18:44
Show Gist options
  • Save Shilo/6409077 to your computer and use it in GitHub Desktop.
Save Shilo/6409077 to your computer and use it in GitHub Desktop.
A category for NSString to reverse string.
//
// NSString+Reverse.h
//
// Created by Shilo White on 9/1/13.
// Copyright (c) 2013 Shilocity Productions. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSString (Reverse)
@property (nonatomic, readonly) NSString *reversedString;
@end
//
// NSString+Reverse.m
//
// Created by Shilo White on 9/1/13.
// Copyright (c) 2013 Shilocity Productions. All rights reserved.
//
#import "NSString+Reverse.h"
@implementation NSString (Reverse)
- (NSString *)reversedString
{
NSMutableString *reversedString = [NSMutableString stringWithCapacity:self.length];
[self enumerateSubstringsInRange:NSMakeRange(0,self.length)
options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences)
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[reversedString appendString:substring];
}];
return reversedString;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment