【问题标题】:NSArray of NSStrings is returning just one big string in one object, and I need a lot of objectsNSStrings 的 NSArray 在一个对象中只返回一个大字符串,我需要很多对象
【发布时间】:2013-05-31 19:36:23
【问题描述】:

我有一个 NSArray,它通过 JSON 获取 WS 一些日期值。 这就是我的 NSLOG 返回的内容: "2013-05-31T17:00:00Z", "2013-05-31T17:30:00Z", "2013-05-31T18:00:00Z", "2013-05-31T20:00:00Z", ...

我需要一个只返回小时和分钟的新 NSArray,如下所示: "17:00", "17:30", "18:00", "20:00", ...

我正在做类似的事情:

        for (int a =0; a<array2.count; a++)
        {
            NSString *myString = [array2 objectAtIndex:a];
            NSString *horaDeInicio = [myString substringWithRange:NSMakeRange(11, 5)];
            [horariosProgramas addObject:horaDeInicio];
        }

回复正是我所需要的,但回复只有一大串。所以我的数组只有一个对象。我需要在对象中分离这个大字符串。我怎样才能做到这一点?? 谢谢!

【问题讨论】:

  • 你能告诉我们你是如何创建然后显示或使用horariosProgramas从而得出结论它变成了一个大字符串吗?
  • in viewDidLoad: horariosProgramas = [[NSMutableArray alloc] init];当我在 tableview detailEmissoraViewController.horariosProgramas = [horariosProgramas objectAtIndex:[sender tag]] 中连续选择一个按钮时;而detailEmissoraViewController,horariosPrograma是一个属性。在 cellForRowAtIndexPath 我尝试使用 NSString *subCellValue = [horariosProgramas objectAtIndex:indexPath.row]; cell.detailTextLabel.text = subCellValue;但它崩溃了 -[__NSCFString objectAtIndex:]: unrecognized selector sent to instance
  • 但是如果你已经完成了.horariosProgramas = [horariosProgramas objectAtIndex:[sender tag]];,那么本地属性肯定是一个字符串,而不是一个数组,当你下次尝试subCellValue = [horariosProgramas objectAtIndex:indexPath.row];时会导致问题?
  • 哼,是的,你是对的!那么我该如何解决这个问题??

标签: ios json nsstring nsmutablearray substring


【解决方案1】:

只需用 T 和 Z 字符分隔即可。做这样的事情:

for (int a =0; a<array2.count; a++)
{
     NSString *myString = [array2 objectAtIndex:a];
     NSArray *arr = [myString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"TZ"]];
     NSString *yourDate = [arr objectAtIndex:1];

     [horariosProgramas addObject:yourDate];
}

使用打印语句确认您的数组索引,然后您就可以上路了。

【讨论】:

    【解决方案2】:

    从长远来看,使用 dateFormatter 转换日期会更好,并且您想更改显示格式或其他内容

    NSArray *array2= @[@"2013-05-31T17:00:00Z", 
                       @"2013-05-31T17:30:00Z", 
                       @"2013-05-31T18:00:00Z", 
                       @"2013-05-31T20:00:00Z"];
    
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]];
    
    NSString *inputDateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
    NSString *outputDateFormat = @"HH:mm";
    
    for (NSString *dateString in array2) {
        [dateFormatter setDateFormat:inputDateFormat];
        NSDate *date = [dateFormatter dateFromString:dateString];
    
        [dateFormatter setDateFormat:outputDateFormat];
        [horariosProgramas addObject:[dateFormatter stringFromDate:date]];
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-20
      • 1970-01-01
      • 2013-11-12
      • 2014-12-14
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 2010-10-12
      • 2022-01-18
      相关资源
      最近更新 更多