ディクショナリ数値文字変換処理

2809 ワード

バックグラウンドで返されるデジタルフォーマットは、フロントエンドの文字表示でエラーが発生したため、以下を自分で書きました.
strcmp

C/C++ ,
str1,str2,
str1==str2, ; , 。
str1 str1>str2, 。

hファイル:

//
// NSDictionary+hdObjectForKeyAndToString.h
// JC
//
// Created by CML on 2017/7/19.
// Copyright © 2017 zhaozilong. All rights reserved.
//

import

@interface NSDictionary (hdObjectForKeyAndToString)

/**
NSNumber .

@param key keyString
@return resultString
/
-(NSString
)hdObjectForKeyAndToString:(NSString*)key;

@end


mファイル:

//
// NSDictionary+hdObjectForKeyAndToString.m
// JC
//
// Created by CML on 2017/7/19.
// Copyright © 2017 zhaozilong. All rights reserved.
//

import "NSDictionary+hdObjectForKeyAndToString.h"

@implementation NSDictionary (hdObjectForKeyAndToString)

-(NSString)hdObjectForKeyAndToString:(NSString)key{
id result=[self objectForKey:key];
if([result isKindOfClass:[NSNumber class]]){
return [self hdNumberString:result];
}
return [result description];
}

-(NSString)hdNumberString:(NSNumber)number{
NSString *result=@"";
const char *type=[number objCType];
if (strcmp (type, @encode (NSInteger)) == 0) {
result=[NSString stringWithFormat:@"%ld",[number integerValue]];
} else if (strcmp (type, @encode (NSUInteger)) == 0) {
result=[NSString stringWithFormat:@"%lu",[number unsignedIntegerValue]];
} else if (strcmp (type, @encode (int)) == 0) {
result=[NSString stringWithFormat:@"%d",[number intValue]];
} else if (strcmp (type, @encode (float)) == 0) {
result=[NSString stringWithFormat:@"%f",[number floatValue]];
} else if (strcmp (type, @encode (double)) == 0) {
result=[NSString stringWithFormat:@"%lf",[number doubleValue]];
} else if (strcmp (type, @encode (long)) == 0) {
result=[NSString stringWithFormat:@"%ld",[number longValue]];
} else if (strcmp (type, @encode (long long)) == 0) {
result=[NSString stringWithFormat:@"%lld",[number longLongValue]];
} else {
result=[NSString stringWithFormat:@"%ld",[number integerValue]];
}
if ([[result componentsSeparatedByString:@"."] count]>1) {
result=[result stringByReplacingOccurrencesOfString:@"0+$"
withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [result length])];
}
return result;
}

@end