NSString 位移運算 (shift operation)

自己寫了一個簡單的位移運算
因為沒有偵測邊界 所以用try包起來
import後 會繼承在nsstring
直接使用即可

ex:
 NSString *temp=[[NSString alloc] initWithString:@"test"];
 NSlog("Shift=> %@",[temp shift:3]);


ShiftEncode.h
#import 

@interface NSString (ShiftEncode) 
- (NSString*)Shift:(int)offset;
@end

ShiftEncode.m
#import "ShiftEncode.h"

@implementation NSString (ShiftEncode)

- (NSString*)Shift:(int)offset {
    NSMutableString* decoded = [NSMutableString stringWithString:self];
    
    NSMutableString* DecodeStr=[[NSMutableString alloc] init];
    @try{
    for (int i=0;i<[decoded length];i++) {
        UTF16Char xx=[decoded characterAtIndex:i];
        if (xx==' ') {
            [DecodeStr appendFormat:@" "];
        }else {
            UTF16Char newChar=xx+offset;
            [DecodeStr appendFormat:@"%C",newChar];
        }
    }
    }@catch (NSException *EXP) {
        NSLog(@"NSSTRING SHIFT :%@",EXP);
        DecodeStr=nil;
    }  
    return DecodeStr;
}

@end
See also : bitbucket

留言

這個網誌中的熱門文章

Office 2021 離線安裝封裝與KMS啟動步驟

Ollama使用心得與模型導入教學

ARC下NSMutableDictionary 無法使用retainCount