Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在数组中创建方法?还是更短?_Objective C - Fatal编程技术网

Objective c 在数组中创建方法?还是更短?

Objective c 在数组中创建方法?还是更短?,objective-c,Objective C,我想把它缩短或者占用更少的空间 totalTime = [self timeFormatted:([currentFeed duration].intValue)-1]; NSString *word = @":00:"; if ([totalTime rangeOfString:word].location == NSNotFound) { totalTime = [totalTime stringByReplacingOccurrencesOfString:

我想把它缩短或者占用更少的空间

 totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
    NSString *word = @":00:";
    if ([totalTime rangeOfString:word].location == NSNotFound) {
        totalTime = [totalTime stringByReplacingOccurrencesOfString:@"00:" withString:@""];
        totalTime = [totalTime stringByReplacingOccurrencesOfString:@"01:" withString:@"1:"];
        totalTime = [totalTime stringByReplacingOccurrencesOfString:@"02:" withString:@"2:"];
        totalTime = [totalTime stringByReplacingOccurrencesOfString:@"03:" withString:@"3:"];
        totalTime = [totalTime stringByReplacingOccurrencesOfString:@"04:" withString:@"4:"];
        totalTime = [totalTime stringByReplacingOccurrencesOfString:@"05:" withString:@"5:"];
        totalTime = [totalTime stringByReplacingOccurrencesOfString:@"06:" withString:@"6:"];
        totalTime = [totalTime stringByReplacingOccurrencesOfString:@"07:" withString:@"7:"];
        totalTime = [totalTime stringByReplacingOccurrencesOfString:@"08:" withString:@"8:"];
        totalTime = [totalTime stringByReplacingOccurrencesOfString:@"09:" withString:@"9:"];
    }

非常感谢您的帮助。

为NSString添加一个类别:

@implementation NSString (replace)

-(void) replace:(NSString*)old with:(NSString*)new{

self = [self stringByReplacingOccurrencesOfString:old  withString:new];
}
所以你只需要打电话:
[totalTime将:@“00:”替换为:@”“

为NSString添加一个类别:

@implementation NSString (replace)

-(void) replace:(NSString*)old with:(NSString*)new{

self = [self stringByReplacingOccurrencesOfString:old  withString:new];
}
所以你只需要打电话:
[totalTime将:@“00:”替换为:@”“

您可以将totalTime设置为可变字符串。然后,您可以将映射放入NSDictionary并对其进行迭代

NSMutableString *ms = [[totalTime mutableCopy] autorelease];

NSDictionary *d = @{@"00":@"", @"01:":@"1:", @"02:":@"2:" /* ... */};

[d enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    [ms replaceOccurrencesOfString:key withString:obj options:NSCaseInsensitiveSearch range:NSMakeRange(0, [ms length])];
}];

totalTime = ms;

顺便说一句,如果您试图格式化日期,请查看。

您可以将totalTime设置为可变字符串。然后,您可以将映射放入NSDictionary并对其进行迭代

NSMutableString *ms = [[totalTime mutableCopy] autorelease];

NSDictionary *d = @{@"00":@"", @"01:":@"1:", @"02:":@"2:" /* ... */};

[d enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    [ms replaceOccurrencesOfString:key withString:obj options:NSCaseInsensitiveSearch range:NSMakeRange(0, [ms length])];
}];

totalTime = ms;

顺便说一句,如果您试图格式化日期,请查看。

使用正则表达式采用不同的方法:

NSError *error = NULL;
// replace 0X: with X: where X is 1-9
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"0([1-9]:)" options:0 error:&error];
date = [regex stringByReplacingMatchesInString:date options:0 range:NSMakeRange(0,date.length) withTemplate:@"$1"];

// remove 00: if not preceded by :
regex = [NSRegularExpression regularExpressionWithPattern:@"(?<!:)00:" options:0 error:&error];
date = [regex stringByReplacingMatchesInString:date options:0 range:NSMakeRange(0,date.length) withTemplate:@""];
NSError*error=NULL;
//将0X:替换为X:其中X是1-9
NSRegularExpression*regex=[NSRegularExpression regular expressionwithpattern:@“0([1-9]:)”选项:0错误:&error];
日期=[regex StringByReplacingMatchesInstalling:date选项:0范围:NSMakerRange(0,date.length),带模板:@“$1”];
//删除00:如果前面没有:

regex=[NSRegularExpression regular expression with pattern:@”(?对正则表达式采用不同的方法:

NSError *error = NULL;
// replace 0X: with X: where X is 1-9
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"0([1-9]:)" options:0 error:&error];
date = [regex stringByReplacingMatchesInString:date options:0 range:NSMakeRange(0,date.length) withTemplate:@"$1"];

// remove 00: if not preceded by :
regex = [NSRegularExpression regularExpressionWithPattern:@"(?<!:)00:" options:0 error:&error];
date = [regex stringByReplacingMatchesInString:date options:0 range:NSMakeRange(0,date.length) withTemplate:@""];
NSError*error=NULL;
//将0X:替换为X:其中X是1-9
NSRegularExpression*regex=[NSRegularExpression regular expressionwithpattern:@“0([1-9]:)”选项:0错误:&error];
日期=[regex StringByReplacingMatchesInstalling:date选项:0范围:NSMakerRange(0,date.length),带模板:@“$1”];
//删除00:如果前面没有:

regex=[NSRegularExpression regular expression with pattern:@“(?NSDictionary*d={@“00”:“@“,@”01::@“1:”,@“02::@”2:};我尝试了这个方法,得到了这个错误“预期”}”,您的编译器可能没有使用Objective-C文本。请尝试以这种方式声明字典:
NSDictionary*d=[NSDictionary dictionary dictionary dictionary WithObjectsAndKeys:@“00”'您的编译器可能不使用Objective-C文本。请尝试这样声明字典:
NSDictionary*d=[NSDictionary dictionary WithObjectSandKeys:@'、@“1:”、@“01:”、@“2:”、@“02:”、/*…*/nil];
您在曲线括号前面缺少一个@:
NSDictionary*d={“00:@'、@“01::@“1:”、@“02:”、@“02:”、@“2:”};
谢谢@Herm我把这个打字错误归咎于Python:)