【问题标题】:String to dictionary and backwards字符串到字典和向后
【发布时间】:2014-06-22 18:21:01
【问题描述】:

我为 ESTIMOTE Beacons 设计了一个解决方案,它不会让您每次经过该地区时都感到烦恼,但我是 Objective C、iOS、iPhone 等的新手,我无法真正在 Objective-C 中工作。我们在这里假设我们在区域中只有一个信标。因此,每次您进入一个区域时,它都会开始范围,找到唯一的 1 个信标并执行以下操作: 检查特定文件是否在特定路径上,如果不是,则创建文件并将字符串放入其中,例如“major_as_int:actual_time;”,通过 get 变量将主要值发送到 .php 文件,php 完成这项工作。

这是我的第一个问题。如何以“23:10:01”格式获取时间?

如果文件已经存在,请检查它是否为空,如果 FILE ISNT EMPTY 这里是我的第二个问题....

如何将字符串转换为字典,编辑字典,然后将字典转换回字符串,以便将文件中的旧字符串替换为字典中的新字符串?

例如:我的文件中已经有一个字符串“12345:12:45:12;54321:20:15:01;90900:05:21:13;”我想用它制作字典 { 12345 : 12:45:12, 54321 : 20:15:01, 90900 : 05:21:13 }。主要值应该是字典中的整数,所以我可以问字典中的实际主要值作为键。如果是,请检查它存在多长时间,如果 24 小时或更长时间结束,请再次执行 php。如果没有,就什么也不做。将相同的字符串返回到文件。如果实际主值不在字典中,则将实际主值添加为具有实际时间值的新键。字典看起来像 { 12345 : 12:45:12, 54321 : 20:15:01, 90900 : 05:21:13, 88771 : 10:12:33 }。然后字典被转换回字符串 "12345:12:45:12;54321:20:15:01;90900:05:21:13;88771:10:12:33;"

一些sn-ps的代码会很高兴看到但我愿意学习因为这是我未来的工作,所以请告诉我正确的方向并且不要发布太多代码。或者我可能以错误的方式攻击问题?提前致谢。

【问题讨论】:

  • 你是否有权将分隔符从“:”更改为字符串中的其他字符。例如:“12345#12:45:12”?
  • 是的,只是作为示例

标签: ios objective-c string dictionary time


【解决方案1】:

这应该将字符串转换为字典(虽然还没有测试过)。从这个例子中应该可以清楚地看到反向转换。

    NSString *string1 = @"12345:12:45:12;54321:20:15:01;90900:05:21:13;";
    NSArray *components1 = [string1 componentsSeparatedByString:@";"];

    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
    for (NSString *string2 in components1) {
        NSArray *components2 = [string2 componentsSeparatedByString:@":"];
        NSMutableArray *mutableComponents2 = [components2 mutableCopy];
        NSString *majorValue = [mutableComponents2 firstObject];

        // Check whether there is actually a majorValue
        if (majorValue) {
            [mutableComponents2 removeObjectAtIndex:0];
            mutableDictionary[majorValue] = [mutableComponents2 componentsJoinedByString:@":"];
        }
    }

    NSLog(@"mutableDictionary: %@", mutableDictionary);

编辑:如果您的字符串看起来像123#12:12:12;,这将更改为

    NSString *string1 = @"12345#12:45:12;54321#20:15:01;90900#05:21:13;";
    NSArray *components1 = [string1 componentsSeparatedByString:@";"];

    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
    for (NSString *string2 in components1) {
        NSArray *components2 = [string2 componentsSeparatedByString:@"#"];
        NSMutableArray *mutableComponents2 = [components2 mutableCopy];
        NSString *majorValue = [mutableComponents2 firstObject];

        // Check whether there is actually a majorValue
        if (majorValue) {
            mutableDictionary[majorValue] = [mutableComponents2 lastObject];
        }
    }

    NSLog(@"mutableDictionary: %@", mutableDictionary);

【讨论】:

  • 如果我的字符串看起来像“123#12:12:12;”,if 中的第二个语句会如何?关于 NSArray *compenents1。最后一个“;”会发生什么? components1 是否有一个空字符串作为数组中的最后一个对象?
  • @onskulis 查看我的更改。
  • 字符串中的最后一个半列呢?
  • 这由if (majorValue) { 处理。试试看,加断点。
【解决方案2】:

为了回答您的第一个问题,关于如何获取 23:59:59 格式的时间,我提供以下代码。

#import <Foundation/Foundation.h>

@implementation NSDateFormatter(myCategory)
+ (NSDateFormatter *)for24Hours {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSString *template = [[self class] dateFormatTemplateFor24Hours];
    [formatter setDateFormat:template];
    return formatter;
}
+ (NSString *)dateFormatTemplateFor24Hours {
    NSString  *template = @"HHmmss";
    const NSUInteger options = 0;
    NSLocale *locale = [NSLocale currentLocale];
    NSString *dateAs24Hours = [NSDateFormatter dateFormatFromTemplate:template options:options locale:locale];
    return dateAs24Hours;
}
@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSDateFormatter *formatter = [NSDateFormatter for24Hours];
        NSDate *now = [NSDate date];
        NSLog(@"%@", [formatter stringFromDate:now]);
    }
    return 0;
}

【讨论】:

  • 我刚刚将没有主函数的 sn-p 复制并粘贴到我的 ViewController.m 中,效果很好,谢谢,但是我已经向 NSDateFormater(myCategory) 添加了一个方法并将其命名为“getDate”,它返回您示例中的 NSLog 返回的内容。我怎样才能调用我的方法?我尝试了很多变体,但没有任何效果。
  • 试试[NSDateFormatter getDate];,我建议您阅读有关类别的内容,如果您不熟悉这个概念,Apple 有很好的文档developer.apple.com/library/mac/documentation/cocoa/conceptual/…
猜你喜欢
  • 2014-06-07
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 2012-10-11
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多